0

我正在尝试在 appwidget 中制作一个简单的切换按钮。为此,我想保存当前状态,并且为此使用共享首选项..但我仍然遇到麻烦。

布尔值 isRecordting 始终为 false

这是关于接收的:

@Override
    public void onReceive(Context context, Intent intent)
    {
        switchToggle(context, intent);

        super.onReceive(context, intent);
    }

    private void switchToggle(Context context, Intent intent)
    {
        SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
        isRecording = prefs.getBoolean(IS_RECORDING, false);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
        Log.d(TAG,"Toggle State: " + isRecording);

        if (isRecording)
        {
            remoteViews.setImageViewResource(R.id.btnRecordToggle, R.drawable.record_button_enabled);
        }
        else
        {
            remoteViews.setImageViewResource(R.id.btnRecordToggle, R.drawable.record_button);
        }

        ComponentName componentName = new ComponentName(context, RecorderWidget.class); 
        AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);
        isRecording = !isRecording;
        prefs.edit().putBoolean(IS_RECORDING, isRecording);
    }

这是更新中:

@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        // TODO Auto-generated method stub
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for (int widgetId : appWidgetIds) 
        {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);

            // Register an onClickListener
            Intent intent = new Intent(context, RecorderWidget.class);
            intent.setAction(ACTION_START_RECORDING);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.btnRecordToggle, pendingIntent);


            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
4

1 回答 1

1

发现问题了!!!..

我需要向我的 Sherde 首选项支付费用……对不起,大家都知道了……

这是固定代码:

    private void switchToggle(Context context, Intent intent)
        {
            SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
            isRecording = prefs.getBoolean(IS_RECORDING, false);
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget);
            Log.d(TAG,"Toggle State: " + isRecording);

            if (isRecording)
            {
                remoteViews.setImageViewResource(R.id.btnRecordToggle, R.drawable.record_button_enabled);
            }
            else
            {
                remoteViews.setImageViewResource(R.id.btnRecordToggle, R.drawable.record_button);
            }

            ComponentName componentName = new ComponentName(context, LifeRecorderWidget.class); 
            AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteViews);
            isRecording = !isRecording;

           //THIS IS THE FIX
            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean(IS_RECORDING, isRecording);
            editor.commit();
        }
于 2012-06-02T13:02:01.623 回答