使用所有方法,但应用程序不会重新启动

时间:2019-09-20 11:49:31

标签: android broadcastreceiver

在任务管理器(最近)中按“清除全部”按钮后,应用程序关闭

这些是声明的清单文件服务

<service
            android:name=".BackgroundServices"
            android:enabled="true"
            android:stopWithTask="false" />
<receiver
            android:name=".RestartService"
            android:enabled="true"
            android:exported="true"
            android:label="RestartServiceWhenStopped"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter android:priority="1000">
                <action android:name="RestartService" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT" />

                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

其重启服务

public class RestartService extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {

            Log.i("Broadcast Listened", "Service tried to stop");
            //Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(new Intent(context, BackgroundServices.class));
            } else {
                context.startService(new Intent(context, BackgroundServices.class));
            }


           // context.startService(new Intent(context.getApplicationContext(), NotificationService.class));
        }


        Log.i("Broadcast Listened", "Service tried to stop");
        //Toast.makeText(context, "Service restarted", Toast.LENGTH_SHORT).show();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            context.startForegroundService(new Intent(context, BackgroundServices.class));
        } else {
            context.startService(new Intent(context, BackgroundServices.class));
        }


    }
}

其包含后台进程的后台类

public class BackgroundServices extends Service implements LocationListener {

    private BroadcastReceiver receiver;
    public int counter = 0;
    LockScreen obj = new LockScreen();
    // context is important
    // every gui or view or activity have context
    // i will use context of NotificationService class
    Context mContext;
    // database class
    Apply_password_on_AppDatabase db = new Apply_password_on_AppDatabase(this);
    Password_Database pass_db = new Password_Database(this);

    // flag is used for stopping or running loop check of
    // current app running
    static int flag = 0;
    static int flag2 = 0;
    public String strCurrentSpeed;
    public int speed;
    // when any app is lanuch and it have password set on it
    // that app name save in current_app varaible
    String current_app = "";

    public BackgroundServices() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // add context of NotificationService to mContext variable
        mContext = this;
        // oreo used different approach for background services
        // other use same approach



        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(1, new Notification());


    }

    //  oreo api approach

    @RequiresApi(Build.VERSION_CODES.O)
    private void startMyOwnForeground() {
        String NOTIFICATION_CHANNEL_ID = "example.permanence";
        String channelName = "Background Service";
        NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
        chan.setLightColor(Color.BLUE);
        chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        assert manager != null;
        manager.createNotificationChannel(chan);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        Notification notification = notificationBuilder.setOngoing(true)
                .setContentTitle("App is running in background")
                .setPriority(NotificationManager.IMPORTANCE_MIN)
                .setCategory(Notification.CATEGORY_SERVICE)
                .build();
        startForeground(2, notification);
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);


        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
            startMyOwnForeground();
        else
            startForeground(1, new Notification());


        startTimer();
        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        stoptimertask();
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("restartservice");
        // broadcastIntent.setPriority(1000); // needs for ( devices >= O )
        broadcastIntent.setClass(this, RestartService.class);
        this.sendBroadcast(broadcastIntent);
        super.onDestroy();

    }

但是当应用程序被任务管理器杀死时,仍然不会重新启动

0 个答案:

没有答案