Making a Toggle Button in an AppWidget

2019-06-09 15:20发布

I am trying to make a simple toggle button in an appwidget. to do that i want to save the current state and i am using the shared prefs for that .. and yet i still have trouble.

The value of the boolean isRecordting is alwasy false

here is the 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);
    }

Here is the 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条回答
beautiful°
2楼-- · 2019-06-09 15:51

Found the problem !!!..

i needed to commit the chages to my shearde prefs .. sorry to boder ya all ...

here is the fixed code:

    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();
        }
查看更多
登录 后发表回答