Android(安卓)从一个应用打开另一个应用
16lz
2021-01-26
如果你知道目标应用的包名和想打开的界面类名,那就很方便。
//这些代码是启动另外的一个应用程序的主Activity,当然也可以启动任意一个Activity ComponentName componetName = new ComponentName( //这个是另外一个应用程序的包名 "com.poynt.weibo", //这个参数是要启动的Activity "com.poynt.weibo.ui.IndexActivity"); try { Intent intent = new Intent(); intent.setComponent(componetName); startActivity(intent); } catch (Exception e) { // Toast.makeText(getApplicationContext(), "可以在这里提示用户没有找到应用程序,或者是做其他的操作!", 0).show();
如果你不知道目标应用的界面类名,可以使用下面的方法,启动目标应用。
/** 获取PackageManager对象 */ PackageManager packageManager = getPackageManager(); PackageInfo pi = null; try { pi = packageManager.getPackageInfo("com.welove520.welove", 0); } catch (PackageManager.NameNotFoundException e) { } // 设置标签内需要满足的条件 Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); resolveIntent.setPackage(pi.packageName); // 通过queryIntentActivities获取ResolveInfo对象 List apps = packageManager.queryIntentActivities(resolveIntent, 0); ResolveInfo ri = apps.iterator().next(); if (ri != null ) { String className = ri.activityInfo.name; Intent intent = new Intent(); ComponentName cn = new ComponentName("com.welove520.welove", className); intent.setComponent(cn); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Context context = getApplicationContext(); context.startActivity(intent); }
更多相关文章
- Android(安卓)基础
- Android(安卓)WMS分析(一) WindowManager
- Android(安卓)5.0 SystemServer--->Android入口
- 【Android】Activity_习题
- Android应用启动优化:一种DelayLoad的实现和原理
- android之mainfest 参数解说
- Android(安卓)Framework初步认识
- android使用广播退出应用程序
- VLC for Android源码下载和编译