我正在尝试唤醒我的设备以发出网络请求。当我将闹钟设置为一两分钟时。它完美地运作。但是当我将它设置为大约10分钟并关闭我的显示器时,警报会被提升,但是凌空会引发一个" NoConnectionError"如标题所示。
我研究过它,我使用WakeFullBroadCastReceiver实现了我的接收器。在此之后,我遇到了同样的问题。我进一步研究并为我的wifi添加了一个锁。不过,我也遇到了同样的错误。
请提供任何帮助。
接收器:
@Override
public void onReceive (Context context, Intent intent) {
Log.i("Receiver", "Called here");
Intent serIntent1 = new Intent(context, FetchTodayWordService.class);
startWakefulService(context, serIntent1);
}
服务
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("SERVICE", "started");
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
lock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, "LockTag");
lock.acquire();
MakeRequest(intent);
return START_STICKY;
}
@Override
protected void onHandleIntent (Intent intent) {
Log.i("OUTPUT", "Service called");
MakeRequest(intent);
}
public void MakeRequest(final Intent intent)
{
if (intent == null) return;
JsonObjectRequest jsonObjectRequest =
new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse (JSONObject response) {
lock.release();
wordDay = ParseWordDay.ParseJSON(response);
if(wordDay != null)
{
new StoreData().DoWork(getApplicationContext(), wordDay, intent);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse (VolleyError error) {
TodayWordReceiver.completeWakefulIntent(intent);
lock.release();
Log.i("OUTPUT", String.valueOf(error));
}
});
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
}
设置闹钟:
private void SetAlarm() {
int alarmType = AlarmManager.ELAPSED_REALTIME;
final int TIME = 1000 * 60 * 5;
Intent intent = new Intent(this, TodayWordReceiver.class);
/*boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(),
1, intent, PendingIntent.FLAG_NO_CREATE) != null);
if (!alarmUp)
{*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + TIME,
TIME, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
/*}else Toast.makeText(MainActivity.this, "ALarm already set", Toast.LENGTH_SHORT).show();*/
}
错误输出:
04-29 23:48:23.125 15702-15702 / com.example.clinton.light I / Receiver:在这里调用 04-29 23:48:23.145 15702-15702 / com.example.clinton.light I / SERVICE:已启动 04-29 23:48:23.175 15702-15702 / com.example.clinton.light I / OUTPUT:com.android.volley.NoConnectionError:java.net.UnknownHostException:无法解析主机&#34; api.wordnik.com& #34;:没有与主机名相关联的地址
真的很感激任何帮助。我可能无法标记你的答案,我没有足够的分数。
答案 0 :(得分:1)
我意识到只有在屏幕打开时(当设备处于唤醒状态时)网络操作才会成功。所以我将警报类型替换为
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
而不是:
int alarmType = AlarmManager.ELAPSED_REALTIME;
并修复了它。无需对wifi进行任何锁定或使用任何睡眠线程。
所以完整的AlarmCode如下所示以供将来参考:请不要使用问题中显示的wifi锁。
private void SetAlarm() {
int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
final int TIME = 1000 * 60 * 5;
Intent intent = new Intent(this, TodayWordReceiver.class);
/*boolean alarmUp = (PendingIntent.getBroadcast(getApplicationContext(),
1, intent, PendingIntent.FLAG_NO_CREATE) != null);
if (!alarmUp)
{*/
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + TIME,
TIME, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
/*}else Toast.makeText(MainActivity.this, "ALarm already set", Toast.LENGTH_SHORT).show();*/
}
就是这样!
答案 1 :(得分:0)
当代理关闭,网络连接中断或DNS问题时,UnknowHostException是一个非常通用的错误。检查进程正在运行的会话,并查看是否存在会话超时问题。