如果你知道另外一个程序的类名就可以这样写
intent.addCategory(Intent.CATEGORY_LAUNCHER)ComponentName cn = new ComponentName(packageName, className)
intent.setComponent(cn)
startActivity(intent)
2.如果你只知道包名不知道类名,首先获取类名
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)
}
}
然后使用1中的方法调用程序
启动另一个应用:Intent intent = new Intent()
intent.setClassName("目标应用的包名", "目标应用的目标Activity")
startActivity(intent)
后台运行就只能启动目标应用的Service,即目标应用的目标Activity是一个Service。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)