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

时间:2017-10-04 12:13:35

标签: android service background

我已经创建了一个后台服务,它定期点击我的服务器但不幸的是,后台服务在一段时间后停止。

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


}

}

2 个答案:

答案 0 :(得分:0)

使用最新版本的Android API 26级,后台服务有限制。请参阅Background execution limits

答案 1 :(得分:0)

只有在代码的任何位置调用startService时,

onStartCommand才会自动撤消。因此仅在Manifest中声明服务是不够的。

因此,请致电

运行服务
startService(new Intent(context, serviceName.class));

如果服务没有停止,上面的代码onStartCommand方法将被定期撤销。

有关您的信息,请从最近的关闭服务中删除应用。并且

@Override
public void onTaskRemoved(Intent rootIntent) {

}
正在调用

方法。

如果您手动启动服务,即使正在关闭,它也会自动重新启动。