Android foreground service creation method

When it comes to front desk service, experienced students will think of keep-alive. There are actually many keep-alive libraries for everyone to use, but here is a record of this most traditional method. At the same time, we also need front desk services for software similar to music playback.

The most important thing is our Service class

public class MyService extends Service {

    private int one;
    private int count;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(100,getNotification("标题","内容"));
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        Log.i("infomationHaha",one+"");
                        one++;
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private Notification getNotification(String title,String message){
        count++;
        //创建一个跳转到活动页面的意图
        Intent clickIntent = new Intent(this,MainActivity.class);
        clickIntent.putExtra("flag",count);
        //创建一个用于页面跳转的延迟意图
        PendingIntent contentIntent = PendingIntent.getActivity(this,count,clickIntent
                ,PendingIntent.FLAG_UPDATE_CURRENT);
        //创建一个通知消息的构造器
        Notification.Builder builder = new Notification.Builder(this);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            //Android8.0开始必须给每个通知分配对应的渠道
            builder = new Notification.Builder(this,"channel_id");
        }
        builder.setContentIntent(contentIntent)//设置内容的点击意图
                .setAutoCancel(true)//设置是否允许自动清除
                .setSmallIcon(R.mipmap.ic_launcher)//设置状态栏里的小图标
                .setTicker("提示消息来啦")//设置状态栏里面的提示文本
                .setWhen(System.currentTimeMillis())//设置推送时间,格式为"小时:分钟"
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))//设置通知栏里面的大图标
                .setContentTitle(title)//设置通知栏里面的标题文本
                .setContentText(message);//设置通知栏里面的内容文本
        //根据消息构造器创建一个通知对象
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            Notification notify = builder.build();
            return notify;
        }
        return null;
    }
}

The startForeground method in the onCreate method is the core. As long as this method is called, the foreground service can be implemented and the priority of the application can be improved.

In addition, versions higher than Android 9.0 need to add a foreground service permission.

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

Starting the Service and registering the manifest file is the same as using the ordinary Service. Students who don’t know how to do it can refer to my previous article.

Guess you like

Origin blog.csdn.net/weixin_38322371/article/details/114023770