android 使用Service进行双进程守护,防止进程被杀

以下所用的方法只适用于防止app太久在后台太久而被系统杀死,在系统的一键清除功能下进程还是会被杀死的。

若要防止进程被系统的一键清除功能杀死,要设置允许当前app自启动。如何设置请看 跳转自启动管理页


定义MyService

public class MyService extends Service {
    String msg;
    public MyService() {
        msg = "Msg from MyService";
    }

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

    public class MyBinder extends Binder{
        public String getMsg(){
            return msg;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent service = new Intent(this, OtherService.class);
        startService(service);
        bindService(service, sc, Context.BIND_IMPORTANT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext());
        //设置为0,通知栏不可见。设置为1,通知栏可见
        startForeground(0, builder.build());

        //返回此参数可以在Service因系统资源紧张被杀死的时候尝试重启
        return START_STICKY;
    }

    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //由于配置之后OtherService不在同一线程,无法进行交互
//            OtherService.OtherBinder otherBinder = (OtherService.OtherBinder) service;
//            Toast.makeText(MyService.this, otherBinder.getMsg(), Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(),"绑定MyService",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 连接出现了异常断开了,OtherService被杀掉了
            Toast.makeText(getApplicationContext(),"OtherService挂了",Toast.LENGTH_SHORT).show();
            Intent service = new Intent(MyService.this, OtherService.class);
            startService(service);
            bindService(service, sc, Context.BIND_IMPORTANT);
        }
    };
}

定义OtherService

public class OtherService extends Service {
    String msg;
    public OtherService() {
        msg = "Msg from OtherService";
    }

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

    public class OtherBinder extends Binder {
        public String getMsg(){
            return msg;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Intent service = new Intent(this, MyService.class);
        startService(service);
        bindService(service, sc, Context.BIND_IMPORTANT);
        return START_STICKY;
    }

    private ServiceConnection sc = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
//            MyService.MyBinder myBinder = (MyService.MyBinder) service;
//            Toast.makeText(OtherService.this,myBinder.getMsg(),Toast.LENGTH_SHORT).show();

            Toast.makeText(getApplicationContext(),"绑定OtherService",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // 连接出现了异常断开了,MyService被杀掉了
            Toast.makeText(getApplicationContext(),"MyService挂了",Toast.LENGTH_SHORT).show();
            Intent service = new Intent(OtherService.this, MyService.class);
            startService(service);
            bindService(service, sc, Context.BIND_IMPORTANT);
        }
    };
}


配置manifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zero.servicetest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true" />
        <!--运行在“:OtherService”线程,防止两个服务一起被杀死-->
        <service
            android:name=".OtherService"
            android:enabled="true"
            android:exported="false"
            android:process=":OtherService"></service>
    </application>

</manifest>


开启服务

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, MyService.class);
        startService(intent);
        Intent intent2 = new Intent(this, OtherService.class);
        startService(intent2);
    }
    
}




猜你喜欢

转载自blog.csdn.net/m940034240/article/details/76982805