【发布时间】:2017-10-23 10:20:16
【问题描述】:
我的 IntentService 有问题。我需要我的服务在后台每 1 分钟访问一次网站中的 JSON 文件,而不打开应用程序(发出通知,我在 JSON 中收到)。它会这样做,但 3-4 分钟后,它会抛出“java.net.SocketTimeoutException: connect timed out”错误,我仍然可以将域解析为 IP 地址,但我无法连接到网站,并且手机仍然有移动连接。所有网站都会发生同样的情况。
我的 IntentService:
public class BackgroundService extends IntentService {
Runnable runnable = () -> new bgJob().execute();
Handler handler = new Handler();
DatabaseHandler db = new DatabaseHandler(this);
public BackgroundService() {
super("BackgroundService");
}
@Override
public void onCreate() {
super.onCreate();
runnable.run();
}
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent){
Log.d("BG_SERVICE","TASK REMOVED!");
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);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("BG_SERVICE","TASK DESTROYED!");
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);
}
@Override
protected void onHandleIntent(Intent intent) {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
runnable.run();
}
private class bgJob extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
Request request = new Request.Builder()
.url("https://google.com/")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
int webresponse = response.code();
Log.d("Response","CODE: "+webresponse);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
handler.postDelayed(runnable, 60000);
}
}}
我在手机上使用 Android 7.0 并连接移动设备。
【问题讨论】:
-
这在 Android 6.0+ 设备上是不可能的。用户非常厌倦开发人员认为他们需要频繁地与他们的服务器通信并对用户造成伤害(耗尽电池、消耗过多带宽等)。在最近的 Android 版本中添加的打盹模式、应用待机和后台执行限制专门用于防止您尝试执行的操作。
-
@CommonsWare 所以没有办法,我可以在后台接收这个 JSON 信息吗? Whatsapp 等应用程序如何在后台接收新消息?
-
"那么没有办法,我可以在后台接收到这个JSON信息吗?" - 你当然可以。您不能通过轮询服务器每分钟连续执行此操作。 “Whatsapp 等应用程序如何在后台接收新消息?” -- 我假设他们使用 Firebase 云消息传递。
-
转到设置 -> 电池使用情况并允许手机在后台或屏幕关闭时使用服务。在大多数最近的手机中,它会被禁用以节省电池寿命。
-
@RakeshPolo 我认为这不是理想的解决方案,因为每个应用程序用户都需要这样做。
标签: java android intentservice miui