2013-09-22 102 views
1

我正在處理我的第一個小部件,我正在關注Android developers tutorial。最初,小部件運行良好。它出現在窗口小部件列表中,並將其拖動到主屏幕,並出現在主屏幕中。設置活動後,主屏幕中未顯示小部件

但我需要一個配置活動來配置一些參數,並且我使用了PreferenceActivity。再次,窗口小部件將出現在窗口小部件列表中,並且在將其拖動到主屏幕後,會顯示PreferenceActivity。但是當我退出PreferenceActivity(使用後退按鈕)時,小部件不會出現在主屏幕上。

我通過互聯網搜索,並遵循一些建議(添加主要活動和其他),但這些解決方案並沒有爲我工作。

以下是我的代碼。首先是Android清單。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.my_widget" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="10" 
     android:targetSdkVersion="16" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.my_widget.Main" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Preferences" 
      android:label="@string/title_activity_preferences" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> 
      </intent-filter> 
     </activity> 
     <receiver android:name="com.example.my_widget.My_AppWidgetProvider" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/appwidget_info" /> 
     </receiver> 
    </application> 
</manifest> 

微件服務提供商:

 public class My_AppWidgetProvider extends AppWidgetProvider { 
    private Context context; 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
     final int N = appWidgetIds.length; 
     this.context = context; 
     try { 

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

       int appWidgetId = appWidgetIds[i]; 
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); 
        int number = (new Random().nextInt(100)); 

        // Set the text 
        remoteViews.setTextViewText(R.id.update, ""+number); 


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

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
    } 

偏好活性,對創建方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Intent intent = getIntent(); 
    Bundle extras = intent.getExtras(); 
    if (extras != null) { 
     mAppWidgetId = extras.getInt(
       AppWidgetManager.EXTRA_APPWIDGET_ID, 
       AppWidgetManager.INVALID_APPWIDGET_ID); 
    } 


} 

偏好活性,對backpressed方法:

@Override 
public void onBackPressed() { 
    super.onBackPressed(); 
    do_widget(); 
} 
private void do_widget() { 
    try { 
     AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext()); 
     RemoteViews views = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.widget); 
     appWidgetManager.updateAppWidget(mAppWidgetId, views); 
     Intent resultValue = new Intent(); 
     resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 
     setResult(RESULT_OK, resultValue); 
     finish(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

小部件xml信息文件:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 

    android:configure="com.example.my_widget.Preferences" 
    android:initialKeyguardLayout="@layout/widget" 
    android:initialLayout="@layout/widget" 
    android:minHeight="72dp" 
    android:minWidth="300dp" 
    android:previewImage="@drawable/ic_launcher" 
    android:resizeMode="horizontal|vertical" 
    android:updatePeriodMillis="6000" 
    android:widgetCategory="home_screen|keyguard"> 

    </appwidget-provider> 

回答

2

我設法自己解決它。問題出在onBackPressed方法中。替換爲:

@Override 
public void onBackPressed() { 
    //don't call super!!! 
    do_widget(); 
} 
private void do_widget() { 
    try { 
     Intent resultValue = new Intent(); 
     resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId); 
     setResult(RESULT_OK, resultValue); 
     finish(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
}