1、在一个Activity中调用startActivity()方法。
2、在一个Activity中调用startActivityRequest()方法。
具体可参见:http://blog.csdn.net/fenghome/article/details/8520414
跳转activity的几种方式,主要是通过intent意图,分为显示和隐式intent
1.显示跳转
A.class跳转
用例:
1
2
3
|
Intent it =
new
Intent(A.
this
,B.
class
);
//it.putExtra("key","value"); //跳转时传的参数
startActivity(it);
|
使用场景:应用内部跳转,频率较高
B.包名、类名跳转
用例:
1
2
3
4
|
Intent it=
new
Intent();
it.setClassName(
"cn.thear"
,
"cn.thear.MainActivity"
);
//包名和类名
//it.putExtra("key","value"); //传一些参数
startActivity(it);
|
使用场景:内部跳转+外部跳转。
C.ComponentName跳转
用例:
1
2
3
4
|
Intent it=
new
Intent();
it.setComponent(
new
ComponentName(<span></span>
"cn.thear"
,
"cn.thear.MainActivity"
));
//包名和类名
//it.putExtra("key","value"); //传一些参数
startActivity(it);
|
使用场景:内部跳转+外部跳转。
//intent = new Intent();
//ComponentName cn = new ComponentName("org.cyj604b.mylauncher", "org.cyj604b.mylauncher.MyLauncher");
//intent.setComponent(cn);
//startActivity(intent);
注:在外部跳转时,当要跳转第三应用时,只知道包名和类名,就推荐用B或C跳转了。但这个不是特别灵活,比如在要跳转的activity前有个过渡初始化页面时,那直接跳过去肯定会出问题的啦。
2.隐式跳转
1
2
3
|
Intent it =
new
Intent(
"com.custom.skip.action"
);
//参数要跳转的activity中定义的action名,这个action是在androidManifest.sml中定义
//it.putExtra("key","value"); //跳转时传的参数
startActivity(it);
|
使用场景:内部跳转+外部跳转。推荐非第三应用的外部跳转,这样方便约定此action跳转
3.跳转时遇到的问题
####在外部跳转(尤其是第三方应用跳转系统应用)时,相信大家肯定遇到了个权限问题######
1
|
10
-
21
12
:
08
:
13.183
: E/AndroidRuntime(
25061
): java.lang.SecurityException: Permission Denial: starting Intent { cmp=cn.thear/cn.thear.MainActivity} from ProcessRecord{
42212920
25061
:com.example.outappopenactivity/u0a10026} (pid=
25061
, uid=
10026
) not exported from uid
1000
|
这个是因为2个应用不在同一个用户组里,是跨进程跳转。需要在AndroidManifest.xml中要跳转的activity中加入一个属性android:exported="true",允许外部跳转,此时能够跳转成功。
但是,这个属性的介绍是这样的:
http://developer.android.com/guide/topics/manifest/activity-element.html#exported
当这个值为“false”时,只能在同一个应用或者有相同的user id(比如跳转系统应用,则要添加system uid,并签名)的应用里被启动。
当为“true”,不管什么组件,都能启动这个activity。
这个属性的默认值依赖于这个activity是否声明了intent-filter,如果没有,则为false;否则为默认为true.
通过包名启动activity的方式:
方式一:
public static void runAppByPackageName(Context context, String packageName) {
PackageManager pm = context.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(packageName);
if (intent != null) {
context.startActivity(intent);
} else {
Toast.makeText(context, "找不到应用", Toast.LENGTH_SHORT).show();
}
}
方式二:
private void openApp(String packageName) {
PackageInfo pi;
try {
pi = getPackageManager().getPackageInfo(packageName, 0);
PackageManager pm = this.getPackageManager();
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null) {
String packageName2 = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName2, className);
intent.setComponent(cn);
startActivity(intent);
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(), "应用没找到", Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), "系统应用", Toast.LENGTH_SHORT).show();
}
}