简体   繁体   English

一段时间后android后台服务停止

[英]android background service stops after some time

I have created a background service which is hitting my server on a regular interval but unfortunately, the background service stops after some time. 我创建了一个后台服务,该服务每隔一定时间就会命中我的服务器,但是很遗憾,后台服务会在一段时间后停止。

public class LocationService extends Service{

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //TODO do something useful

        new DownloadFilesTask().execute("");
        Toast.makeText(getApplicationContext(), "start", Toast.LENGTH_SHORT).show();

  return START_STICKY;
}


private class DownloadFilesTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {

    try {
        URL url = new URL(siteurl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
            bufferedReader.close();
            return stringBuilder.toString();

        }
        finally{
            urlConnection.disconnect();
        }
    }
    catch(Exception e) {
        Log.e("ERROR", e.getMessage(), e);
       return null;
    }

    }

    @Override
    protected void onPostExecute(String result) {
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

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

@Override
public void onDestroy() {
    super.onDestroy();

   Intent myIntent = new Intent(getApplicationContext(), LocationService.class);

PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);

AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);


alarmManager1.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.add(Calendar.SECOND, 10);

alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);

}
@Override
public void onTaskRemoved(Intent rootIntent) {
    super.onTaskRemoved(rootIntent);

    Intent myIntent = new Intent(getApplicationContext(), LocationService.class);

PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);

AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);


alarmManager1.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();

calendar.setTimeInMillis(System.currentTimeMillis());

calendar.add(Calendar.SECOND, 10);

alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);


}

}

With the latest release of Android API level 26, there is limit put on background services. 在最新版本的Android API级别26中,后台服务受到限制。 Please refer Background execution limits 请参考后台执行限制

onStartCommand will revoke automatically only if startService is called once from anywhere of your code. 仅当从代码的任何位置调用一次startService时,onStartCommand才会自动撤消。 So declaring service only in Manifest would not enough. 因此仅在清单中声明服务是不够的。

So, run service by calling 因此,通过调用来运行服务

startService(new Intent(context, serviceName.class));

Following above codes onStartCommand method will be revoked periodically if service is not stopped. 如果服务未停止,则上述代码的onStartCommand方法将被定期吊销。

For your information, removing app from recent closes service. 供您参考,从最近关闭服务中删除应用程序。 And

@Override
public void onTaskRemoved(Intent rootIntent) {

}

method is being called. 方法被调用。

If you start service manually it will start again automatically even is being closed. 如果您手动启动服务,即使已关闭它也会自动重新启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM