从Activity手动更改窗口小部件状态

时间:2012-07-19 10:58:41

标签: android android-widget

我的应用程序有一个小工具,当我们点击它时点亮LED闪光灯并在我们再次点击它时将其关闭。

以下是代码(感谢Kartik来自post):

WidgetProvider.java

public class WidgetProvider extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {

        Intent receiver = new Intent(context, WidgetReceiver.class);
        receiver.setAction("COM_FLASHLIGHT");
        receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                receiver, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget);

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

        appWidgetManager.updateAppWidget(appWidgetIds, views);

    }
}

WidgetReceiver.java

public class WidgetReceiver extends BroadcastReceiver {
    public static boolean isLightOn = false;
    public static Camera camera;

    @Override
    public void onReceive(Context context, Intent intent) {
        RemoteViews views = new RemoteViews(context.getPackageName(),
                R.layout.widget);

        if (isLightOn) {
            views.setImageViewResource(R.id.imageButtonWidget,
                    R.drawable.widget_lamp_button_default);
        } else {
            views.setImageViewResource(R.id.imageButtonWidget,
                    R.drawable.widget_lamp_button_checked);
        }


        AppWidgetManager appWidgetManager = AppWidgetManager
                .getInstance(context);
        appWidgetManager.updateAppWidget(new ComponentName(context,
                WidgetProvider.class), views);


        if (isLightOn) {
            if (camera != null) {
                camera.stopPreview();
                camera.release();
                camera = null;
            }
            isLightOn = false;


        } else {
            // Open the default i.e. the first rear facing camera.
            camera = Camera.open();
            if (camera == null) {
                Toast.makeText(context, "R.string.no_camera",
                        Toast.LENGTH_SHORT).show();

            } else {
                // Set the torch flash mode
                Parameters param = camera.getParameters();
                param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                try {
                    camera.setParameters(param);
                    camera.startPreview();
                    isLightOn = true;
                } catch (Exception e) {
                    Toast.makeText(context, "R.string.no_flash",
                            Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

现在我想从我的应用内部切换小部件。在我的应用程序的主要活动中使用此代码,我可以释放小部件拍摄的相机:

MainActivity.java

//Stop widget camera
        if (WidgetReceiver.isLightOn){
        Camera a = WidgetReceiver.camera;
        a.stopPreview();
        a.release();
        a = null;
        WidgetReceiver.isLightOn=false;}

但问题是小部件仍设置为已检查的drawable(R.drawable.widget_lamp_button_checked)。因此,FlashLight已关闭,但我仍然需要强制小部件将其drawable设置为未选中的(R.drawable.widget_lamp_button_default)。

我该怎么做?

修改:解决了问题

WidgetProvider.java

   public class WidgetProvider extends AppWidgetProvider {

        @Override
        public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                int[] appWidgetIds) {

             // Set widget's drawable to unchecked

            RemoteViews views2 = new RemoteViews(context.getPackageName(),
                    R.layout.widget);
            AppWidgetManager mManager = AppWidgetManager.getInstance(MainActivity
                    .getContext());
            ComponentName cn = new ComponentName(MainActivity.getContext(),
                    WidgetProvider.class);
            views2.setImageViewResource(R.id.imageButtonWidget,
                    R.drawable.widget_lamp_button_default);
            mManager.updateAppWidget(cn, views2);


            // Widget OnClick Behavior

            Intent receiver = new Intent(context, WidgetReceiver.class);
            receiver.setAction("COM_FLASHLIGHT");
            receiver.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                    receiver, 0);

            RemoteViews views = new RemoteViews(context.getPackageName(),
                    R.layout.widget);

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

            appWidgetManager.updateAppWidget(appWidgetIds, views);

        }
    }

WidgetReceiver.java - >保持不变

MainActivity.java

    public class MainActivity extends Activity {
        private static Context mContext;

        public static Context getContext() {
            return mContext;
        }


       @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mContext = this;


                   //Stop widget camera

                    if (WidgetReceiver.isLightOn){
                    Camera a = WidgetReceiver.camera;
                    a.stopPreview();
                    a.release();
                    a = null;
                    WidgetReceiver.isLightOn=false;}


        // Fire Widget's update with Intent 

        Intent intent = new Intent(this, WidgetProvider.class);
                intent.setAction("android.appwidget.action.APPWIDGET_UPDATE");
                // Use an array and EXTRA_APPWIDGET_IDS instead of
                // AppWidgetManager.EXTRA_APPWIDGET_ID,
                // since it seems the onUpdate() is only fired on that:
                int[] ids = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids);
                sendBroadcast(intent);

}

1 个答案:

答案 0 :(得分:0)

向AppWidgetProvider发送广播,并使用updateAppWidget类的AppWidgetManager方法在AppWidgetProvider的onReceive()方法中更新窗口小部件:

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


    ...
    views = new RemoteViews(context.getPackageName(),
    R.layout.yourwidgetlayout);
    AppWidgetManager mManager = AppWidgetManager.getInstance(App
    .getContext());
    ComponentName cn = new ComponentName(App.getContext(),
    YourAppWidgetProvider.class);
    //change your views,here I change text of text view witch it's id is "widgettextview" 
    views.setTextViewText(R.id.widgettextview, "lastWord");
    mManager.updateAppWidget(cn, views);
    ...
}     

这是App定义:

public class App extends Application implements OnInitListener {

    private static Context mContext;

    public void onCreate() {
        super.onCreate();
        mContext = this;
        }
    public static Context getContext() {
        return mContext;
    }
}