2016-01-21 189 views
0

如何从Android应用程序启动其他Android应用程序,如果我有应用程序的软件包名称?如何从Android应用程序启动其他Android应用程序?

例如:我在Android App中创建了Button。按下按钮时,Skype将启动。

  1. 是否可以在Android中执行上述操作?
  2. 当Button被点击后,是否应该使用Intent来启动其他应用程序?
  3. 它需要什么权限?

在此先感谢。

+0

参考http://stackoverflow.com/questions/3872063/launch-an-application-from-another-application-on-android – sasikumar

+0

http://stackoverflow.com/questions/3872063/launch-an-application-从另一个应用程序在Android –

+0

在这个页面的右上角,你会发现一个白色的输入字段。你知道......这是用于搜索的目的, – nax83

回答

2

因为它不是你的应用程序,正如你所说的,“Skype”。您可以在意图中使用应用程序的包ID。

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address"); 
startActivity(launchIntent); 

对于Skype的,它成为

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.skype.raider"); 
startActivity(launchIntent); 

在你的Java文件,说MainActivity.java

Button button = (Button) findViewById(R.id.button); 

    button.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 

    Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.skype.raider"); 
    startActivity(launchIntent); 
       } 
      }); 

而且在布局文件,说activity_main.xml中

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Skype →" 
     android:id="@+id/button" 
     android:textColor="@color/white"/> 
1

您可以使用下面的代码,推出的Skype或任何其他应用程序:

PackageManager packageManager = getPackageManager(); 
Intent intent = packageManager.getLaunchIntentForPackage("<skype_package_name>"); 
startActivity(intent); 
0

试试这个代码:

PackageManager pm = context.getPackageManager(); 
try { 
    if (pm.getApplicationInfo("com.your.package.name", 0) == null) { 
     // no talk, no update 
     Toast.makeText(context, "packagenot found", Toast.LENGTH_SHORT).show(); 

    } else { 
     Intent packageIntent= pm.getLaunchIntentForPackage("com.your.package.name"); 

     packageIntent.addCategory(Intent.ACTION_SENDTO); 
     packageIntent.setType("text/plain"); 
     startActivity(packageIntent); 
    } 
} catch (PackageManager.NameNotFoundException e) { 

    // no talk, no update 
    Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show(); 
} 
2

是如果你有其他的应用程序包名称

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("other app package name"); 
startActivity(LaunchIntent); 
0
final String appPackageName = "com.example"; 
         final Intent openPlayStore = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)); 
         if (hasHandlerForIntent(openPlayStore)) 
          startActivity(openPlayStore); 
         else 
          startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName))); 

private boolean hasHandlerForIntent(Intent intent) { 
     return getActivity().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY).size() > 0; 
    } 
相关问题