如何使即使被系统杀死也无法移动的服务? [英] How to make Service that is unremovable even if killed by the system?

查看:66
本文介绍了如何使即使被系统杀死也无法移动的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建蓝光滤镜应用程序.因此,我想显示所有应用程序的视图.我是通过以下服务做到的,

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 extends Service {
    public OverlayService() {}
    View mView;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();

        WindowManager.LayoutParams params;
        if (Build.VERSION.SDK_INT < 26)
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, 
                    PixelFormat.TRANSLUCENT);
        else
            params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.MATCH_PARENT,
                    WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                            | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                            | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                    PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.RIGHT | Gravity.TOP;
        params.setTitle("Load Average");
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        mView = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.overlay, null);
        wm.addView(mView, params);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mView != null) {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
        }

    }
}

工作正常.

"如果我从最近的应用中删除应用,则会停止服务,然后自动几秒钟后启动服务"

"if I remove app from recents, It stops service then automatically starts the service after few seconds"

为什么服务停止并重新启动?如何避免这种情况?

Why the service is stopped and restarted? How to avoid this?

我尝试覆盖 onStartCommand(),并使用了START_STICKY .但是没用.

I tried overriding onStartCommand() and used START_STICKY . But no use.

帮帮我.预先谢谢!!!

Help me. Thanks in advance!!!

推荐答案

首先创建 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;
}

在您的MainActivity中添加以下代码

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();
}   }

在Manifest.XML中

In Manifest.XML

 <receiver
    android:name="Restarter"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="restartservice" />
    </intent-filter>
</receiver>

这篇关于如何使即使被系统杀死也无法移动的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆