0

I am trying to wake up my device to make a network request. When I set the alarm to a minute or two. It works perfectly. But when I set it to about 10 minutes and turn off my display, the alarm is raised, but volley throws a "NoConnectionError" as shown in the title.

I researched on it, I have implemented my receiver using a WakeFullBroadCastReceiver. After this, I was getting the same problem. I researched further and added a lock to my wifi. Still, I am getting the same error.

Please any help will be appreciated.

Receiver:

 @Override
public void onReceive (Context context, Intent intent) {
    Log.i("Receiver", "Called here");
    Intent serIntent1 = new Intent(context, FetchTodayWordService.class);
    startWakefulService(context, serIntent1);
}

Service

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

Setting Alarm:

 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();*/
}

Error Output:

04-29 23:48:23.125 15702-15702/com.example.clinton.light I/Receiver: Called here 04-29 23:48:23.145 15702-15702/com.example.clinton.light I/SERVICE: started 04-29 23:48:23.175 15702-15702/com.example.clinton.light I/OUTPUT: com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "api.wordnik.com": No address associated with hostname

Any help will really be appreciated. I might not be able to mark your answer, I don't have enough points.

1
  • 1
    Check your wifi advanced settings on your device and see if "Keep Wi-Fi on during sleep" is set to "always" Commented Apr 29, 2016 at 20:36

2 Answers 2

1

I realized that the network operation was successful only when the screen is on (When the device is awake). So I replaced the alarm type to

int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP; instead of :

int alarmType = AlarmManager.ELAPSED_REALTIME;

And that fixed it. No need to make any locks to wifi or use any sleeping threads.

So full AlarmCode is shown below for future reference: Please do not use the wifi lock shown in the question.

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();*/
}

And that's it!

Sign up to request clarification or add additional context in comments.

Comments

0

UnknowHostException is a very generic error for when either proxy is down, network connection is down or DNS issue. Check the session that the process is running and see could there be a session timeout issue.

16 Comments

Hi Minh, how do I check the session that the process is running? Can you elaborate a little. Still an amateur in these things...
I placed the url in my browser and refreshed about 20 times, none of them gave me a network problem
I am sorry I am not able to provide the url, you will need an API key to get it working. Which is secret :)
Your browser does have proxy settings configured. How are you running your application? If you start the application using some script, in UNIX, you can specify the user running the process.
Perhaps, wifi have yet fully available? Maybe after lock.acquire() method, loop and/or wait/sleep for a few seconds to ensure connection can be made before making the request?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.