How to make Service that is unremovable even if killed by the system?

后端 未结 3 1515
北荒
北荒 2021-01-25 22:46

I am creating a Blue light filter app. So that, I want to display the view over all the apps. I did it by the following Service,

public class OverlayService exte         


        
相关标签:
3条回答
  • 2021-01-25 23:09

    the service must be restarted because android donot allow it. You can use Foreground service to improve your app.

    0 讨论(0)
  • 2021-01-25 23:16

    First Create BroadcastReceiver

    public class Restarter extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        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, YourService.class));
        } else {
            context.startService(new Intent(context, YourService.class));
        }
     } 
    }
    

    Add below code in your service

     @Override
    public void onDestroy() {
        super.onDestroy();
        stoptimertask();
    
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("restartservice");
        broadcastIntent.setClass(this, Restarter.class);
        this.sendBroadcast(broadcastIntent);
    }
    
    
    
    private Timer timer;
    private TimerTask timerTask;
    public void startTimer() {
        timer = new Timer();
        timerTask = new TimerTask() {
            public void run() {
                Log.i("Count", "=========  "+ (counter++));
            }
        };
        timer.schedule(timerTask, 1000, 1000); //
    }
    
    public void stoptimertask() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    

    In your MainActivity add below code

    public class MainActivity extends AppCompatActivity {
    Intent mServiceIntent;
    private YourService mYourService;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mYourService = new YourService();
        mServiceIntent = new Intent(this, mYourService.getClass());
        if (!isMyServiceRunning(mYourService.getClass())) {
            startService(mServiceIntent);
        }
    }
    
    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                Log.i ("Service status", "Running");
                return true;
            }
        }
        Log.i ("Service status", "Not running");
        return false;
    }
    
    
    @Override
    protected void onDestroy() {
        stopService(mServiceIntent);
        super.onDestroy();
    }   }
    

    In Manifest.XML

     <receiver
        android:name="Restarter"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="restartservice" />
        </intent-filter>
    </receiver>
    
    0 讨论(0)
  • 2021-01-25 23:23

    Try this add this method in service class

    @Override
    public void onTaskRemoved(Intent rootIntent){
        super.onTaskRemoved(rootIntent);
     }
    
    0 讨论(0)
提交回复
热议问题