Android OnClickPendingIntent ( widget -> service ) firing by itself
<p>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.</p>

<p>widget:</p>

<pre><code>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);
    }


}
</code></pre>

<p>service:</p>

<pre><code>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 &gt; 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;
    }
}
</code></pre>

<p>Thanks!</p>
