当前位置: 代码迷 >> Android >> Android 匿名启动activity 起步系统activity
  详细解决方案

Android 匿名启动activity 起步系统activity

热度:54   发布时间:2016-04-28 00:57:18.0
Android 匿名启动activity 启动系统activity

一般我们使用Intent 进行activity跳转时我们都知道需要跳转的activity的名字,例如:

				Intent intent=new Intent(FirstActivity.this,SecondActitivy.class);				startActivity(intent);
SecondActitivy.class和FirstActivity不再同一个App的时候,我们就需要用到匿名启动,

匿名启动:

首先需要设置被启动的SecondActivity 的xml配置文件:

   <activity android:name="testIntent.SecondActitivy" >            <intent-filter>                <action android:name="toSecondPage" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>
FirstActivity 可以利用<action android:name="toSecondPage" />

来找到 SecondActivity

FirstActivity代码如下:

<span style="white-space:pre">				</span>Intent intent=new Intent();				intent.setAction("toSecondPage");				startActivity(intent);


这样就可以利用ActionName调到非本App的Activity


启动系统activity也是这样实现:

通过一个系统提供的ActionName来跳转到系统的Activity,同时可以附加一些消息;(xml 就是4个简单的按钮)

package testIntent;import java.net.URL;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.example.androidtest.R;public class FirstActivity extends Activity implements OnClickListener{		private Button toWeBButton;	private Button toPicButton;	private Button toMesButton;	private Button toPhoneButton;		@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.first_activity_main);			toWeBButton=(Button) findViewById(R.id.toWeb);		toPicButton=(Button) findViewById(R.id.toPic);		toMesButton=(Button) findViewById(R.id.toMes);		toPhoneButton=(Button) findViewById(R.id.toPhone);				toWeBButton.setOnClickListener(this);		toPicButton.setOnClickListener(this);		toMesButton.setOnClickListener(this);		toPhoneButton.setOnClickListener(this);	}	@Override	public void onClick(View v) {		// TODO Auto-generated method stub		Intent intent=new Intent();				if(v.equals(toWeBButton)){//跳转到 网页 百度首页			intent.setAction(Intent.ACTION_VIEW);			Uri uri=Uri.parse("http://www.baidu.com");			intent.setData(uri);		}else if(v.equals(toPicButton)){//打开系统图片			intent.setAction(Intent.ACTION_GET_CONTENT);			intent.setType("image/*");//打开所有的图片, 如果需要获取图片就需要写回调函数		}else if(v.equals(toMesButton)){//发送消息			intent.setAction(Intent.ACTION_SEND);			intent.setType("text/plain");			intent.putExtra(Intent.EXTRA_TEXT, "这是我第一次这样发信息");					}else if(v.equals(toPhoneButton)){//拨打电话			intent.setAction(Intent.ACTION_VIEW);			Uri uri=Uri.parse("tel:1839860592");			intent.setData(uri);		}		startActivity(intent);	}}



  相关解决方案