2015-04-02 65 views
0

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

清单:

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

的Widget:

<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> 

回答

0

看看这个链接上创造更新你的widget: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(); 
    } 
+0

我已经想通了由自己,但thatnk你:) – qwertz 2015-04-03 14:59:51

+0

不客气:) – MiloRambaldi 2015-04-03 15:01:49