Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从用户强制停止或清除任务中保存或阻止我的后台服务_Java_Android_Eclipse_Android Studio_Service - Fatal编程技术网

Java 如何从用户强制停止或清除任务中保存或阻止我的后台服务

Java 如何从用户强制停止或清除任务中保存或阻止我的后台服务,java,android,eclipse,android-studio,service,Java,Android,Eclipse,Android Studio,Service,你好,朋友们,我正在创建一个android应用程序锁应用程序,用户可以在其中锁定应用程序,因为我正在使用一个始终在后台运行的后台服务。我的服务在所有设备上都运行良好,但在一些新设备上,如OPPO,Redme Mi和Lenovo Mobile有一个advace功能,如用户可以从清洁器中清理所有正在运行的任务,由于此高级功能,当用户清理所有任务时,我的服务也会停止,应用程序锁定将不起作用,因此用户需要进入应用程序并手动启动服务。是否有任何解决方案可以保护我的服务不受此影响。 其中一个应用程序锁有这种

你好,朋友们,我正在创建一个android应用程序锁应用程序,用户可以在其中锁定应用程序,因为我正在使用一个始终在后台运行的后台服务。我的服务在所有设备上都运行良好,但在一些新设备上,如OPPO,Redme Mi和Lenovo Mobile有一个advace功能,如用户可以从清洁器中清理所有正在运行的任务,由于此高级功能,当用户清理所有任务时,我的服务也会停止,应用程序锁定将不起作用,因此用户需要进入应用程序并手动启动服务。是否有任何解决方案可以保护我的服务不受此影响。 其中一个应用程序锁有这种解决方案 此应用程序服务是在完成清理任务后启动的。我也想对我的应用程序执行相同的操作。我尝试了许多解决方案,但没有得到任何结果。我的服务代码如下

public class MyAppLockService extends Service {
public static int BuildV = 0;
public static final int NOTIFICATION_ID = 11259186;
private static boolean flag;
public static boolean isLauncher;
public static boolean isRunning;
public static ArrayList<String> locked_list;
public static String pack;
public static Thread th;
String currentHomePackage;
private boolean isRefreshedList;
boolean mAllowDestroy;
BroadcastReceiver mReciever;
private boolean mShowNotification;
UsageStatsManager mUsageStatsManager;
ActivityManager manager;
PowerManager pmanager;
SharedPreferences prefs;
public boolean run;
Timer f6t;
TimerTask tt;

class C02221 extends BroadcastReceiver {
    C02221() {
    }

    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(Utils.ACTION_UPDATE)) {
            MyAppLockService.this.refreshList();
        } else if (intent.getAction().equals(Utils.ACTION_STOP_SELF)) {
            MyAppLockService.this.doStopSelf();
        } else if (intent.getAction().equals(Utils.ACTION_REMOVE_APP)) {
            MyAppLockService.this.removeAppFromLockedList(intent
                    .getStringExtra("packName"));
        }
    }
}

@SuppressLint("NewApi")

class C02232 extends TimerTask {
    C02232() {
    }

    @SuppressLint("NewApi")
    public void run() {
        if (Utils.isScreenOn(MyAppLockService.this.pmanager)) {
            MyAppLockService.this.isRefreshedList = false;
            String current = "";
            try {
                current = Utils.getProcess(
                        MyAppLockService.this.mUsageStatsManager,
                        MyAppLockService.this.getApplicationContext());
            } catch (Exception e) {
                current = "";
            }
            if (current != null) {
                if (MyAppLockService.flag
                        && current
                                .equals(MyAppLockService.this.currentHomePackage)) {
                    if (MyAppLockService.this.prefs.getBoolean(
                            "immediately", true)) {
                        MyAppLockService.locked_list = new DBHelper(
                                MyAppLockService.this
                                        .getApplicationContext())
                                .getApsHasStateTrue();
                    }
                    MyAppLockService.flag = false;
                }
                if (!current
                        .equals(MyAppLockService.this.currentHomePackage)
                        && MyAppLockService.locked_list.contains(current)) {
                    Intent it;
                    if (MyAppLockService.BuildV >= 23) {
                        long endTime = System.currentTimeMillis();
                        UsageEvents usageEvents = MyAppLockService.this.mUsageStatsManager
                                .queryEvents(endTime - 10000, endTime);
                        Event event = new Event();
                        while (usageEvents.hasNextEvent()) {
                            usageEvents.getNextEvent(event);
                        }
                        if (current.equals(event.getPackageName())
                                && event.getEventType() == 1) {
                            MyAppLockService.pack = current;
                            if (MyAppLockService.this.prefs.getBoolean(
                                    "isPattern", false)) {
                                MyAppLockService.this
                                        .confirmPattern(current);
                            } else {
                                it = new Intent(
                                        MyAppLockService.this
                                                .getApplicationContext(),
                                        AppLockActivity.class);
                                it.setFlags(DriveFile.MODE_READ_ONLY);
                                MyAppLockService.this
                                        .getApplicationContext()
                                        .startActivity(it);
                            }
                            MyAppLockService.flag = true;
                            return;
                        }
                        return;
                    }
                    MyAppLockService.pack = current;
                    if (MyAppLockService.this.prefs.getBoolean("isPattern",
                            false)) {
                        MyAppLockService.this.confirmPattern(current);
                    } else {
                        it = new Intent(
                                MyAppLockService.this
                                        .getApplicationContext(),
                                AppLockActivity.class);
                        it.setFlags(DriveFile.MODE_READ_ONLY);
                        MyAppLockService.this.getApplicationContext()
                                .startActivity(it);
                    }
                    MyAppLockService.flag = true;
                }
            }
        } else if (!MyAppLockService.this.isRefreshedList) {
            MyAppLockService.this.refreshList();
        }
    }
}

public MyAppLockService() {
    this.run = true;
}

static {
    BuildV = VERSION.SDK_INT;
}

public IBinder onBind(Intent intent) {
    return null;
}

public void onCreate() {




    this.prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
    this.manager = (ActivityManager) getApplicationContext()
            .getSystemService("activity");
    this.pmanager = (PowerManager) getApplicationContext()
            .getSystemService("power");
    if (BuildV >= 21) {
        this.mUsageStatsManager = (UsageStatsManager) getApplicationContext()
                .getSystemService("usagestats");
    }
    startNotification();
    Intent intent = new Intent("android.intent.action.MAIN");
    intent.addCategory("android.intent.category.HOME");
    this.currentHomePackage = getPackageManager().resolveActivity(intent,
            Cast.MAX_MESSAGE_LENGTH).activityInfo.packageName;
    this.mReciever = new C02221();
    IntentFilter filter = new IntentFilter(Utils.ACTION_UPDATE);
    filter.addAction(Utils.ACTION_STOP_SELF);
    filter.addAction(Utils.ACTION_REMOVE_APP);
    registerReceiver(this.mReciever, filter);
    refreshList();
    this.tt = new C02232();
    this.f6t = new Timer();
    this.f6t.schedule(this.tt, 500, 500);
    super.onCreate();
    Calendar cal = Calendar.getInstance();
    Intent intent12 = new Intent(getBaseContext(),MyAppLockService.class);
    PendingIntent pintent = PendingIntent.getService(getBaseContext(), 0, intent12, 0);
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),5, pintent);
}

public int onStartCommand(Intent intent, int flags, int startId) {
    return super.onStartCommand(intent, START_STICKY_COMPATIBILITY, startId);

}

public void onDestroy() {
    try {
        this.f6t.cancel();
        this.tt.cancel();
    } catch (Exception e) {
        e.printStackTrace();
    }

        Intent intent1 = new Intent("com.android.techtrainner");
        intent1.putExtra("yourvalue", "torestore");
        sendBroadcast(intent1);


}



public void refreshList() {
    locked_list = new DBHelper(getApplicationContext())
            .getApsHasStateTrue();
    if (locked_list.size() == 0) {
        doStopSelf();
    }
    this.isRefreshedList = true;
}

@SuppressLint({ "InlinedApi" })
private void startForegroundWithNotification() {
    PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this,
            Calculator_Activity.class), 0);
    String title = getString(R.string.app_name);
    String content = getString(R.string.app_name);
    Builder nb = new Builder(this);
    nb.setSmallIcon(R.drawable.ic_transparent);
    nb.setContentTitle(title);
    nb.setContentText(content);
    nb.setWhen(System.currentTimeMillis());
    nb.setContentIntent(pi);
    nb.setOngoing(true);
    nb.setPriority(0);
    startForeground(NOTIFICATION_ID, nb.build());
}

private void startNotification() {
    startForegroundWithNotification();
    if (!this.mShowNotification) {
        HelperService.removeNotification(this);
    }
}

private void doStopSelf() {
    this.mAllowDestroy = true;
    unregisterReceiver(this.mReciever);
    stopForeground(true);
    stopSelf();
}

private void confirmPattern(String st) {

    Intent pIntent = new Intent(getApplicationContext(),
            ResetActivity.class);
    pIntent.putExtra("isFromReset", true);
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),
            12345, pIntent, DriveFile.MODE_READ_ONLY);

    Intent intent = new Intent(LockPatternActivity.ACTION_COMPARE_PATTERN, null,
            getApplicationContext(), LockPatternActivity.class);
    intent.putExtra("packName", st);
    intent.putExtra("isStealthMode", this.prefs.getBoolean(
            AlpSettings.Display.METADATA_STEALTH_MODE, false));
    intent.putExtra("isFromLock", true);
    intent.setFlags(DriveFile.MODE_READ_ONLY);
    intent.addFlags(Cast.MAX_MESSAGE_LENGTH);

    intent.putExtra(LockPatternActivity.EXTRA_PENDING_INTENT_FORGOT_PATTERN, pi);

    startActivity(intent);

}

private void removeAppFromLockedList(String packName) {
    locked_list.remove(packName);
}
公共类MyAppLockService扩展服务{
公共静态int BuildV=0;
公共静态最终整数通知_ID=11259186;
私有静态布尔标志;
公共静态布尔isLauncher;
公共静态布尔值正在运行;
公共静态ArrayList锁定列表;
公共静态字符串包;
公共静态线程;
字符串当前HomePackage;
私有布尔isRefreshedList;
布尔马洛夫销毁;
广播接收机;
私有布尔MSHOW通知;
UsageStatsManager mUsageStatsManager;
活动经理;
PowerManager-pmanager;
共享引用优先权;
公营;
定时器f6t;
TimerTask tt;
C02221类接收机{
C02221(){
}
公共void onReceive(上下文、意图){
if(intent.getAction().equals(Utils.ACTION_UPDATE)){
MyAppLockService.this.refreshList();
}else if(intent.getAction().equals(Utils.ACTION\u STOP\u SELF)){
MyAppLockService.this.doStopSelf();
}else if(intent.getAction().equals(Utils.ACTION\u REMOVE\u APP)){
MyAppLockService.this.removeAppFromLockedList(意图
.getStringExtra(“packName”);
}
}
}
@SuppressLint(“新API”)
类C02232扩展了TimerTask{
C02232(){
}
@SuppressLint(“新API”)
公开募捐{
if(Utils.isScreenOn(MyAppLockService.this.pmanager)){
MyAppLockService.this.isRefreshedList=false;
字符串current=“”;
试一试{
当前=Utils.getProcess(
MyAppLockService.this.mUsageStatsManager,
MyAppLockService.this.getApplicationContext());
}捕获(例外e){
当前=”;
}
如果(当前!=null){
if(MyAppLockService.flag
&&当前
.equals(MyAppLockService.this.currentHomePackage)){
if(MyAppLockService.this.prefs.getBoolean(
“立即”,对{
MyAppLockService.locked_list=new DBHelper(
MyAppLockService.this
.getApplicationContext())
.getApsHasStateTrue();
}
MyAppLockService.flag=false;
}
如果(!当前
.equals(MyAppLockService.this.currentHomePackage)
&&MyAppLockService.locked_list.contains(当前)){
意图;
如果(MyAppLockService.BuildV>=23){
long-endTime=System.currentTimeMillis();
UsageEvents UsageEvents=MyAppLockService.this.mUsageStatsManager
.queryEvents(endTime-10000,endTime);
事件=新事件();
while(usageEvents.hasNextEvent()){
usageEvents.getNextEvent(事件);
}
if(current.equals(event.getPackageName())
&&event.getEventType()=1){
MyAppLockService.pack=当前;
if(MyAppLockService.this.prefs.getBoolean(
“isPattern”,假){
MyAppLockService.this
.确认模式(当前);
}否则{
它=新的意图(
MyAppLockService.this
.getApplicationContext(),
AppLockActivity.class);
it.setFlags(DriveFile.MODE\u只读);
MyAppLockService.this
.getApplicationContext()
.星触觉(it);
}
MyAppLockService.flag=true;
返回;
}
返回;
}
MyAppLockService.pack=当前;
if(MyAppLockService.this.prefs.getBoolean)(“isPattern”,
假的){
MyAppLockService.this.confirmPattern(当前);
}否则{
它=新的意图(
MyAppLockService.this
.getApplicationContext(),
AppLockActivity.class);
it.setFlags(DriveFile.MODE\u只读);
MyAppLockService.this.getApplicationContext()
.星触觉(it);
}
MyAppLockService.flag=true;
}
}
}如果(!MyAppLockService.this.isRef
                    if(!ServiceClass.boolRuningService){
                           AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
                        alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30*1000, pintent);
                    }