Roger Travis
Roger Travis

Reputation: 8538

Android OnClickPendingIntent ( widget -> service ) firing by itself

I've ran into a pretty interesting problem - I have a widget that when pressed, would fire a OnClickPendingIntent to invoke a service that would play a sound. But from time to time, about one an hour, it would fire by itself.

widget:

public class WidgetActivity extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        Intent intent = new Intent(context.getApplicationContext(), SilentService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

        PendingIntent pendingIntent = PendingIntent.getService(
                context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.layout, pendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

        context.startService(intent);
    }


}

service:

public class SilentService extends Service {
    @Override
    public void onStart(Intent intent, int startId) {

        Log.e("UpdateWidgetService", "Called");

        final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());

        int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

        if (appWidgetIds.length > 0) {
            for (final int widgetId : appWidgetIds) {

                final RemoteViews remoteViews = new RemoteViews(getPackageName(),   R.layout.widget_layout);

                remoteViews.setImageViewResource(R.id.ivc, R.drawable.pic1);
                 appWidgetManager.updateAppWidget(widgetId, remoteViews);

                MediaPlayer mpl = MediaPlayer.create(this, R.raw.music1);

                int intRand = random.nextInt(5)+1;

                Log.e("r",String.valueOf(intRand));

                mpl.start();

                mpl.setOnCompletionListener(new OnCompletionListener() {

                     public void onCompletion(MediaPlayer mp) {
                         remoteViews.setImageViewResource(R.id.ivc, R.drawable.pic2);
                         appWidgetManager.updateAppWidget(widgetId, remoteViews);
                     }
                    });


            }
            stopSelf();
        }
        super.onStart(intent, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Thanks!

Upvotes: 0

Views: 281

Answers (1)

Du3
Du3

Reputation: 1422

I have a feeling Android is killing your service and then your code is trying to start it again. I have seen similar issues. Give this a read: http://developer.android.com/guide/topics/fundamentals.html#proclife

When your service restarts after Android tries to get memory back, it is trying to start the service, which creates a new media player, and starts it back up. This is my assumption without knowing exactly how the code is setup, but based on what I see it's most likely the issue.

Upvotes: 1

Related Questions