调用apk方法一:知道包名类名
/**
* @exception :启动apk
* @param :String 包名
* @param :String 类名
* **/
public void startActivity(String packageName,String className){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
startActivity(intent);
}
调用apk方法二:只知道知道包名
/**
* @exception :启动apk
* @param :String 包名
* **/
public void startAPP(String appPackageName){
try{
Intent intent = this.getPackageManager().getLaunchIntentForPackage(appPackageName);
startActivity(intent);
}catch(Exception e){
Toast.makeText(this, "没有安装", Toast.LENGTH_LONG).show();
}
}
同一个apk内部启动另一个activity:
MainActivity:当前所在activity
SetActivity:要启动的activity
Intent intent = new Intent(MainActivity.this,SetActivity.class);
startActivity(intent);
调用apk方法三:今天遇到个坑,如果app启动类名不是MainActivity(现在没时间找原因,总之方法二启动不了了)可能上面的方法就不行了,那就试试下面这个方法吧
/**
* 根据包名启动app 主类名不是MainActivity的情况
* */
public static void RunApp(String packageName,Context mContext) {
PackageInfo pi;
try {
pi = mContext.getPackageManager().getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.setPackage(pi.packageName);
PackageManager pManager = mContext.getPackageManager();
List apps = pManager.queryIntentActivities(
resolveIntent, 0);
ResolveInfo ri = (ResolveInfo) apps.iterator().next();
if (ri != null) {
packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
mContext.startActivity(intent);
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}