Android 从您自己的(意图)打开另一个应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2780102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open another application from your own (intent)
提问by AndersWid
I know how to update my own programs, and I know how to open programs using the a predefined Uri (for sms or email for example)
我知道如何更新我自己的程序,我知道如何使用预定义的 Uri 打开程序(例如,对于短信或电子邮件)
I need to know how I can create an Intent to open MyTracks or any other application that I don't know what intents they listen to.
我需要知道如何创建一个 Intent 来打开 MyTracks 或我不知道他们听什么意图的任何其他应用程序。
I got this info from DDMS, but I havn't been succesful in turning this to an Intent I can use. This is taken from when opening MyTracks manually.
我从 DDMS 得到了这个信息,但我没有成功地将它转化为我可以使用的 Intent。这是从手动打开 MyTracks 时获取的。
Thanks for your help
谢谢你的帮助
05-06 11:22:24.945: INFO/ActivityManager(76): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks bnds=[243,338][317,417] }
05-06 11:22:25.005: INFO/ActivityManager(76): Start proc com.google.android.maps.mytracks for activity com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks: pid=1176 uid=10063 gids={3003, 1015}
05-06 11:22:26.995: INFO/ActivityManager(76): Displayed activity com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks: 1996 ms (total 1996 ms)
回答by Christopher
I have work it like this,
我有这样的工作,
/** Open another app.
* @param context current Context, like Activity, App, or Service
* @param packageName the full package name of the app to open
* @return true if likely successful, false if unsuccessful
*/
public static boolean openApp(Context context, String packageName) {
PackageManager manager = context.getPackageManager();
try {
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
//throw new ActivityNotFoundException();
}
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
return true;
} catch (ActivityNotFoundException e) {
return false;
}
}
Example usage:
用法示例:
openApp(this, "com.google.android.maps.mytracks");
Hope it helps someone.
希望它可以帮助某人。
回答by Gaurav Vaish
Firstly, the concept of "application" in Android is slightly an extended one.
首先,Android 中“应用程序”的概念是一个稍微扩展的概念。
An application - technically a process - can have multiple activities, services, content providers and/or broadcast listeners. If at least one of them is running, the application is up and running (the process).
一个应用程序——技术上是一个进程——可以有多个活动、服务、内容提供者和/或广播侦听器。如果其中至少有一个正在运行,则应用程序已启动并正在运行(进程)。
So, what you have to identify is how do you want to "start the application".
因此,您必须确定的是您希望如何“启动应用程序”。
Ok... here's what you can try out:
好的...这是您可以尝试的内容:
- Create an intent with
action=MAIN
andcategory=LAUNCHER
- Get the
PackageManager
from the current context usingcontext.getPackageManager
packageManager.queryIntentActivity(<intent>, 0)
where intent hascategory=LAUNCHER
,action=MAIN
orpackageManager.resolveActivity(<intent>, 0)
to get the first activity with main/launcher- Get the
ActivityInfo
you're interested in - From the
ActivityInfo
, get thepackageName
andname
- Finally, create another intent with with
category=LAUNCHER
,action=MAIN
,componentName = new ComponentName(packageName, name)
andsetFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- Finally,
context.startActivity(newIntent)
- 使用
action=MAIN
和创建意图category=LAUNCHER
- 使用
PackageManager
从当前上下文中获取context.getPackageManager
packageManager.queryIntentActivity(<intent>, 0)
意图在哪里category=LAUNCHER
,action=MAIN
或者packageManager.resolveActivity(<intent>, 0)
使用主/启动器获取第一个活动- 获取
ActivityInfo
您感兴趣的 - 从
ActivityInfo
,得到packageName
和name
- 最后,创建另一个意图用
category=LAUNCHER
,action=MAIN
,componentName = new ComponentName(packageName, name)
和setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- 最后,
context.startActivity(newIntent)
回答by zawhtut
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(ComponentName.unflattenFromString("com.google.android.maps.mytracks/com.google.android.apps.mytracks.MyTracks"));
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
EDIT :
编辑 :
as suggested in comments, add one line before startActivity(intent);
按照评论中的建议,在前面添加一行 startActivity(intent);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
回答by Muzikant
If you already have the package name you wish to activate, you can use the following code which is a bit more generic:
如果您已经有了要激活的包名称,则可以使用以下更通用的代码:
PackageManager pm = context.getPackageManager();
Intent appStartIntent = pm.getLaunchIntentForPackage(appPackageName);
if (null != appStartIntent)
{
context.startActivity(appStartIntent);
}
I found that it works better for cases where the main activity was not found by the regular method of start the MAIN activity.
我发现它在启动 MAIN 活动的常规方法未找到主要活动的情况下效果更好。
回答by inversus
This is the code of my solution base on MasterGaurav solution:
这是我基于 MasterGaurav 解决方案的解决方案代码:
private void launchComponent(String packageName, String name){
Intent launch_intent = new Intent("android.intent.action.MAIN");
launch_intent.addCategory("android.intent.category.LAUNCHER");
launch_intent.setComponent(new ComponentName(packageName, name));
launch_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(launch_intent);
}
public void startApplication(String application_name){
try{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
List<ResolveInfo> resolveinfo_list = activity.getPackageManager().queryIntentActivities(intent, 0);
for(ResolveInfo info:resolveinfo_list){
if(info.activityInfo.packageName.equalsIgnoreCase(application_name)){
launchComponent(info.activityInfo.packageName, info.activityInfo.name);
break;
}
}
}
catch (ActivityNotFoundException e) {
Toast.makeText(activity.getApplicationContext(), "There was a problem loading the application: "+application_name,Toast.LENGTH_SHORT).show();
}
}
回答by peter.bartos
Using the solution from inversus, I expanded the snippet with a function, that will be called when the desired application is not installed at the moment. So it works like: Run application by package name. If not found, open Android market - Google play for this package.
使用 inversus 的解决方案,我用一个函数扩展了代码片段,当当前未安装所需的应用程序时将调用该函数。所以它的工作原理是:按包名运行应用程序。如果未找到,请打开 Android 市场 - Google Play 获取此包。
public void startApplication(String packageName)
{
try
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);
for(ResolveInfo info : resolveInfoList)
if(info.activityInfo.packageName.equalsIgnoreCase(packageName))
{
launchComponent(info.activityInfo.packageName, info.activityInfo.name);
return;
}
// No match, so application is not installed
showInMarket(packageName);
}
catch (Exception e)
{
showInMarket(packageName);
}
}
private void launchComponent(String packageName, String name)
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setComponent(new ComponentName(packageName, name));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void showInMarket(String packageName)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
And it is used like this:
它是这样使用的:
startApplication("org.teepee.bazant");
回答by Swetha
Use this :
用这个 :
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("com.package.name");
startActivity(intent);
回答by Flinbor
Open application if it is exist, or open Play Store application for install it:
打开应用程序(如果存在),或打开 Play Store 应用程序进行安装:
private void open() {
openApplication(getActivity(), "com.app.package.here");
}
public void openApplication(Context context, String packageN) {
Intent i = context.getPackageManager().getLaunchIntentForPackage(packageN);
if (i != null) {
i.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(i);
} else {
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageN)));
}
catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + packageN)));
}
}
}
回答by KCN
To Start another application activity from my application Activity. It is working fine for me.
从我的应用程序活动启动另一个应用程序活动。它对我来说很好。
Below code will work if the another application already installed in your phone otherwise it is not possible to redirect form one app to another app.So make sure your app launched or not
如果您的手机中已经安装了另一个应用程序,则下面的代码将起作用,否则无法将一个应用程序重定向到另一个应用程序。因此请确保您的应用程序启动与否
Intent intent = new Intent();
intent.setClassName("com.xyz.myapplication", "com.xyz.myapplication.SplashScreenActivity");
startActivity(intent);
回答by Anil Kongovi
// This works on Android Lollipop 5.0.2
// 这适用于 Android Lollipop 5.0.2
public static boolean launchApp(Context context, String packageName) {
final PackageManager manager = context.getPackageManager();
final Intent appLauncherIntent = new Intent(Intent.ACTION_MAIN);
appLauncherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = manager.queryIntentActivities(appLauncherIntent, 0);
if ((null != resolveInfos) && (!resolveInfos.isEmpty())) {
for (ResolveInfo rInfo : resolveInfos) {
String className = rInfo.activityInfo.name.trim();
String targetPackageName = rInfo.activityInfo.packageName.trim();
Log.d("AppsLauncher", "Class Name = " + className + " Target Package Name = " + targetPackageName + " Package Name = " + packageName);
if (packageName.trim().equals(targetPackageName)) {
Intent intent = new Intent();
intent.setClassName(targetPackageName, className);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Log.d("AppsLauncher", "Launching Package '" + packageName + "' with Activity '" + className + "'");
return true;
}
}
}
return false;
}