How to show an actitivity only when an app widget is first created?

后端 未结 2 1627
野趣味
野趣味 2021-01-16 04:43

My app widget needs to be configured when it first added to the home screen. I want to open a configuration view right after the user adds the widget.

AppWidge

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-16 04:46

    In the AppWidgetProvider XML file there is an attribute called android:configure

    You can use this to point to an Activity to launch when the app is dropped onto the home screen. ex:

    
    
    

    In your chosen configuration activity, the user can make whatever choice they want. You'll need to grab and store the widget's id. In the onCreate method, get the id like this:

    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null)
        mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    

    When they've made their selection, use this to send a message back to your home screen widget that it is ready to be displayed:

    Intent intent = new Intent();
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
    setResult(RESULT_OK, intent);
    
    finish();
    

    That'll get what you're after.

提交回复
热议问题