Configuration Activity不会更新App Widget

时间:2016-12-21 17:35:10

标签: android android-activity configuration android-appwidget

<{3}}上的“developers.android.com”说:

  

创建App Widget时,model.user = { firstName: 'John', lastName: 'Smith' }方法将不会被调用(系统在启动配置活动时不会发送ACTION_APPWIDGET_UPDATE广播)。首次创建App Widget时,配置Activity负责从AppWidgetManager请求更新。

然后在this page中显示了“从配置活动中更新App Widget”的示例。

所以我根据其指示(以及其他一些资源的帮助)创建了一个项目但是onUpdate()方法(以及onUpdate()方法)不会从配置活动

  • 预期结果(logcat窗口):
onReceive(...)
  • 真实结果(logcat窗口):

    1-首先(16:59:31):插入小部件后(问题在这里!)

        XXXX:: ConfigureActivity onCreate(...) method: Started.
               Expected occurring FIRST: when the widget has been inserted.
    
        XXXX:: ConfigureActivity: Finished.
               Expected occurring LAST: when OK button has been clicked.
    
        XXXX:: AppWidgetProvider onUpdate(...) method: Started.
               Expected occurring LAST: when OK button has been clicked.
    

    2- First(16:59:31):插入小部件时:

    12-21 16:59:31.688 I/XXXX: AppWidgetProvider onUpdate(...) method: Started.
                        Expected occurring LAST: when OK button has been clicked.
    

    3-最后(17:00:52):单击确定按钮时:

    12-21 16:59:31.743 I/XXXX: ConfigureActivity onCreate(...) method: Started.
                        Expected occurring FIRST: when the widget has been inserted.
    

    有关实际结果的详细信息,请参阅结尾。

这是我的来源:

  • 的AndroidManifest.xml
12-21 17:00:52.063 I/XXXX: ConfigureActivity: Finished.
                    Expected occurring LAST: when OK button has been clicked.
  • RES / XML /的 example_appwidget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">

    <application>

        <activity
            android:name=".MainActivity">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="ExampleAppWidgetProvider">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/example_appwidget_info" />
        </receiver>

        <activity android:name=".ExampleAppWidgetConfigure">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
            </intent-filter>
        </activity>

    </application>

</manifest>
  • RES /布局/的 example_appwidget.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:configure="com.example.myapp.ExampleAppWidgetConfigure"
    android:initialLayout="@layout/example_appwidget"
    android:minHeight="40dp"
    android:minWidth="40dp"
    android:previewImage="@drawable/widget_preview"
    android:updatePeriodMillis="86400000">
</appwidget-provider>
  • ExampleAppWidgetProvider.java
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Click Me"/>

</RelativeLayout>
  • RES /布局/的 activity_widget_configuration.xml
...
public class ExampleAppWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.i("XXXX", "AppWidgetProvider onUpdate(...) method: Started.\n" +
                "Expected occurring LAST: when OK button has been clicked.");
        final int N = appWidgetIds.length;

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];

            // Create an Intent to launch ExampleActivity
            Intent intent = new Intent(context, MainActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

            // Get the layout for the App Widget and attach an on-click listener
            // to the button
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.example_appwidget);
            views.setOnClickPendingIntent(R.id.button, pendingIntent);

            // Tell the AppWidgetManager to perform an update on the current app widget
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("XXX", "AppWidgetProvider onReceive(...) method: Started: " + intent.getAction());
        super.onReceive(context, intent);
    }
}
  • ExampleAppWidgetConfigure.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/ok_btn"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK" />

</LinearLayout>

问题出在哪里?或者我错误地预料到了?

额外信息:

更准确实际结果

...
public class ExampleAppWidgetConfigure extends Activity {
    private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i("XXXX", "ConfigureActivity onCreate(...) method: Started.\n" +
                "Expected occurring FIRST: when the widget has been inserted.");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_widget_configuration);

        final Button OK_btn = (Button) findViewById(R.id.ok_btn);

        OK_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Context context = getApplicationContext();

                // 1. First, get the App Widget ID from the Intent that launched the Activity:
                Intent intent = getIntent();
                Bundle extras = intent.getExtras();
                if (extras != null) {
                    mAppWidgetId = extras.getInt(
                            AppWidgetManager.EXTRA_APPWIDGET_ID,
                            AppWidgetManager.INVALID_APPWIDGET_ID);
                }

                // 2. Perform your App Widget configuration:
                    /* Nothing. */

                // 3. When the configuration is complete, get an instance of the AppWidgetManager
                //    by calling getInstance(Context):
                AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

                // 4. Update the App Widget with a RemoteViews layout by calling
                //    updateAppWidget(int, RemoteViews):
                RemoteViews views = new RemoteViews(context.getPackageName(),
                        R.layout.example_appwidget);
                appWidgetManager.updateAppWidget(mAppWidgetId, views);

                // 5. Finally, create the return Intent, set it with the Activity result,
                //    and finish the Activity:
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);

                Log.i("XXXX", "ConfigureActivity: Finished.\n" +
                        "Expected occurring LAST: when OK button has been clicked.");
                finish();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将此代码移到onClickListener之外。另外,验证mAppWidgetId不等于AppWidgetManager.INVALID_APPWIDGET_ID。

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

除非...假设您的配置活动实际上是在您添加小部件时打开...并且您的代码确实位于com.example.myapp包中...我不确定发生了什么。也许它与RemoteViews有关。而不是在OnClickListener中使用AppWidgetManager,只需像这样直接向接收者发送一个意图。

Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, ExampleAppWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {mAppWidgetId});
sendBroadcast(intent);

原始回答:

这与Guide非常相似。唯一有点奇怪的就是这一行

final Context context = getApplicationContext();

尝试删除该行并改为执行此操作

...
final Button OK_btn = (Button) findViewById(R.id.ok_btn);
final Context context = this;
OK_btn.setOnClickListener(new View.OnClickListener() {
...