【问题标题】:Widget not placed on home screen after adding a preferences activity添加首选项活动后,小部件未放置在主屏幕上
【发布时间】:2014-02-20 19:28:54
【问题描述】:

将首选项活动添加到我的小部件后,我无法再将其添加到主屏幕。

当我将小部件放到屏幕上时,首选项活动按预期显示,我可以使用它,设置已存储,但关闭它的唯一方法是返回按钮,只要我按下它,我就会返回小部件选择屏幕,没有小部件添加到主屏幕。

关于该问题的documentation has only a very limited example 以及我能找到的关于此问题的每个示例或教程都已完全过时(对于 SDK 1.5 或类似版本),几乎不再适用。

我尝试将文档中的这些行添加到我的偏好活动的 onStop() 方法中,但它不会改变行为。

那么,如何正确关闭首选项活动,以便 WidgetManager 将小部件添加到主屏幕?

以下是相关文件:

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="18" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver android:name="de.schneidr.android.MyTchiboWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@xml/mytchibo_appwidget_info" />
        </receiver>

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

    </application>

</manifest>

appinfo_widget.xml:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="250dp"
    android:minHeight="40dp"
    android:minResizeWidth="250dp"
    android:minResizeHeight="40dp"
    android:updatePeriodMillis="86400000"
    android:previewImage="@drawable/tcm_logo"
    android:initialLayout="@layout/mytchibo_appwidget"
    android:resizeMode="none"
    android:configure="de.schneidr.android.MyTchiboWidgetConfigure" 
    android:widgetCategory="home_screen">
</appwidget-provider> 

MyTchiboWidgetConfigure.java

package de.schneidr.android;

import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
import de.schneidr.mytchibo.R;

public class MyTchiboWidgetConfigure extends Activity {

       @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Display the fragment as the main content.
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new MyTchiboWidgetConfigureFragment())
                    .commit();
        }

       protected void onStop() {
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();
            int mAppWidgetId = 0;
            if (extras != null) {
                mAppWidgetId = extras.getInt(
                        AppWidgetManager.EXTRA_APPWIDGET_ID, 
                        AppWidgetManager.INVALID_APPWIDGET_ID);
            }

            Context context = this.getApplicationContext();

            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.mytchibo_appwidget);
                    appWidgetManager.updateAppWidget(mAppWidgetId, views);

            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();
           super.onStop();
       }
}

【问题讨论】:

  • 查看错误日志。可能存在膨胀错误。尝试一个非常简单的布局,例如一个 TextView。
  • 没有错误。该小部件无需偏好活动即可完美运行。
  • @GeraldSchneider The page 您发布的内容包含您需要的所有示例。

标签: android android-widget android-preferences


【解决方案1】:

好的,我在this answer 中找到了解决方案。基本上,我在OnStop() 函数中拥有的内容属于onBackPressed()

public class MyTchiboWidgetConfigure extends Activity {
    private static String CONFIGURE_ACTION="android.appwidget.action.APPWIDGET_CONFIGURE";

    @Override
    public void onBackPressed() {
        if (CONFIGURE_ACTION.equals(getIntent().getAction())) {
            Intent intent = getIntent();
            Bundle extras = intent.getExtras();

            if (extras != null) {
                int id = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                AppWidgetManager mgr = AppWidgetManager.getInstance(this);
                RemoteViews views = new RemoteViews(getPackageName(),
                        R.layout.mytchibo_appwidget);

                mgr.updateAppWidget(id, views);

                Intent result = new Intent();

                result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
                setResult(RESULT_OK, result);
                sendBroadcast(new Intent(this, MyTchiboWidgetProvider.class));
            }
        }

        super.onBackPressed();
    }
}

解决方案直接取自这里: https://github.com/commonsguy/cw-advandroid/blob/b01438e7f0fed8f795ddec4be43066905f03d0cc/AppWidget/TwitterWidget/src/com/commonsware/android/appwidget/TWPrefs.java

【讨论】:

    【解决方案2】:

    好吧,不让配置(首选项)活动的启动模式“singleInstance”对我有用。 不知不觉加了这行

            android:launchMode="singleInstance"
    

    在我的活动清单中。我删除了该行,它起作用了。

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2011-04-25
      • 1970-01-01
      相关资源
      最近更新 更多