public void openApp( String packageName) { // TODO 把应用杀掉然后再启动,保证进入的是第一个页面 PackageInfo pi = null; try { pi = getApplicationContext().getPackageManager().getPackageInfo(packageName, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); return; } Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER); resolveIntent.setPackage(pi.packageName); PackageManager pManager = getApplicationContext().getPackageManager(); List<ResolveInfo> apps = pManager.queryIntentActivities(resolveIntent, 0); ResolveInfo ri = apps.iterator().next(); if (ri != null) { String startappName = ri.activityInfo.packageName; String className = ri.activityInfo.name; System.out.println( "启动的activity是: " + startappName+":"+className); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ComponentName cn = new ComponentName(startappName, className); intent.setComponent(cn); getApplicationContext().startActivity(intent); } }
通过包名唤起应用
最新推荐文章于 2024-06-05 11:22:26 发布