【发布时间】:2018-01-09 15:07:32
【问题描述】:
这几天我想弄清楚如何解决这个问题。
我有一个 Android 应用程序,它实际上由一个简单的 WebView 和一个用于启动/停止服务的按钮组成。该服务启动一个任务(每 60 秒)检查是否有任何更新(调用外部 PHP 函数),如果有,它会显示通知。
我还有一个由服务的“onTaskRemoved”函数调用的 BroadcastReceiver(我也尝试从 onDestroy 调用 BroadcastReceiver,但结果相同),重新启动关闭时服务。
应用打开时后台服务正常工作;当我关闭它时,“onTaskRemoved”调用了重新启动服务的 BroadcastReceiver,它仍然可以工作几分钟(每次它改变多长时间)。几分钟后,检查更新的任务停止工作,我在日志中看到的唯一消息是 Activity 已被杀死(收到的最后一条消息是 "01-09 15:03:34.593 4306 5082 I ActivityManager : Killing 13691:com.xxx.xxx/u0a262 (adj 906): DHA:SPC_empty #31”,但根据我在许多研究中读到的内容,该服务应该继续运行,我错了吗? .
下面是部分代码:
主类中启动/停止服务的按钮点击监听器:
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if (btn.getText().equals("Start Monitoring"))
{
mMonitoringService = new MonitoringService(getCtx());
mServiceIntent = new Intent(getCtx(), mMonitoringService.getClass());
if (!isMyServiceRunning(mMonitoringService.getClass())) {
startService(mServiceIntent);
}
btn.setText("Stop Monitoring");
...
}
else
{
mMonitoringService = new MonitoringService(getCtx());
if (isMyServiceRunning(mMonitoringService.getClass())) {
mServiceIntent = new Intent(getCtx(), mMonitoringService.getClass());
stopService(mServiceIntent);
}
btn.setText("Start Monitoring");
...
}
}
});
服务:
public class MonitoringService extends Service {
Context mContext;
private long lastCheck = System.currentTimeMillis() - 6000044;
public MonitoringService(Context applicationContext, String UsrCookie) {
super();
mContext = applicationContext;
Log.i("SERVICE", "here I am!");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (intent != null)
{
Log.d("Intent","not null");
startMyTimer(mContext);
}
else{
Log.d("Intent","null");
}
return START_STICKY;
}
@Override
public void onDestroy(){
super.onDestroy();
Log.i("EXIT", "onDestroy!");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.i("EXIT", "onTaskRemoved!");
Intent broadcastIntent = new Intent("com.xxx.xxx.ActivityRecognition.RestartMonitoring");
broadcastIntent.setClass(this,MonitoringRestarterBroadcastReceiver.class);
sendBroadcast(broadcastIntent);
stopMyTimertask();
}
public void startMyTimer(Context inContext) {
//set a new Timer
myTimer = new Timer();
//initialize the TimerTask's job
initializeMyTimerTask(inContext);
//schedule the timer, to wake up every 0.1 second
myTimer.schedule(myTimerTask, 100, 100); //
}
public void initializeMyTimerTask(final Context inContext) {
Log.i("DPCLite", "initializeMyTimerTask");
myTimerTask = new TimerTask() {
public void run() {
try {
Long nowDate = System.currentTimeMillis();
if (((nowDate - lastCheck) / 1000) > 60 && isNetworkAvailable())
{
lastCheck = nowDate;
callPHPfunction(inContext);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
public void stopMyTimertask() {
//stop the timer, if it's not already null
if (myTimer != null) {
Log.i("stopMyTimertask", "STOPPED");
myTimer.cancel();
myTimer = null;
}
}
还有BroadcastReceiver:
public class MonitoringRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i(MonitoringRestarterBroadcastReceiver.class.getSimpleName(), "Service Stopped!");
Intent newIntent = new Intent(context, MonitoringService.class);
context.startService(newIntent);
}
}
【问题讨论】:
-
附加信息:以上所有行为都发生在三星 A5 2017 设备(具有 Android 7.0)上测试它。刚刚在 LG g4(具有 Android 6.0)上进行了测试,但没有发生这种情况(服务仍然存在)。有什么想法吗?
-
另一个附加信息:在“执行服务”列表下,我的服务似乎仍在运行,即使在任务停止后也是如此。
标签: android service broadcastreceiver android-service android-broadcastreceiver