判断一个应用是否在运行,卸载应用,从一个应用启动另一个应用
1、判断集团无线城市客户端是否在启动
ActivityManager am = (ActivityManager)VersionUpdateActivity.this.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> list = am.getRunningTasks(100);
for (RunningTaskInfo info : list)
{
if (info.topActivity.getPackageName().equals("com.whty.wicity.china")
&& info.baseActivity.getPackageName().equals("com.whty.wicity.china"))
{
LogManager.showLog(TAG, "==============集团无线城市客户端已经启动=====================", LogManager.DEBUG);
isAppRunning = true;
break;
}
}
2、卸载无线城市客户端
Uri uri = Uri.parse("package:" + "com.jscity");
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
3、判断一个应用是否存在
PackageInfo packageInfo;
String packagename = "com.whty.wicity.china";
try
{
packageInfo = mContext.getPackageManager().getPackageInfo(packagename, 0);
}
catch (NameNotFoundException e)
{
packageInfo = null;
e.printStackTrace();
}
if (null == packageInfo)
{
return false;
}
else
{
return true;
}
通过一个apk去启动另一个apk
Intent mIntent = new Intent();
ComponentName comp =
new ComponentName("com.whty.wicity.china", "com.whty.wicity.china.home.WicityHomeActivity");
mIntent.setComponent(comp);
mIntent.setAction("android.intent.action.VIEW");
startActivity(mIntent);
this.finish();
应用是否安装的广播
/**
*
* 类名称:ApkInstallReceiver
* 作者: 王中伏
* 创建时间:2013-7-4
* 类描述:应用是否安装的广播
* 版权声明 : Copyright (C) 2012-2014 南京欣网
* 修改时间:上午10:24:37
*
*/
public class ApkInstallReceiver extends BroadcastReceiver
{
/**
* 日志标志TAG
*/
private static final String TAG = "ApkInstallReceiver";
/**
* 延时时间为2s
*/
private static int delayMillis = 2000;
/**
* handler
*/
Handler handler = new Handler();
@Override
public void onReceive(final Context context, Intent intent)
{
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction()))
{
Log.e("sinner", intent.getDataString());
if (intent.getDataString().equals("package:com.whty.wicity.china"))
{
handler.postDelayed(new Runnable()
{
@Override
public void run()
{
uninstallWcityApk(context);
}
}, delayMillis);
}
}
}
/**
*
* 方法名称:uninstallWcityApk
* 作者:王中伏
* 方法描述:卸载省无线城市客户端
* 输入参数:@param context
* 返回类型:void
* 备注:
*/
private void uninstallWcityApk(Context context)
{
Uri uri = Uri.parse("package:" + "com.jscity");
Intent intent = new Intent(Intent.ACTION_DELETE, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
LogManager.showLog(TAG, "========卸载省无线城市客户端=============", LogManager.ERROR);
}
}
在AndroidManifest.xml
<receiver android:name="com.jscity.logic.ApkInstallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
自己可以参考无线智慧客户器最后下线的应用。