Android - 当 App 关闭时 Volley 请求抛出 ECONNREFUSED
Android - Volley request throws ECONNREFUSED when App is closed
我有一个运行 intentservice
的警报管理器,它每分钟从服务器获取数据,然后发送通知。问题是当应用程序打开时我仍然可以获得数据但是如果应用程序关闭它发送它抛出 ECONNREFUSED
.
齐射错误:
java.util.concurrent.ExecutionException:com.android.volley.NoConnectionError:
java.net.ConnectException: failed to connect to samples.openweathermap.org/138.201.197.100 (port 443) AFTER 2500ms: isConnected failed: ECONNREFUSED (Connection refused)
在 Android 5.1.1
进行测试
主要Activity:
private void scheduleAlarm() {
Intent toastIntent= new Intent(getApplicationContext(),MyBroadcastReceiver.class);
PendingIntent toastAlarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
long startTime=System.currentTimeMillis(); //alarm starts immediately
AlarmManager backupAlarmMgr=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
backupAlarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,startTime,1000*30,toastAlarmIntent); // alarm will repeat after every 15 minutes
}
广播接收器:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyJobIntentService.enqueueWork(context,intent);
}}
MyJobIntentService:
public class MyJobIntentService extends JobIntentService {
private String status="";
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, MyJobIntentService.class, 1000, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
String url = "http://forsitefloodapp.xyz/Func/Func_GetFloodReportsBarangay";
RequestFuture<JSONArray> future = RequestFuture.newFuture();
JsonArrayRequest request = new JsonArrayRequest(url,future,future);
requestQueue.add(request);
try {
JSONArray response = future.get(10, java.util.concurrent.TimeUnit.SECONDS);
int size = response.length();
if(size==0){
status="NO REPORTS";
}
else {
status="REPORTS DETECTED";
}
} catch (InterruptedException e) { status="Interuppted";
} catch (ExecutionException e) { status=e.toString();
} catch (TimeoutException e) { status="Timeout";
}
Log.i("MyJobIntentService", "Completed service @ " + status);
//Sends notification
}}
intentservice
将无法作为 api 21
及更高版本的后台服务(应用程序已关闭)工作..如果您只是针对 api 21
之后使用 JobScheduler API
...如果你的目标是 Api 14
之后(Oreo 或 pre-Oreo)使用 JobIntentService
作为它的 modernway.your 警报管理器应该触发 broadcastreceiver
,它会在时间到了时启动服务。服务将获取数据并为此发送通知。
我有一个运行 intentservice
的警报管理器,它每分钟从服务器获取数据,然后发送通知。问题是当应用程序打开时我仍然可以获得数据但是如果应用程序关闭它发送它抛出 ECONNREFUSED
.
齐射错误:
java.util.concurrent.ExecutionException:com.android.volley.NoConnectionError:
java.net.ConnectException: failed to connect to samples.openweathermap.org/138.201.197.100 (port 443) AFTER 2500ms: isConnected failed: ECONNREFUSED (Connection refused)
在 Android 5.1.1
主要Activity:
private void scheduleAlarm() {
Intent toastIntent= new Intent(getApplicationContext(),MyBroadcastReceiver.class);
PendingIntent toastAlarmIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
long startTime=System.currentTimeMillis(); //alarm starts immediately
AlarmManager backupAlarmMgr=(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
backupAlarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,startTime,1000*30,toastAlarmIntent); // alarm will repeat after every 15 minutes
}
广播接收器:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MyJobIntentService.enqueueWork(context,intent);
}}
MyJobIntentService:
public class MyJobIntentService extends JobIntentService {
private String status="";
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, MyJobIntentService.class, 1000, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
String url = "http://forsitefloodapp.xyz/Func/Func_GetFloodReportsBarangay";
RequestFuture<JSONArray> future = RequestFuture.newFuture();
JsonArrayRequest request = new JsonArrayRequest(url,future,future);
requestQueue.add(request);
try {
JSONArray response = future.get(10, java.util.concurrent.TimeUnit.SECONDS);
int size = response.length();
if(size==0){
status="NO REPORTS";
}
else {
status="REPORTS DETECTED";
}
} catch (InterruptedException e) { status="Interuppted";
} catch (ExecutionException e) { status=e.toString();
} catch (TimeoutException e) { status="Timeout";
}
Log.i("MyJobIntentService", "Completed service @ " + status);
//Sends notification
}}
intentservice
将无法作为 api 21
及更高版本的后台服务(应用程序已关闭)工作..如果您只是针对 api 21
之后使用 JobScheduler API
...如果你的目标是 Api 14
之后(Oreo 或 pre-Oreo)使用 JobIntentService
作为它的 modernway.your 警报管理器应该触发 broadcastreceiver
,它会在时间到了时启动服务。服务将获取数据并为此发送通知。