Activity中启动另一个应用的另类方法,无需类名
2011-11-25 23:24
405 查看
Activity中启动另一个应用的另类方法,无需类名
由陈瑞轩发布于2011-10-22,Saturday,13:50 [android开发]
PackageManager类中有一个
public abstract Intent getLaunchIntentForPackage (String packageName) 方法,源码如下:
/**
* Return a "good" intent to launch a front-door activity in a package,
* for use for example to implement an "open" button when browsing through
* packages. The current implementation will look first for a main
* activity in the category {@link Intent#CATEGORY_INFO}, next for a
* main activity in the category {@link Intent#CATEGORY_LAUNCHER}, or return
* null if neither are found.
*
* <p>Throws {@link NameNotFoundException} if a package with the given
* name can not be found on the system.
*
* @param packageName The name of the package to inspect.
*
* @return Returns either a fully-qualified Intent that can be used to
* launch the main activity in the package, or null if the package does
* not contain such an activity.
*/
public abstract Intent getLaunchIntentForPackage(String packageName);
Android 开发有时需要在一个应用中启动另一个应用,比如Launcher加载所有的已安装的程序的列表,当点击图标时可以启动另一个应用。
一般我们知道了另一个应用的包名和MainActivity的名字之后便可以直接通过如下代码来启动:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
但是更多的时候,我们一般都不知道应用程序的启动Activity的类名,而只知道包名,我们可以通过ResolveInfo类来取得启动Acitivty的类名。
下面是实现代码:
private void openApp(String packageName) {
PackageInfo pi = getPackageManager().getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
String packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
}
}
本文来陈瑞轩的博客,转载请标明出处:http://www.chenruixuan.com/?post=708
由陈瑞轩发布于2011-10-22,Saturday,13:50 [android开发]
PackageManager类中有一个
public abstract Intent getLaunchIntentForPackage (String packageName) 方法,源码如下:
/**
* Return a "good" intent to launch a front-door activity in a package,
* for use for example to implement an "open" button when browsing through
* packages. The current implementation will look first for a main
* activity in the category {@link Intent#CATEGORY_INFO}, next for a
* main activity in the category {@link Intent#CATEGORY_LAUNCHER}, or return
* null if neither are found.
*
* <p>Throws {@link NameNotFoundException} if a package with the given
* name can not be found on the system.
*
* @param packageName The name of the package to inspect.
*
* @return Returns either a fully-qualified Intent that can be used to
* launch the main activity in the package, or null if the package does
* not contain such an activity.
*/
public abstract Intent getLaunchIntentForPackage(String packageName);
Android 开发有时需要在一个应用中启动另一个应用,比如Launcher加载所有的已安装的程序的列表,当点击图标时可以启动另一个应用。
一般我们知道了另一个应用的包名和MainActivity的名字之后便可以直接通过如下代码来启动:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
但是更多的时候,我们一般都不知道应用程序的启动Activity的类名,而只知道包名,我们可以通过ResolveInfo类来取得启动Acitivty的类名。
下面是实现代码:
private void openApp(String packageName) {
PackageInfo pi = getPackageManager().getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resolveIntent.setPackage(pi.packageName);
List<ResolveInfo> apps = pm.queryIntentActivities(resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null ) {
String packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
}
}
本文来陈瑞轩的博客,转载请标明出处:http://www.chenruixuan.com/?post=708
相关文章推荐
- Android Activity中启动另一应用程序的方法,无需得到类名
- 从一个A应用发广播,在另一个应用B的广播接收者的onReceive()方法中接收此广播并启动这个应用里的activity
- 23、Android Activity中启动另一应用程序的方法,无需得到类名
- Android 如何在Activity中启动另一应用程序的方法,无需得到类名
- Android Activity中启动另一应用程序的方法,无需得到类名
- Android Activity中启动另一应用程序的方法,无需得到类名
- Android Activity中启动另一应用程序的方法,无需得到类名
- Android Activity中启动另一应用程序的方法,无需得到类名
- Android--Activity中启动另一应用程序的方法,无需得到类名
- Android Activity中启动另一应用程序的方法,无需得到类名
- Android 如何在Activity中启动另一应用程序的方法,无需得到类名
- Android Activity中启动另一应用程序的方法,无需得到类名
- Android Activity中启动另一应用程序的方法,无需得到类名
- Android在一个应用中启动另一个应用的Activity时失败的解决办法
- (大总结)从寻找fragment静态导入activity总是失败的解决方法中了解android应用的系统启动过程
- startActivity方法启动一个新的activity时怎么让新的activity在另一个新的 task中
- Android在一个应用中启动另一个应用的Activity的办法
- android应用开发实践三----Atcity 通过Intent 来启动另一个Activity来实现简易拨号
- Android官方开发文档Training系列课程中文版:构建第一款安卓应用之启动另一个Activity
- Android Activity中启动另一应用程序的方法,无需类名