Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The ELAPSED_REALTIME approach could work I think, but strangly it is not for a small test app I created. Basically the test app is a widget and a IntentService. The widget only shows the current time, while the Intent Service gets the current time and sends a broadcast which the widget consumes in the onReceive() method and updates itself. The widget is of course a registered receiver for the custom broadcast action. In the onEnabled() method in the widget, I initialize the alarm for AlarmManager. Now some code:</p> <p><strong>Widget onEnabled():</strong></p> <pre><code>@Override public void onEnabled(Context c) { super.onEnabled(c); Intent intent = new Intent(c.getApplicationContext(), SimpleIntentService.class); PendingIntent intentExecuted = PendingIntent.getService(c.getApplicationContext(), 45451894, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) c.getApplicationContext().getSystemService(Context.ALARM_SERVICE); alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, 3000, intentExecuted); } </code></pre> <p><strong>Widget onReceive():</strong></p> <pre><code>@Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if (intent.getAction().equals(ACTION_RESP)) { AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); ComponentName thisWidget = new ComponentName(context.getApplicationContext(), Widget.class); int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget); for (int appWidgetId : allWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getApplicationContext().getPackageName(), R.layout.widget); String s = ""; if (intent.hasExtra("msg")) s = intent.getStringExtra("msg"); // Set the text remoteViews.setTextViewText(R.id.textView1, s); appWidgetManager.updateAppWidget(allWidgetIds, remoteViews); } } } </code></pre> <p><strong>SimpleIntentService onHandleIntent():</strong></p> <pre><code>@Override protected void onHandleIntent(Intent intent) { Log.w("TestService", "SimpleIntentService onHandleIntent called."); String msg = new Date().toGMTString(); Intent broadcastIntent = new Intent(); broadcastIntent.setAction(Widget.ACTION_RESP); broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); broadcastIntent.putExtra("msg", msg); sendBroadcast(broadcastIntent); } </code></pre> <p>So, I have tested this on my Nexus 4 running Android 4.2.1 and on an emulator with Android 4.0.4. In both cases the SimpleIntentService gets created and onHandleIntent() gets called every 3 seconds, even when I manually turn the display off. I have absolutely no idea why the AlarmManager still schedules the alarm, do you have?!</p> <p>Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload