ok,实现此功能很简单,就直接上代码吧!!
下面的代码直接实现了一个kill指定meta-data进程的方法及根据包名运行一个activity的方法
package com.skyworth.dtv;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.util.Log;
public class DtvAppHelper {
public static final String META_SOURCE_MODE = "androidtv.meidasource.mode";
public static final String META_SOURCE_MODE_DTV = "dtv";
static final String TAG = "DtvAppCloseHelper";
static Method forceStopPackage = null;
public static void shutdownDtvApps(Context ctx) {
PackageManager pm = ctx.getPackageManager();
ActivityManager am = (ActivityManager) ctx
.getSystemService(Context.ACTIVITY_SERVICE);
List<String> list = getRunningDtvApps(am, pm);
for (int i = 0; i < list.size(); i++) {
Log.i(TAG, "will stop package name : " + list.get(i));
forceStopPackage(am, list.get(i));
}
}
private static void forceStopPackage(ActivityManager am, String pkgname) {
try {
if (forceStopPackage == null) {
forceStopPackage = am.getClass().getDeclaredMethod(
"forceStopPackage", String.class);
}
if (forceStopPackage != null) {
forceStopPackage.setAccessible(true);
forceStopPackage.invoke(am, pkgname);
}
} catch (Exception e) {
Log.e(TAG, "forceStopPackage error:" + e);
}
}
private static List<String> getRunningDtvApps(ActivityManager am,
PackageManager pm) {
List<String> plist = new ArrayList<String>();
List<ActivityManager.RunningAppProcessInfo> applist = am
.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : applist) {
String[] pkgNameList = appProcess.pkgList; // 得到包名列表
int pid = appProcess.pid;
for (int i = 0; i < pkgNameList.length; i++) {
String pkgName = pkgNameList[i];
try {
ApplicationInfo ai = pm.getApplicationInfo(pkgName,
PackageManager.GET_META_DATA);
if (ai.metaData != null) {
String value = ai.metaData.getString(META_SOURCE_MODE);
if (META_SOURCE_MODE_DTV.equals(value)) {
Log.i(TAG, "packageName:" + pkgName
+ " process pid:" + pid);
plist.add(pkgName);
}
}
} catch (NameNotFoundException e) {
Log.e(TAG, "get meta data is empty");
}
}
}
return plist;
}
public static void runDtvApps(Context ctx, String packageName) {
Log.i(TAG, "will start packageName:" + packageName);
PackageInfo pi = null;
try {
pi = ctx.getPackageManager().getPackageInfo(packageName, 0);
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
resolveIntent.setPackage(pi.packageName);
PackageManager pManager = ctx.getPackageManager();
List<ResolveInfo> apps = pManager.queryIntentActivities(
resolveIntent, 0);
ResolveInfo ri = apps.iterator().next();
if (ri != null) {
packageName = ri.activityInfo.packageName;
String className = ri.activityInfo.name;
Log.i(TAG, "packageName: " + packageName + " className:"
+ className);
Intent intent = new Intent(Intent.ACTION_MAIN);
ComponentName cn = new ComponentName(packageName, className);
intent.setComponent(cn);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
}
} catch (Exception e) {
Log.e(TAG, "start activity failed " + e);
}
}
}
下面具体列一下不同父元素,读取的方法情况(为了对比方便写在一个函数中了,可以根据情况拆分):
void test(Context ctx, String packageName, String className) {
/* 在application的应用 */
try {
ApplicationInfo appInfo = ctx.getPackageManager()
.getApplicationInfo(packageName,
PackageManager.GET_META_DATA);
String msg = appInfo.metaData.getString("myMsg");
Log.i(TAG, "message :" + msg);
//TODO
} catch (Exception e) {
Log.e(TAG, "getApplicationInfo error:" + e);
}
/* 在Activity的应用 */
ComponentName cn = new ComponentName(packageName, className);
try {
ActivityInfo info = ctx.getPackageManager().getActivityInfo(cn,
PackageManager.GET_META_DATA);
String msg = info.metaData.getString("myMsg");
Log.i(TAG, "message :" + msg);
//TODO
} catch (Exception e) {
Log.e(TAG, "getActivityInfo error:" + e);
}
/* 在service的应用 */
try {
ServiceInfo info = ctx.getPackageManager().getServiceInfo(cn,
PackageManager.GET_META_DATA);
String msg = info.metaData.getString("myMsg");
Log.i(TAG, "message :" + msg);
// TODO
} catch (Exception e) {
Log.e(TAG, "getServiceInfo error:" + e);
}
/* 在receiver的应用 */
try {
ComponentName crn = new ComponentName(ctx, className);
ActivityInfo info = ctx.getPackageManager().getReceiverInfo(crn,
PackageManager.GET_META_DATA);
String msg = info.metaData.getString("myMsg");
Log.i(TAG, "message :" + msg);
// TODO
} catch (Exception e) {
Log.e(TAG, "getReceiverInfo error:" + e);
}
}