Android编程防止进程被第三方软件杀死的方法_Android

本文实例讲述了Android编程防止进程被第三方软件杀死的方法。分享给大家供大家参考,具体如下:

项目测试的时候发现,按home键回到桌面,再用360清理内存,软件被结束,再次进入的时候报错,看了下log,以为是有的地方没有控制好,但是又不知道360结束的是什么(这个现在还没弄明白)。使用小米系统的进程管理优化内存就不报错。

后来想到用Service防止软件被kill掉,查了下资料,发现google 管方就有,ForegroundService 前台服务,让服务一直以前台任务的方式运行,可以在service 的oncreate来实现前台服务, 通过这个方法必须发送一个通知栏,让用户知道服务在运行。

Notification notification = new Notification(R.drawable.icon, "服务开启", System.currentTimeMillis());
notification.flags|= Notification.FLAG_NO_CLEAR;
notification.flags=Notification.FLAG_ONGOING_EVENT;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "service", "防止服务被任务管理器所杀", pendingIntent);
startForeground(ONGOING_NOTIFICATION, notification);

这样就能保持service 运行,可是通知栏不能清除 ,一清除就会被kill。
后来一次 做自定义Notification的时候,通知栏没有显示通知,查看后发现 service 也没被kill 。所以就进一步去研究了下 最后发现 只用两行代码就能保持服务不会被kill,并且不会有通知栏通知代码如下:

Notification notification = new Notification();
startForeground(1, notification);

完整代码如下:

public class TestService extends Service {
 private static final Class[] mStartForegroundSignature = new Class[] {
   int.class, Notification.class };
 private static final Class[] mStopForegroundSignature = new Class[] { boolean.class };
 private NotificationManager mNM;
 private Method mStartForeground;
 private Method mStopForeground;
 private Object[] mStartForegroundArgs = new Object[2];
 private Object[] mStopForegroundArgs = new Object[1];
 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }
 @Override
 public void onCreate() {
  super.onCreate();
  mNM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  try {
   mStartForeground = TestService.class.getMethod("startForeground",
     mStartForegroundSignature);
   mStopForeground = TestService.class.getMethod("stopForeground",
     mStopForegroundSignature);
  } catch (NoSuchMethodException e) {
   mStartForeground = mStopForeground = null;
  }
  // 我们并不需要为 notification.flags 设置 FLAG_ONGOING_EVENT,因为
  // 前台服务的 notification.flags 总是默认包含了那个标志位
  Notification notification =new Notification();
  // 注意使用 startForeground ,id 为 0 将不会显示 notification
  startForegroundCompat(1, notification);
 }
 @Override
 public void onDestroy() {
  super.onDestroy();
  stopForegroundCompat(1);
 }
 // 以兼容性方式开始前台服务
 private void startForegroundCompat(int id, Notification n) {
  if (mStartForeground != null) {
   mStartForegroundArgs[0] = id;
   mStartForegroundArgs[1] = n;
   try {
    mStartForeground.invoke(this, mStartForegroundArgs);
   } catch (IllegalArgumentException e) {
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    e.printStackTrace();
   } catch (InvocationTargetException e) {
    e.printStackTrace();
   }
   return;
  }
  mNM.notify(id, n);
 }
 // 以兼容性方式停止前台服务
 private void stopForegroundCompat(int id) {
  if (mStopForeground != null) {
   mStopForegroundArgs[0] = Boolean.TRUE;
   try {
    mStopForeground.invoke(this, mStopForegroundArgs);
   } catch (IllegalArgumentException e) {
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    e.printStackTrace();
   } catch (InvocationTargetException e) {
    e.printStackTrace();
   }
   return;
  }
  // 在 setForeground 之前调用 cancel,因为我们有可能在取消前台服务之后
  // 的那一瞬间被kill掉。这个时候 notification 便永远不会从通知一栏移除
  mNM.cancel(id);
 }
}

经测试,360手机助手,腾讯手机管家都不能kill这个service,但是手动结束后,再次打开发现音频还在播放(跟音频有关的客户端),感觉有点小别扭

希望本文所述对大家Android程序设计有所帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, 进程
第三方软件
linux 防止进程被杀死、进程防止被杀死、linux 杀死进程、linux强制杀死进程、杀死进程,以便于您获取更多的相关知识。

时间: 2024-07-31 13:14:36

Android编程防止进程被第三方软件杀死的方法_Android的相关文章

Android编程实现系统重启与关机的方法_Android

本文实例讲述了Android编程实现系统重启与关机的方法.分享给大家供大家参考,具体如下: 最近在做个东西,巧合碰到了sharedUserId 的问题,所以收集了一些资料,存存档备份. 安装在设备中的每一个apk文件,Android 给每个 APK 进程分配一个单独的用户空间,其 manifest 中的 userid 就是对应一个 Linux 用户都会被分配到一个属于自己的统一的 Linux 用户 ID,并且为它创建一个沙箱,以防止影响其他应用程序(或者其他应用程序影响它). 用户 ID 在应用

Android编程判断网络连接是否可用的方法_Android

本文实例讲述了Android编程判断网络连接是否可用的方法.分享给大家供大家参考,具体如下: 为了提高用户体验,我们在开发 android 应用的过程需要联网获取数据的时候我们首先要做的一步就是: 1.判断当前手机是否打开了网络 2.打开了网络是否可以上网 然后再去执行联网逻辑,避免没联网做不必要的工作! 通常情况下,我们是这样判断的 public static boolean isNetAvailable(Context context) { ConnectivityManager conne

Android编程之退出整个应用程序的方法_Android

本文实例讲述了Android编程之退出整个应用程序的方法.分享给大家供大家参考,具体如下: 我们在写android应用程序时,经常会遇到想退出当前Acitivity,或者直接退出应用程序.我之前的一般操作是按返回键,或者直接按home键直接返回,其实这两种操作都没有关闭当前应用程序,没有释放系统资源.有时跳转的activity较多时,还需要多次按返回键,这样感觉一点都不爽. 后面添加了一个菜单返回功能键,这个方法也只能用system.exit(0)来关闭当前活动的Activity,代码如下: p

Android编程重写ViewGroup实现卡片布局的方法_Android

本文实例讲述了Android编程重写ViewGroup实现卡片布局的方法.分享给大家供大家参考,具体如下: 实现效果如图: 实现思路 1. 重写onMeasure(int widthMeasureSpec, int heightMeasureSpec)设置每个子View的大小 2. 重写onLayout(boolean changed, int l, int t, int r, int b) 设置每个子View的位置 第一步:新建FlowLayout继承ViewGroup package com

Android编程之判断SD卡状态的方法_Android

本文实例讲述了Android编程之判断SD卡状态的方法.分享给大家供大家参考,具体如下: 首先我们要在AndroidManifest.xml中增加SD卡访问权限: <!-- 在SDCard 的挂载权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission

Android编程实现向桌面添加快捷方式的方法_Android

本文实例讲述了Android编程实现向桌面添加快捷方式的方法.分享给大家供大家参考,具体如下: 有时候为了使用方便,需要在桌面上添加快捷方式,下面是两种添加快捷方式的方法: 方法1: void setshortCut() { Intent addShortcut = new Intent(); // 设置快捷方式的名字 addShortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式练习"); // 构建快捷方式中专门的图标 Parcelab

Android编程单击图片实现切换效果的方法_Android

本文实例讲述了Android编程单击图片实现切换效果的方法.分享给大家供大家参考,具体如下: 新建一个Android项目,命名为FrameLayout 此实例主要操作src文件夹下的MainActivity.Java类文件和res/layout下的activity_main.xml布局文件 1.布局主页面代码activity_main.xml↓ <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android&qu

Android编程实现只显示图片一部分的方法_Android

本文实例讲述了Android编程实现只显示图片一部分的方法.分享给大家供大家参考,具体如下: 在Android应用程序中加载一张图片,然后把它显示出来这是一件非常容易的事情,那怎么才能显示一张图片的一小部分呢,一种做法是把图片ps一下,要显示的那部分单独存成一张图片,然后程序中加载它,并把它显示出来.但这样会增加程序的图片量.对一张完整的图,用程序去切割你想要的那部分也是很简单的. 下面实现的一个程序,是加载了一张图片,然后经过变换让图片填充到手机的整个屏幕,然后在屏幕的中间显示图片中间的100

Android编程实现自定义系统菜单背景的方法_Android

本文实例讲述了Android编程实现自定义系统菜单背景的方法.分享给大家供大家参考,具体如下: 不多说,上图,见代码. package lab.sodino.menutest; import android.content.Context; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.AttributeSet; import androi