Android小部件从配置中退出

时间:2015-04-02 11:06:09

标签: android android-activity widget configure provider

我正在创建一个带有配置活动的小部件,我想知道如何根据用户输入的设置退出配置并更新小部件?有没有Android默认或我必须手动创建一个按钮退出?我现在有以下代码:

清单:

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

窗口小部件:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="250dp"
    android:minHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/widget_icon"
    android:initialLayout="@layout/widget_layout"
    android:configure="com.example.widget.WidgetConfigure"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen|keyguard">

</appwidget-provider>

1 个答案:

答案 0 :(得分:0)

查看此链接以在创建时更新您的小部件:Android: How to update widget text on creation

完成编辑配置活动后,尝试使用配置活动中的此方法更新窗口小部件:

 private void updateWidgetScreen(String updateData) {

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
        ComponentName thisWidget = new ComponentName(this, WidgetProvider.class);
        remoteViews.setTextViewText(R.id.kalanPara, updateData);

        Intent intent = new Intent(this, WidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, MainUtils.getWidgetID(prefs));
        intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        remoteViews.setRemoteAdapter(MainUtils.getWidgetID(prefs), R.id.list_view, intent);

        Intent clickIntent = new Intent(this, ConfigureActivity.class);
        PendingIntent clickPI = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setPendingIntentTemplate(R.id.list_view, clickPI);
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }

然后您可以使用以下代码退出活动:(但这不是必需的)

@Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }