Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法将小部件id传递给配置活动_Android_Android Intent_Android Widget_Android Appwidget - Fatal编程技术网

Android 无法将小部件id传递给配置活动

Android 无法将小部件id传递给配置活动,android,android-intent,android-widget,android-appwidget,Android,Android Intent,Android Widget,Android Appwidget,我编写了一个具有配置活动的AppWidget(我使用了与应用程序本身相同的配置活动) 在调试模式下将小部件添加到主屏幕时,我将小部件id(使用put extra)传递给intent。当单击小部件本身(要加载prefs的活动,我会在onCreate方法处中断,在调用intent.getExtras或intent.getIntExtra的部分,我会得到null。 我想使用以下代码,但无法理解如何使用: : 问题是android使用PendingEvents进行缓存。解决方案是添加标志_UPDATE_

我编写了一个具有配置活动的AppWidget(我使用了与应用程序本身相同的配置活动) 在调试模式下将小部件添加到主屏幕时,我将小部件id(使用put extra)传递给intent。当单击小部件本身(要加载prefs的活动,我会在onCreate方法处中断,在调用intent.getExtras或intent.getIntExtra的部分,我会得到null。 我想使用以下代码,但无法理解如何使用: :

问题是android使用PendingEvents进行缓存。解决方案是添加标志_UPDATE_CURRENT标志,这会导致android更新缓存的PendingEvent

PendingEvent ConfigPendingEvent=PendingEvent.getActivity(上下文、请求代码、configIntent、PendingEvent.FLAG、更新当前)

这是我的密码:

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.dryrun" android:versionCode="3"
    android:versionName="1.1" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/ic_launcher_test"
        android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:debuggable="true"><!-- different< android:theme="@style/Theme.NoBackground" -->

        <!-- Main Activity -->
        <activity android:name=".MyActivity"
            android:configChanges="orientation"> <!--android:screenOrientation="portrait" -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Preferences -->

        <activity android:name=".Preferences.EditPreferences"
            android:configChanges="orientation">
             <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
        </activity>

        <!-- Widgets -->

        <!--  Widget-->
        <receiver android:name=".Widget.testWidget" android:label="@string/app_widget_">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <!--action
                    android:name="com.test.dryrun.Widget.testWidget.PREFENCES_WIDGET_CONFIGURE" /-->
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/test_widget__provider" />
        </receiver>
        <service android:name=".Widget.testWidget$WidgetService" />

        <uses-permission android:name="android.permission.BIND_REMOTEVIEWS"></uses-permission>
    </application>
</manifest>
序言课

public class EditPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        Intent intent = getIntent();
        m_extras = intent.getExtras();
        mAppWidgetId = intent.getIntExtra("widget_id", defaultVal);
    }
    private Bundle m_extras;

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
    {

        if(key.equals(getString(R.string.rlvntString)))
        {
                        Context ctx = getApplicationContext();
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
            setResult(RESULT_CANCELED);
            if (m_extras != null) 
            {
                mAppWidgetId = m_extras.getInt(
                        AppWidgetManager.EXTRA_APPWIDGET_ID, 
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                RemoteViews views = new RemoteViews(ctx.getPackageName(),
                        R.layout.test_widget);
                appWidgetManager.updateAppWidget(mAppWidgetId, views);
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);
                finish();
            }
            else
            {
                RemoteViews views = new RemoteViews(ctx.getPackageName(),
                        R.layout.test_widget);
                appWidgetManager.updateAppWidget(mAppWidgetId, views);
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);
                finish();
            }
        }
    }

我需要改变下一件事:

//old code-what you get in all the widget examples
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, runConfigtest, 0);
//new code - this is how you should write it
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, runConfigtest, PendingIntent.FLAG_UPDATE_CURRENT);

现在它起作用了

我缩短了我的课程,省去了不相关的东西。
public class EditPreferences extends PreferenceActivity implements OnSharedPreferenceChangeListener
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        Intent intent = getIntent();
        m_extras = intent.getExtras();
        mAppWidgetId = intent.getIntExtra("widget_id", defaultVal);
    }
    private Bundle m_extras;

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
    {

        if(key.equals(getString(R.string.rlvntString)))
        {
                        Context ctx = getApplicationContext();
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
            setResult(RESULT_CANCELED);
            if (m_extras != null) 
            {
                mAppWidgetId = m_extras.getInt(
                        AppWidgetManager.EXTRA_APPWIDGET_ID, 
                        AppWidgetManager.INVALID_APPWIDGET_ID);
                RemoteViews views = new RemoteViews(ctx.getPackageName(),
                        R.layout.test_widget);
                appWidgetManager.updateAppWidget(mAppWidgetId, views);
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);
                finish();
            }
            else
            {
                RemoteViews views = new RemoteViews(ctx.getPackageName(),
                        R.layout.test_widget);
                appWidgetManager.updateAppWidget(mAppWidgetId, views);
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
                setResult(RESULT_OK, resultValue);
                finish();
            }
        }
    }
//old code-what you get in all the widget examples
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, runConfigtest, 0);
//new code - this is how you should write it
PendingIntent runtestPendingIntent = PendingIntent.getActivity(context, 0, runConfigtest, PendingIntent.FLAG_UPDATE_CURRENT);