欢迎来到 Android人的问与答 ,在这里提问或帮助他人解答。 Question2Answer的1.6.x和1.7.x版本的中文翻译包我修改更新:

Question2answer

如何使用中文语言包

Q2A 1.6.x - Chinese Simplified (简体中文)

Q2A 1.7.x - Chinese Simplified (简体中文)


国内服务器推荐 阿里云服务器
国外服务器推荐 DigitalOcean
VPN推荐 正版Green VPN
阿里云推荐码:ldvq50

Android中如何启动别的应用

0 喜欢 0 不喜欢
245 浏览

Intent i;
PackageManager manager = getPackageManager();
try {
    i = manager.getLaunchIntentForPackage("app package name");
    if (i == null)
        throw new PackageManager.NameNotFoundException();
    i.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(i);
} catch (PackageManager.NameNotFoundException e) {

}

 
最新提问 12月 31, 2014 分类:Android 小技巧 | 用户: forlong401 (7,050 分)

1个回答

0 喜欢 0 不喜欢

Firstly, the concept of "application" in Android is slightly an extended one.

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 and category=LAUNCHER
Get the PackageManager from the current context using context.getPackageManager
packageManager.queryIntentActivity(<intent>, 0) where intent has category=LAUNCHER, action=MAIN or packageManager.resolveActivity(<intent>, 0) to get the first activity with main/launcher
Get theActivityInfo you're interested in
From the ActivityInfo, get the packageName and name
Finally, create another intent with with category=LAUNCHER, action=MAIN, componentName = new ComponentName(packageName, name) and setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Finally, context.startActivity(newIntent)

 
最新回答 12月 31, 2014 用户: forlong401 (7,050 分)
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);
        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));
        startActivity(intent);
    }
}
...