Prevent launch onUpdate WidgetProvider

I am implementing a widget with the ConfigurationActivity function and should remain compatible with Eclair (2.1).

AppWidgetProviders documentationonUpdate indicates that:

... However, if you declared configuration activity, this method is not called when the user adds the App widget, but is called for subsequent updates. The configuration job performs the first update during configuration. (See the “Creating an Application Widget Customization Action” section below.)

Unfortunately, this is not true (at least for my Nexus S with JellyBean). Actually onUpdategets called before onCreate my ConfigurationActivity triggers. I want to know if other phones exhibit similar behavior, and if it is possible to prevent a call onUpdateinside my provider?

My workaround is to save the flag in SharedPreferences inside my WidgetConfigurationActivity with this particular AppWidgetId. If it is not there, I can assume that ConfigurationActivity was not called first. It works, but in my opinion it’s really terrible. If I can not prevent onUpdatefrom starting, is there a better solution?

+5
source share
3 answers

, onUpdate() . . :

http://developer.android.com/reference/android/appwidget/AppWidgetProvider.html

http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_UPDATE

, . , "updatePeriodMillis" XML Widget Info 0 .

- -. , , Receiver , . ( ), . , , . , , , .

, !

EDIT: onReceive() Receiver :

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    if( appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID )
    {
        super.onReceive(context, intent);
    }

}

Widgets, appWidgetId INVALID_APPWIDGET_ID, . "super.onReceive()" onUpdate() , onUpdate() .

+4

.

updatePeriodMillis 0. onUpdate , Widget. , Android.

0

I solved this problem by adding an action to the intent in onUpdate, then setting the if check onReceive to see if the widget was clicked:

I added intentClick.setAction(WIDGET_CLICKED);that only starts when you click on the widget.

public class WidgetProvider extends AppWidgetProvider {

    private static final String TAG = WidgetProvider.class.getSimpleName();
    private static String WIDGET_CLICKED = "widget_clicked";

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.e(TAG, "onUpdate()");

    remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
    watchWidget = new ComponentName(context, WidgetProvider.class);

    Intent intentClick = new Intent(context, WidgetProvider.class);
    intentClick.setAction(WIDGET_CLICKED);
    intentClick.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, "" + appWidgetIds[0]);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[0], intentClick, 0);
    remoteViews.setOnClickPendingIntent(R.id.img_btn, pendingIntent);
    remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.play);

    appWidgetManager.updateAppWidget(watchWidget, remoteViews);
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    Log.e(TAG, "onReceive()");
    Bundle extras = intent.getExtras();

    //If AND ONLY IF WIDGET_CLICKED
    if (extras != null && intent.getAction().equals(WIDGET_CLICKED)) {
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);

        //If isPlaying
        if (isPlaying) {
            //setBackground as PLAY
            remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.play);
            isPlaying = false;

            Intent i = new Intent(context, MediaPlayerService.class);
            i.putExtra("command", "stopSong");
            context.startService(i);

            //If NOT playing
        } else {
            //setBackground as STOP
            remoteViews.setInt(R.id.img_btn, "setBackgroundResource", R.drawable.stop);
            isPlaying = true;

            Intent i = new Intent(context, MediaPlayerService.class);
            i.putExtra("command", "playRandomSong");
            context.startService(i);
        }

        watchWidget = new ComponentName(context, WidgetProvider.class);

        (AppWidgetManager.getInstance(context)).updateAppWidget(watchWidget, remoteViews);
    }
}
0
source

All Articles