美文网首页
Android8.0 Server启动并取消弹框

Android8.0 Server启动并取消弹框

作者: 帅气企鹅 | 来源:发表于2020-04-22 16:29 被阅读0次

    Android8.0以上server的启动有新的方法:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                startForegroundService(new Intent(this, AreaService.class));

    }else {

                startService(new Intent(this, AreaService.class));

    }

    在AreaService的onCreate和onDestroy添加下面的方法:

    @Override

        public void onCreate() {

            super.onCreate();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                StringCHANNEL_ID="myservice";

                NotificationChannel channel =new NotificationChannel(CHANNEL_ID,

                        "mytest", NotificationManager.IMPORTANCE_HIGH);

                ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

                Notification notification =new NotificationCompat.Builder(this, CHANNEL_ID)

    .setContentTitle("")

    .setContentText("").build();

                startForeground(1, notification);   //主要这边的不要写startForeground(0, notification); 

         NotificationManager mNotificationManager = (NotificationManager)                         getSystemService(Context.NOTIFICATION_SERVICE);

                mNotificationManager.deleteNotificationChannel(CHANNEL_ID); //    取消弹框操作

            }

    }

    @Override

    public void onDestroy() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                stopForeground(true);

        }

        super.onDestroy();

    }

    相关文章

      网友评论

          本文标题:Android8.0 Server启动并取消弹框

          本文链接:https://www.haomeiwen.com/subject/vxxwihtx.html