从Android上的另一个应用程序启动应用程序
我想从我的Android应用程序启动已安装的软件包。 我认为这是可能的使用意图,但我没有find一个办法。 有没有链接,在哪里可以find信息?
如果您不知道主要活动,则可以使用软件包名称来启动应用程序。
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address"); if (launchIntent != null) { startActivity(launchIntent);//null pointer check in case package name was not found }
我知道这已经回答了,但这里是我如何实现类似的东西:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.package.name"); if (intent != null) { // We found the activity now start the activity intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } else { // Bring user to the market or let them choose an app? intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("market://details?id=" + "com.package.name")); startActivity(intent); }
更好的是,这里是方法:
public void startNewActivity(Context context, String packageName) { Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName); if (intent != null) { // We found the activity now start the activity intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } else { // Bring user to the market or let them choose an app? intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("market://details?id=" + packageName)); context.startActivity(intent); } }
删除重复的代码:
public void startNewActivity(Context context, String packageName) { Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName); if (intent == null) { // Bring user to the market or let them choose an app? intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=" + packageName)); } intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); }
我find了解决scheme。 在应用程序的清单文件中,我find了包名:com.package.address和我想要启动的主要活动的名称:MainActivity以下代码启动此应用程序:
Intent intent = new Intent(Intent.ACTION_MAIN); intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity")); startActivity(intent);
这里是我的例子,如果有人认为它有用,从我的应用程序启动酒吧/二维码扫描仪
Intent intent = new Intent("com.google.zxing.client.android.SCAN"); intent.setPackage("com.google.zxing.client.android"); try { startActivityForResult(intent, SCAN_REQUEST_CODE); } catch (ActivityNotFoundException e) { //implement prompt dialog asking user to download the package AlertDialog.Builder downloadDialog = new AlertDialog.Builder(this); downloadDialog.setTitle(stringTitle); downloadDialog.setMessage(stringMessage); downloadDialog.setPositiveButton("yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); try { myActivity.this.startActivity(intent); } catch (ActivityNotFoundException e) { Dialogs.this.showAlert("ERROR", "Google Play Market not found!"); } } }); downloadDialog.setNegativeButton("no", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int i) { dialog.dismiss(); } }); downloadDialog.show(); }
// check for the app if it exist in the phone it will lunch it otherwise, it will search for the app in google play app in the phone and to avoid any crash, if no google play app installed in the phone, it will search for the app in the google play store using the browser : public void onLunchAnotherApp() { final String appPackageName = getApplicationContext().getPackageName(); Intent intent = getPackageManager().getLaunchIntentForPackage(appPackageName); if (intent != null) { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } else { onGoToAnotherInAppStore(intent, appPackageName); } } public void onGoToAnotherInAppStore(Intent intent, String appPackageName) { try { intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("market://details?id=" + appPackageName)); startActivity(intent); } catch (android.content.ActivityNotFoundException anfe) { intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)); startActivity(intent); } }
如果您知道安装的软件包所反应的数据和操作,则只需在启动之前将这些信息添加到您的意图实例中即可。
如果您有权访问其他应用程序的AndroidManifest,则可以在其中查看所有需要的信息。
// oncreate openApp();
private void openApp() { String packageName = "com.google.android.gm"; if (isAppInstalled(activity, packageName)) startActivity(getPackageManager().getLaunchIntentForPackage(packageName)); else Toast.makeText(activity, "App not installed", Toast.LENGTH_SHORT).show(); } public static boolean isAppInstalled(Activity activity, String packageName) { PackageManager pm = activity.getPackageManager(); try { pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (PackageManager.NameNotFoundException e) { } return false; }
启动新活动的步骤如下:
1.打算打包
2.如果intent为null,则将用户redirect到playstore
3.如果意图不是空的打开活动
public void launchNewActivity(Context context, String packageName) { Intent intent = null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.CUPCAKE) { intent = context.getPackageManager().getLaunchIntentForPackage(packageName); } if (intent == null) { try { intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse("market://details?id=" + packageName)); context.startActivity(intent); } catch (android.content.ActivityNotFoundException anfe) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName))); } } else { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } }