我面临着Android服务的奇怪问题。我正在创建一个应用程序锁定器,它具有一个服务,该服务将继续检查哪个应用程序在前台并相应地采取必要的操作。只要服务正在运行,所有工作正常。一旦我从最近的应用程序或应用程序清除应用程序或服务被低内存的Android操作系统杀死它就永远不会启动。
我为解决问题所做的工作:
onTaskRemoved(Intent rootIntent)
和onDestroy()
android:name=".view.locker.CheckAppService"
android:stopWithTask="false"
似乎没什么用。请帮忙。
供您参考我在下面分享整个服务代码:
public class CheckAppService extends Service{private static Timer timer = new Timer();public Boolean userAuth = false;private Context ctx;public String pActivity="";private String lastPackageName = "";
public IBinder onBind(Intent arg0)
{
return null;
}
public void onCreate()
{
super.onCreate();
ctx = this;
startService();
Utility.printLog("@@@@ service started");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return Service.START_STICKY;
}
private void startService()
{
timer.scheduleAtFixedRate(new mainTask(), 0, 500);
}
private class mainTask extends TimerTask
{
public void run()
{
toastHandler.sendEmptyMessage(0);
}
}
@Override
public void onDestroy()
{
super.onDestroy();
Utility.printLog("@@@@ service destroyed");
Intent intent = new Intent("com.e2e.mobicleaner.locker.restart");
sendBroadcast(intent);
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
/*Intent intent = new Intent("com.e2e.mobicleaner.locker.restart");
sendBroadcast(intent);*/
Intent restartServiceTask = new Intent(getApplicationContext(),this.getClass());
restartServiceTask.setPackage(getPackageName());
PendingIntent restartPendingIntent =PendingIntent.getService(getApplicationContext(), 1,restartServiceTask, PendingIntent.FLAG_ONE_SHOT);
AlarmManager myAlarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
myAlarmService.set(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime() + 1000, restartPendingIntent);
}
private final Handler toastHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
String currentPackage = null;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*1, time);
// Sort the stats by the last time used
if(stats != null) {
SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
}
if(!mySortedMap.isEmpty()) {
currentPackage = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
Utility.printLog("@@@@ currentPackage: " + currentPackage);
Utility.printLog("@@@@ lastPackage: " + lastPackageName);
}
else {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningTask = mActivityManager.getRunningAppProcesses();
//
/*for (ActivityManager.RunningTaskInfo aTask : RunningTask) {
System.out.println("###########");
System.out.println("@@@@@@ running base: "+aTask.baseActivity.flattenToShortString());
System.out.println("@@@@@@ running base: "+aTask.baseActivity.getClassName());
System.out.println("@@@@@@ running: "+aTask.topActivity.flattenToShortString());
System.out.println("@@@@@@ running: "+aTask.topActivity.getClassName());
System.out.println("##########");
}*/
currentPackage = runningTask.get(0).processName;
Utility.printLog("@@@@ currentPackage: " + currentPackage);
Utility.printLog("@@@@ lastPackage: " + lastPackageName);
}
SharedPreferences spLockedApps = CheckAppService.this.getSharedPreferences("lockedApp", Context.MODE_MULTI_PROCESS);
SharedPreferences spUnLockedApps = CheckAppService.this.getSharedPreferences("unLockedApp", Context.MODE_MULTI_PROCESS);
Utility.printLog("@@@@ LockedApp count: "+spLockedApps.getAll().size());
if (!TextUtils.isEmpty(currentPackage) && !currentPackage.equalsIgnoreCase(getPackageName())) {
if (spUnLockedApps.contains(currentPackage)) {
} else if (spLockedApps.contains(currentPackage) && !currentPackage.equalsIgnoreCase(lastPackageName)) {
Utility.printLog("@@@@ else else");
Intent intent = new Intent(CheckAppService.this, LockActivity.class);
intent.putExtra(Lock.TYPE, Lock.UNLOCK_APP);
intent.putExtra(Lock.MESSAGE, getString(R.string.enter_passcode));
intent.putExtra(Lock.PACKAGE_NAME, currentPackage);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
lastPackageName = currentPackage;
}
}
};
}
宣言宣布服务
<service
android:name=".view.locker.CheckAppService"
android:enabled="true"
android:process="com.e2e.mobicleaner.view.locker.service"
android:stopWithTask="false" />