Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid widget is not calling service onStart
    text
    copied!<p>I'm quite new to android development and I want to create a widget for my application. Unfortunately I just can't get it working. I want my widget to start a service. The service should contain all the logic like updating textfields depending on data read of files of the internal storage. I have to update my widget every minute since I want to display a remaining time. For this I thought of an AlarmManager. </p> <p>I used this as a reference: <a href="http://www.helloandroid.com/tutorials/mastering-android-widget-development-part4" rel="nofollow">http://www.helloandroid.com/tutorials/mastering-android-widget-development-part4</a></p> <p>Heres what I tried so far: The AppWidgetProvider:</p> <pre><code>public class WidgetCtrl extends AppWidgetProvider { public static String WIDGET_UPDATE = "WIDGET_UPDATE"; public static int UPDATE_RATE = 60 * 1000; private SessionData sessionData = SessionData.getInstance(); private BaseCtrl baseCtrl = BaseCtrl.getInstance(); @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Service Intent Intent serviceIntent = new Intent(context, UpdateWidgetService.class); serviceIntent.setAction(UpdateWidgetService.UPDATE); PendingIntent servicePendingIntent = PendingIntent.getService(context, 0, serviceIntent, 0); // send Service intent try { servicePendingIntent.send(); } catch (CanceledException e) { // TODO Auto-generated catch block e.printStackTrace(); } // start alarm manager for all widget instances for (int appWidgetId : appWidgetIds) { setAlarm(context, appWidgetId, UPDATE_RATE); } super.onUpdate(context, appWidgetManager, appWidgetIds); } @Override public void onReceive(Context context, Intent intent) { if (WIDGET_UPDATE.equals(intent.getAction())) { Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show(); } super.onReceive(context, intent); } @Override public void onDisabled(Context context) { context.stopService(new Intent(context, UpdateWidgetService.class)); super.onDisabled(context); } public static void setAlarm(Context context, int appWidgetId, int updateRate) { Intent serviceIntent = new Intent(context, UpdateWidgetService.class); serviceIntent.setAction(UpdateWidgetService.UPDATE); PendingIntent servicePendingIntent = PendingIntent.getService(context, 0, serviceIntent, 0); AlarmManager alarms = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); if (updateRate &gt;= 0) { alarms.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), updateRate, servicePendingIntent); } else { // on a negative updateRate stop the refreshing alarms.cancel(servicePendingIntent); } } </code></pre> <p>}</p> <p>The Service:</p> <pre><code>public class UpdateWidgetService extends Service { public static String UPDATE = "update"; private SessionData sessionData = SessionData.getInstance(); private BaseCtrl baseCtrl = BaseCtrl.getInstance(); private Context ctx; @Override public void onStart(Intent intent, int startId) { ctx = getApplicationContext(); int appWidgetId = intent.getExtras().getInt( AppWidgetManager.EXTRA_APPWIDGET_ID); // TODO update information and display AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(ctx); int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS); ComponentName currWidget = new ComponentName(ctx, WidgetCtrl.class); RemoteViews remoteViews = new RemoteViews(ctx.getPackageName(), R.layout.widget); remoteViews.setTextViewText(R.id.widget_text, "test"); final Intent updateIntent = new Intent(ctx, UpdateWidgetService.class); final PendingIntent pending = PendingIntent.getService(ctx, 0, updateIntent, 0); final AlarmManager alarm = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); alarm.cancel(pending); long interval = 60; alarm.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),interval, pending); appWidgetManger.updateAppWidget(appWidgetId, remoteViews); super.onStart(intent, startId); } @Override public IBinder onBind(Intent arg0) { return null; } </code></pre> <p>}</p> <p>The problem I have is that it seems like the Service never gets called. I set breakpoints in the onStart method of the service but I never get there. Can someone help? </p> <p>Please also tell me if you suggest a different structure for my program. </p> <p><strong>EDIT</strong>: I forgot to add the manifest code. Between the application tags I have this code right behind the receiver object for the widget: </p> <pre><code>&lt;service android:name=".UpdateWidgetService"&gt;&lt;/service&gt; </code></pre> <p>Is this correct?</p> <p>I really hope someone can give me a solution or a hint.</p>
 

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