1

そこで、ウィジェットを作成し、トグル ボタンをシミュレートするために最善を尽くしました。Sense Widget はトグル ボタンをサポートしていません。imagebutton を使用しており、その状態に応じて画像を変更する予定です。

唯一の問題は、状態が保存されていないことです。その理由はわかりません。

package com.tdubstudios.beastmode;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetClass extends AppWidgetProvider{

private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";  
private RemoteViews views;  

public boolean beastmodeOn = false;

public void onUpdate(Context context, AppWidgetManager appWidgetManager,  
       int[] appWidgetIds) {  
   final int N = appWidgetIds.length;
   views = new RemoteViews("com.tdubstudios.beastmode", R.layout.widget_layout);

   // Perform this loop procedure for each App Widget that belongs to this  
   // provider  
   for (int i = 0; i < N; i++) {  
       int appWidgetId = appWidgetIds[i];  
       Intent intent = new Intent(context, WidgetClass.class);  
       intent.setAction(ACTION_WIDGET_RECEIVER);  
       PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);  

       views.setOnClickPendingIntent(R.id.widget_ib, pendingIntent);  

       // Tell the AppWidgetManager to perform an update on the current App  
      // Widget  
       appWidgetManager.updateAppWidget(appWidgetId, views);  
   }
}  

@Override  
public void onReceive(Context context, Intent intent) {  
     if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) { 
         if(beastmodeOn){
             beastmodeOn = false;
             Toast.makeText(context, "beastmode is now off", Toast.LENGTH_SHORT).show();
         }else{
             beastmodeOn = true;
             Toast.makeText(context, "beastmode is now ON", Toast.LENGTH_SHORT).show();
         }
     }  
     super.onReceive(context, intent);  
}  

@Override
public void onDeleted(Context context, int[] appWidgetIds) {
    super.onDeleted(context, appWidgetIds);
    Toast.makeText(context, "onDelete has been called in widgetClass", Toast.LENGTH_LONG).show();
}

}

何度ボタンを押しても「ビーストモードがオンになりました」というトーストが表示されます

助言がありますか?

ありがとうございました。

編集:

わかりましたので、このコードを追加しました:

Log.i("widget","BEFORE beastmode is: "+beastmodeOn);
             beastmodeOn = true;
             Toast.makeText(context, "beastmode is now ON", Toast.LENGTH_SHORT).show();
             Log.i("widget","AFTER beastmode is: "+beastmodeOn);

そして私のログは私にこれを返します:

 BEFORE beastmode is: false
 AFTER beastmode is: true

ボタンを押すたびにこれが表示されます。したがって、明らかに、新しいインスタンスまたはその効果をもたらす何かが作成されます。実行すると、すべての変数値または何かを破棄する必要があります。より知識のある人は正しい言葉を知っているかもしれません。

それで、誰かが回避策を知っていますか?

4

1 に答える 1

1

So I don't exactly know why the above didn't work, I'm assuming because it creates a new instance. I've managed to make a work around though by using SharedPreferences to keep track of the toggle. Probably not the best way to do it but it works, so I'm happy.

For anyone else having this problem here ya go:

package com.tdubstudios.beastmode;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetClass extends AppWidgetProvider {

    private static final String ACTION_WIDGET_RECEIVER = "ActionRecieverWidget";
    private RemoteViews views;

    public boolean beastmodeOn;

    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        final int N = appWidgetIds.length;
        views = new RemoteViews("com.tdubstudios.beastmode",
                R.layout.widget_layout);

        // Perform this loop procedure for each App Widget that belongs to this
        // provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = appWidgetIds[i];
            Intent intent = new Intent(context, WidgetClass.class);
            intent.setAction(ACTION_WIDGET_RECEIVER);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                    0, intent, 0);

            views.setOnClickPendingIntent(R.id.widget_ib, pendingIntent);

            SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            boolean value = prefs.getBoolean("beastmodeOn", false);

            if (value) {
                Editor editor = prefs.edit();
                editor.putBoolean("beastmodeOn", false);
                editor.commit();
            }

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {

            SharedPreferences prefs = PreferenceManager
                    .getDefaultSharedPreferences(context);
            boolean value = prefs.getBoolean("beastmodeOn", false);
            Editor editor = prefs.edit();

            if (value) {
                beastmodeOn = true;
            } else {
                beastmodeOn = false;
            }

            if (beastmodeOn) {
                Toast.makeText(context, "beastmode is now off",
                        Toast.LENGTH_SHORT).show();

                editor.putBoolean("beastmodeOn", false);
                editor.commit();
            } else {
                Toast.makeText(context, "beastmode is now ON",
                        Toast.LENGTH_SHORT).show();

                editor.putBoolean("beastmodeOn", true);
                editor.commit();
            }
        }
        super.onReceive(context, intent);
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(context);
        boolean value = prefs.getBoolean("beastmodeOn", false);

        if (value) {
            Editor editor = prefs.edit();
            editor.putBoolean("beastmodeOn", false);
            editor.commit();
        }

        super.onDeleted(context, appWidgetIds);
    }
}
于 2012-06-10T06:15:03.577 に答える