博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android四大组件Service之前台进程(201807最新源码)
阅读量:4182 次
发布时间:2019-05-26

本文共 7513 字,大约阅读时间需要 25 分钟。

想写一个守护进程,到github上找源码,发现基本都是比较过时的。其中比较典型的是,google已经修复了不少原来可以利用的漏洞,而且NotificationCompat.Builder也已经强制要求输入NotificationChannel的信息,所以这里你必须自己用NotificationManager 来创建NotificationChannel,并把ID传给Builder,后面有详细源码,不再细述。

这里给出的,是一个android上标准的前台进程。大致思路如下,

一般在后台运行的Service的系统优先级都比较低的,当系统出现内存不足情况时,会成为优先回收的对象。当希望Service可以一直保持运行状态时,就要提高Service的优先级,其中的一个办法就是使用前台Service。其相关函数:使用startForeground可把Service设置为前台Service。取消则使用stopForeground。

至于broadcast模式,严格来说只能说是同类唤醒,好比你安装了某公司的Aapp,在安装Bapp时,它会检查你有没有装Aapp,如果有启动Aapp,就会把她唤醒(这真是一个糟糕的做法,我们的手机,大部分情况下就是这么被拖死的)。

言归正传,先看效果图,其中显示Foreground那个,就是我们的进程。

再看源码,

activity_main.xml

MainActivity.java

package com.spacesoftwares.spacecapture;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import com.spacesoftwares.spacecapture.service.BackgroundService;import com.spacesoftwares.spacecapture.service.GrayService;import com.spacesoftwares.spacecapture.service.WhiteService;public class MainActivity extends AppCompatActivity {    private Button mBtnWhite, mBtnGray, mBtnBlack, mBtnGeneral;    private final static String TAG = MainActivity.class.getSimpleName();    /**     * 黑色唤醒广播的action     */    private final static String BLACK_WAKE_ACTION = "com.wake.black";    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //View view = getWindow().getDecorView();        mBtnWhite = findViewById(R.id.btn_white);        mBtnGray = findViewById(R.id.btn_gray);        mBtnBlack = findViewById(R.id.btn_black);        mBtnGeneral = findViewById(R.id.btn_background_service);        setListener();    }    private void setListener(){        OnClick onClick = new OnClick();        mBtnWhite.setOnClickListener(onClick);        mBtnGray.setOnClickListener(onClick);        mBtnBlack.setOnClickListener(onClick);        mBtnGeneral.setOnClickListener(onClick);    }    private class  OnClick implements  View.OnClickListener{        @Override        public void onClick(View view) {            int viewId = view.getId();            if (viewId == R.id.btn_white) {         // 正常的Service                Intent whiteIntent = new Intent(MainActivity.this, WhiteService.class);                startService(whiteIntent);                        } else if (viewId == R.id.btn_background_service) {//普通的后台进程                Log.i(TAG, "MAIN: btn_background");                //Context context = getApplicationContext();                Intent bgIntent = new Intent(MainActivity.this, BackgroundService.class);                startService(bgIntent);            }        }    }}

WhiteService.java

package com.spacesoftwares.spacecapture.service;import android.app.Notification;import android.app.NotificationChannel;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Context;import android.content.Intent;import android.graphics.Color;import android.os.Build;import android.os.IBinder;import android.provider.Settings;import android.support.annotation.Nullable;import android.support.v4.app.NotificationCompat;import android.util.Log;import com.spacesoftwares.spacecapture.MainActivity;import com.spacesoftwares.spacecapture.R;import java.io.IOException;import java.nio.channels.Channel;public class WhiteService extends Service {    private final static String TAG = WhiteService.class.getSimpleName();    private final static int FOREGROUND_ID = 1000;    public WhiteService(){}    @Nullable    @Override    public IBinder onBind(Intent intent) {        //return null;        throw new UnsupportedOperationException("Not yet implemented");    }    @Override    public void onCreate() {        Log.i(TAG, "WhiteService->onCreate");        super.onCreate();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i(TAG, "WhiteService->onStartCommand");        //NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);            // Configure the notification channel.            notificationChannel.setDescription("Channel description");            notificationChannel.enableLights(true);            notificationChannel.setLightColor(Color.RED);            notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});            notificationChannel.enableVibration(true);            notificationManager.createNotificationChannel(notificationChannel);        }        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);        builder.setSmallIcon(R.mipmap.ic_launcher);        builder.setContentTitle("Foreground");        builder.setContentText("Text shown on Notification bar");        builder.setContentInfo("Content Info");        builder.setWhen(System.currentTimeMillis());        Intent activityIntent = new Intent(this, MainActivity.class);        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);        builder.setContentIntent(pendingIntent);        Notification notification = builder.build();        startForeground(FOREGROUND_ID, notification);        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        Log.i(TAG, "WhiteService->onDestroy");        super.onDestroy();    }}

backgroundService.java

package com.spacesoftwares.spacecapture.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.support.annotation.Nullable;import android.util.Log;public class BackgroundService extends Service {    public BackgroundService(){    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        //return null;        // TODO: Return the communication channel to the service.        throw new UnsupportedOperationException("Not yet implemented");    }    private final static String TAG = BackgroundService.class.getSimpleName();    @Override    public void onCreate() {        Log.i(TAG, "onCreate");        super.onCreate();    }    @Override    public void onDestroy() {        Log.i(TAG, "onDestroy");        super.onDestroy();    }    @Override    public int onStartCommand(Intent intent, int flags, final int startId) {        Log.i(TAG, "service start command, id: "+String.valueOf(startId));        new Thread(new Runnable() {            @Override            public void run() {                int i = 0;                    Log.i(TAG, "SERVICE"+String.valueOf(startId)+" IS RUNNING for " + String.valueOf(i++) + " seconds");                    try {                        Thread.sleep(1000);                    }catch (InterruptedException e){                        e.printStackTrace();                    }                    stopSelf();            }        }).start();        return super.onStartCommand(intent, flags, startId);    }}

最后,不要忘记在AndroidManifest.xml中添加service。

 

你可能感兴趣的文章
Service Intent must be explicit
查看>>
android studio SDK开发
查看>>
studio 统计代码的行数
查看>>
字符数组和16进制互换
查看>>
PHP项目中出现致命错误: Class 'Redis' not found
查看>>
There is no tracking information for the current branch.
查看>>
fatal: refusing to merge unrelated histories
查看>>
Git命令还原未提交的变更
查看>>
Linux系统中环境变量的配置
查看>>
Linux系统中配置脚本程序开机启动
查看>>
让Linux系统上的nginx支持php程序
查看>>
源码编译安装LNMP环境之Nginx篇
查看>>
源码编译安装LNMP环境之PHP篇
查看>>
Linux中rpm工具使用教程
查看>>
Linux中yum工具使用教程
查看>>
C++字符串函数
查看>>
mknod详解
查看>>
linux中的run-level何解?
查看>>
Linux内核编译详解(转自linuxSir)
查看>>
实模式,保护模式与V86模式
查看>>