Notification 配置记录与防止APP被杀总结

参考文章

仿微信通知栏
Android基础知识(二十):Notification、提醒式通知(横幅)踩坑与通知界面设置跳转
PendingIntent.getActivity的使用

Android平台App进程优先级
Android 程序后台运行和锁屏运行

重要记录点

1、setSmallIcon、setLargeIcon 必须设置。设置小图标的时候,小图标是需要透明背景,实体区域用白色的就可以了,大小64 * 64吧,这个可以修改
2、华为目前是不可以通过代码设置横幅的,想要显示横幅出来,需要自己手动的去通知栏那打开。微信,QQ有钱没办法,天生就可以默认横幅,在notification设置震动也无效,估计所有的开发商都有修改过notification吧
3、可以在线程创建notification
4、开启横幅方式(修改过底层的除开),将通知等级设置最高

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//android 8.0
      val channel = NotificationChannel(ConversionChannelId, ConversionChannelName, IMPORTANCE_HIGH)
   } else {
      notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH)
  }
importance、priority都必须设置到高值

5、设置通知栏不消失的方式

        var notificationBuilder: NotificationCompat.Builder? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//android 8.0
            notificationBuilder = NotificationCompat.Builder(CommonApplication.getContext(), SingleAppChannelId)
        } else {
            notificationBuilder = NotificationCompat.Builder(CommonApplication.getContext())
        }
        notificationBuilder.let {
            val notification = notificationBuilder.build();
            notification.flags = notification.flags or Notification.FLAG_ONGOING_EVENT//不消失的通知栏
        }

6、如果我不开启前台service,只是将notification设置为不消失的通知栏,按下home键后,主oom_adj的值为11,子进程为15。主与子易被杀死

只开启notification_不消失.jpg

7、主进程开启前台Service,按下home键后,oom_adj的值为3,如果此时有子进程,子进程的oom_adj的值为15。子易被杀死


oom_adj_比较.jpg

8、目前学习到尽量不被杀的办法

1、主进程开启前台service
2、service里面监听开屏、息屏广播。不管开屏、息屏都循环播放无声音乐。目前oppo是无法监听到开屏息屏的通知的,所以需要单独处理,如果是oppo,vivo,直接播放无声音乐好了
3、定时的调用startForeground刷新notification,当然要保证notification的id都相同
4、开启service同时获取PowerManager,并且调用acquire()方法,避免息屏的时候CPU直接睡眠,不执行代码

当然了,你退出APP之后,以上系列的所有操作,都需要进行 一 一 关闭
知识点记录

1、PendingIntent.getActivity知识点

PendingIntent.getActivity(Context context, int requestCode,  intent, int flags)
第一个参数连接上下文的context
第二个参数是对PendingIntent的描述,请求值不同Intent就不同
第三个参数是一个Intent对象,包含跳转目标
第四个参数有4种状态

FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。

FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。

FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。

FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。

notification详细代码
 /**
     * @date 创建时间: 2021/2/1
     * @auther gaoxiaoxiong
     * @description 显示信息,这个需要在线程创建,因为头像的获取是线程
     **/
    fun showChatNotification(context: Context) {
        // 创建一个NotificationManager的引用
        val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
        var notificationBuilder: NotificationCompat.Builder? = null
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//android 8.0
            notificationBuilder = NotificationCompat.Builder(CommonApplication.getContext(), ConversionChannelId)
            val channel = NotificationChannel(ConversionChannelId, ConversionChannelName, IMPORTANCE_HIGH)
            channel.description = ConversionChannelName;
            mNotificationManager.createNotificationChannel(channel);
        } else {
            notificationBuilder = NotificationCompat.Builder(CommonApplication.getContext())
            notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH)
        }

        val intent = Intent(context,MainActivity::class.java);
        intent.putExtra(MainActivity.ID,10);

        notificationBuilder.let {
            it.setWhen(System.currentTimeMillis());
            it.setSmallIcon(R.drawable.icon_sml_notification)//设置小图标,设置后在状态栏部分显示,注意,小图标是需要透明背景的,实体会话部分用白色的就可以了。大小64 * 64吧,这个可以修改
            var userHeaderBitmap = GetImageInputStream(daoChatRecordModel.visitorAvatar);//获取网络图片
            if (userHeaderBitmap == null) {
                userHeaderBitmap = BitmapFactory.decodeResource(CommonApplication.getContext().getResources(), R.mipmap.icon_logo)
            }
            it.setLargeIcon(userHeaderBitmap);//设置大图标
            it.setContentTitle(StringUtils.isEmptyValue(daoChatRecordModel.visitorVame));//设置标题
            it.setContentText(ResourcesUtils.getResourcesString(context, R.string.conversation_shopmessage))//设置文本信息
            //设置当点击通知之后,将要跳转的Activity
            it.setContentIntent(PendingIntent.getActivity(context, 15, intent, PendingIntent.FLAG_UPDATE_CURRENT))
            //弹出通知,悬浮在通知栏
            it.setFullScreenIntent(PendingIntent.getActivity(context, 15, intent, PendingIntent.FLAG_UPDATE_CURRENT), true)
            //设置通知在第一次到达时在状态栏中显示的文本
            it.setTicker(StringUtils.isEmptyValue(daoChatRecordModel.content))
            it.setAutoCancel(true)//可以取消

            it.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)//显示通知的全部内容
            it.setPriority(NotificationManager.IMPORTANCE_MAX)// 当发出此类型的通知时,通知会以悬挂的方法显示在屏幕上
            it.setSound(defaultNoticeVoice);//声音
            it.setVibrate(longArrayOf(0, 500, 1000, 1500));//震动

            val notification = notificationBuilder.build();
            it.setPublicVersion(notification);//安全锁屏下的通知

            mNotificationManager.notify(11, notification)

        }
    }



 /**
     * 获取网络图片
     *
     * @param imageurl 图片网络地址
     * @return Bitmap 返回位图
     */
    private fun GetImageInputStream(imageurl: String): Bitmap? {
        if (StringUtils.isEmpty(imageurl)) {
            return null;
        }
        val url: URL
        var connection: HttpURLConnection? = null
        var bitmap: Bitmap? = null
        try {
            url = URL(imageurl)
            connection = url.openConnection() as HttpURLConnection
            connection.setConnectTimeout(2 * 1000) //超时设置
            connection.setDoInput(true)
            connection.setUseCaches(false) //设置不使用缓存
            val inputStream: InputStream = connection.getInputStream()
            bitmap = BitmapFactory.decodeStream(inputStream)
            inputStream.close()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return bitmap
    }
 /**
     * @date 创建时间:2021/2/3 0003
     * @auther gaoxiaoxiong
     * @Descriptiion 获取默认的提示音
     **/
    public Uri getDefaultNoticeVoice() {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (notification == null) {//获取本地的
            return Uri.parse("android.resource://" + CommonApplication.getContext().getPackageName() + "/" + R.raw.default_notice_voice);
        } else {
            return notification;
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 让app 的service常驻其实是很流氓的做法,但是需求摆在那里。。。 但是要清除一点:想百分百保活servic...
    PengPengPro阅读 16,327评论 10 65
  • 版权声明:本文为LooperJing原创文章,转载请注明出处! 自己曾经也在这个问题上伤过脑经,前几日刚好有一个北...
    LooperJing阅读 29,774评论 98 481
  • Android进程保活主要包括两个方面:提高进程的优先级,降低被杀死的概率在进程被杀死后拉活 1.进程优先级 An...
    hxljy阅读 1,176评论 1 5
  • 推荐指数: 6.0 书籍主旨关键词:特权、焦点、注意力、语言联想、情景联想 观点: 1.统计学现在叫数据分析,社会...
    Jenaral阅读 5,753评论 0 5
  • 城空了,有树长出来 我的城死了 铸起它的人,杀死它的人 不愿因为这件事而骄傲 一座城的终结 永远因为终结这件事而显...
    于十六阅读 2,890评论 6 17