Android中如何像 360 一样优雅的杀死后台Service而不启动

##一、已知的 kill 后台应用程序的方法

  • android.os.Process.killProcess(pid);
  • activityManager.killBackgroundProcesses(pkgName);
  • kill -9 pid

这三种方法都可以“杀死”后台应用程序,但是都会自启动,对于前面两种直接使用Android的API就行了,所以就不多介绍了。这里稍微介绍一下 最后一个方法的用法: kill -9 pid ;

方法: kill -9 pid

这其实是一条shell 命令,我们知道Android的底层是Linux系统,所以在Android上可以使用所有的Linux终端命令。那么如何结合在代码中呢,贴一段代码

<!-- lang: java -->
private void killProcess(String pid) {
	Process sh = null;
	DataOutputStream os = null;
	try {
		sh = Runtime.getRuntime().exec("su");
		os = new DataOutputStream(sh.getOutputStream());
		final String Command = "kill -9 " + pid + "\n";
		os.writeBytes(Command);
		os.flush();
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	try {
		sh.waitFor();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}

}

该方法最重要的功能就是告诉你,怎么在Android程序中执行Linux shell 命令


##二、终极方法,杀死后台service而不自启动: am (Activity Manager)命令 相信很多人对 am 命令都挺熟悉的,它是Android系统中/system/bin/目录下的一条命令。不仅可以在终端下启动一个应用程序,还可以启动Service,发送 broadcast以及Intent actionforce stop process 等等,功能非常强大。这里我们要用到一个功能就是强制停止应用程序! 对于命令 am 的介绍与用法,官网给的非常情况,请参照Android 官网: http://developer.android.com/tools/help/adb.html#am

我们用到的方法和功能是:am force-stop <PACKAGE> 下面是我们的代码举例

<!-- lang: java -->
private void forceStopAPK(String pkgName){
	Process sh = null;
	DataOutputStream os = null;
	try {
		sh = Runtime.getRuntime().exec("su");
		os = new DataOutputStream(sh.getOutputStream());
		final String Command = "am force-stop "+pkgName+ "\n";
		os.writeBytes(Command);
		os.flush();
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	try {
		sh.waitFor();
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

通过上面的这段代码,我们调用forceStopAPK方法,传递一个应用程序的包名,那么我们就可以杀死对应的Android程序,而不会自动启动。亲测:微信,淘宝,酷我,等一系列应用,杀死后不会再启动!

转载于:https://my.oschina.net/mopidick/blog/277813

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Android Service常驻后台且应用杀死后仍能发送通知栏消息,可以按照以下步骤进行: 1. 在Service通过startForeground()方法将Service设置为前台Service,同时在通知栏显示一个通知。 2. 在Service使用AlarmManager定时发送通知栏消息,即使应用被杀死也能够执行定时任务,代码示例如下: ``` AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(this, NotificationService.class); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 60 * 1000, pendingIntent); ``` 3. 创建一个NotificationService,用于接收AlarmManager发送的通知栏消息,代码示例如下: ``` public class NotificationService extends Service { private static final int NOTIFICATION_ID = 1; @Override public int onStartCommand(Intent intent, int flags, int startId) { showNotification(); return super.onStartCommand(intent, flags, startId); } private void showNotification() { NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification.Builder(this) .setContentTitle("这是通知标题") .setContentText("这是通知内容") .setSmallIcon(R.mipmap.ic_launcher) .setAutoCancel(true) .build(); notificationManager.notify(NOTIFICATION_ID, notification); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } ``` 需要注意的是,AlarmManager定时发送通知栏消息的时间间隔应该根据实际需求进行调整。同时,如果不再需要Service常驻后台,应该通过stopForeground(true)方法将其设置为普通Service,并且应该取消之前设置的定时任务,以免浪费系统资源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值