Android 如何以编程方式启动特定应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3343432/
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
How do I programmatically launch a specific application?
提问by mbwasi
I want to launch a specif application.
我想启动一个特定的应用程序。
I know how to do Intents but I want to avoid the selection menu if there are multiple apps that can handle the intent, I want to go directly to a particular app. Hope this makes sense.
我知道如何做 Intent,但如果有多个应用程序可以处理 Intent,我想避免选择菜单,我想直接转到特定应用程序。希望这是有道理的。
采纳答案by stealthcopter
You use the package name / class directly, for example to create a new intent to call the twidroid program you'd use the followinglink text:
您可以直接使用包名称/类,例如创建一个新意图来调用您将使用以下链接文本的 twidroid 程序:
Intent intent = new Intent("com.twidroid.SendTweet");
You'd probably want to put a try/catch around for a ActivityNotFoundException for when the application is not installed.
您可能希望在未安装应用程序时为 ActivityNotFoundException 设置一个 try/catch。
回答by Carni
You should use the function of the package manager.
您应该使用包管理器的功能。
Context ctx=this; // or you can replace **'this'** with your **ActivityName.this**
try {
Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.twidroid.SendTweet");
ctx.startActivity(i);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
}
回答by Bharat Dodeja
Intent intent = new Intent();
intent.setClassName("package.name", "package.name.LauncherActivityName");
startActivityForResult(intent,REQUEST_CODE);
回答by Jorgesys
I use:
我用:
try {
Intent intent = new Intent();
intent.setClassName("package.name", "<your_package_name>");
startActivity(intent);
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
But like Cami suggested this will work too:
但就像 Cami 建议的那样,这也行得通:
try {
Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.twidroid.SendTweet");
ctx.startActivity(i);
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
回答by Ahamadullah Saikat
in oncreatemethod call => openApp();method
在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;
}
回答by Gangnus
The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too. - don't forget!
您正在调用的活动不仅应该出现在它自己的包的清单中,而且还应该出现在 CALLING 包的清单中。- 别忘了!
回答by Devendra Vaja
Say you want to open Wonder Clock from your app
假设您想从您的应用程序中打开 Wonder Clock
URL: https://play.google.com/store/apps/details?id=ganesha.diva.app.wonderclock.free&hl=en_US
网址:https: //play.google.com/store/apps/details?id=ganesha.diva.app.wonderclock.free&hl=en_US
id field/package name is of our interest= ganesha.diva.app.wonderclock.free
id 字段/包名称是我们感兴趣的= ganesha.diva.app.wonderclock.free
The Package Manager keeps track of the apps installed on the device and maintains the list of the packages. So anything related to installed apps on the device, the Package manager should be consulted.
包管理器跟踪设备上安装的应用程序并维护包列表。所以任何与设备上安装的应用程序相关的东西,都应该咨询包管理器。
The bundle id or package name is unique across globe so it can be used to check the existence of the package/app on the device.
包 ID 或包名称在全球范围内是唯一的,因此可用于检查设备上包/应用程序的存在。
To Open the Wonder Clock
from your app
Wonder Clock
从您的应用程序打开
Ask Package Manager, for the launcher activity of the given package name
Intent intent = context.getPackageManager().getLaunchIntentForPackage("ganesha.diva.app.wonderclock.free");
- Check if launcher activity exists or not
if(intent != null) continue...
else app is not installed on the device take user to the URL - Add the required flags to the intent
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- start the activity
startActivity(intent);
向包管理器询问给定包名称的启动器活动
Intent intent = context.getPackageManager().getLaunchIntentForPackage("ganesha.diva.app.wonderclock.free");
- 检查启动器活动是否存在,
if(intent != null) continue...
否则设备上未安装应用程序将用户带到URL - 将所需的标志添加到意图
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- 开始活动
startActivity(intent);
回答by Waqar UlHaq
I've resolved issue by
我已经解决了问题
String packageName = "Your package name";
Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);
if(intent == null) {
try {
// if play store installed, open play store, else open browser
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
} catch (Exception e) {
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + packageName));
}
}
startActivity(intent);
回答by Benny
Launch app using Intentwith ComponentName
使用Intent和ComponentName启动应用程序
ComponentName cName = new ComponentName("packageName","packagename.yourMainActivity");
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(cName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
回答by BOT Panzer
What i do to open apps is this
我要打开应用程序是这样的
private void _LaunchApp(final String _Pack) {
Intent launchi = new Intent(Intent.ACTION_VIEW);
launchi.setData(Uri.parse("android-app://".concat(_Pack)));
startActivity(launchi);
}
and the usage
和用法
_LaunchApp("your.package.here");