在AppWidget中制作切换按钮

时间:2012-06-02 12:53:03

标签: android android-widget

我正在尝试在appwidget中创建一个简单的切换按钮。 要做到这一点,我想保存当前状态,我正在使用共享的首选项... 然而我仍然有麻烦。

布尔值isRecordting的值是alwasy false

这是on recive:

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

这是On Update:

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

1 个答案:

答案 0 :(得分:1)

发现问题!!! ..

我需要将更改提交到我的共享首选项.. 对不起,所有人都是......

这是固定代码:

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