2015-04-02 45 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