设置活动后,小组件不显示在主屏幕中

时间:2013-09-22 12:29:47

标签: android configuration android-activity widget

我正在制作我的第一个小部件,我正在关注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();
        }
    }
   }

首选项活动,在create method上:

@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);
    }


}

首选项活动,对后退方法:

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

1 个答案:

答案 0 :(得分: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();
    }
}