2

欢迎大家

我希望当用户按下小部件时,小部件会自行停止,当用户再次按下时,小部件会打开他的功能

我正在使用这篇文章的解决方案在我的小部件上实现 onClick:Android 中的可点击小部件

该解决方案适用于 android 2.2 手机和 android 4.1 手机,但不适用于 android 2.3.6 的 Nexus One。我单击小部件,但没有任何反应...

这是我完整的小部件类:

        public class mWidgetProvider extends AppWidgetProvider{         
            public static String ACTION_WIDGET_CLICK = "ActionWidgetClick"; 
            public static final String ACTION_AUTO_UPDATE = "AUTO_UPDATE";

            DecimalFormatSymbols symbols;
            DecimalFormat tempDecimalFormat;
            int pMaxFreq=-1;
            String cCurrentFreq=null;
            int freq = 0;
            float usage =-1;
            BufferedReader freqBufferedReader;
            PowerManager pm;
            RemoteViews remoteViews;
            ComponentName thisWidget;   
            WidgetAlarm widgetAlarm;
            ActivityManager manager;

            @Override
            public void onDisabled(Context context) {
                super.onDisabled(context);
                context.stopService(new Intent(context, UpdateService.class));

                //stop alarm
                widgetAlarm = new WidgetAlarm(context.getApplicationContext());
                widgetAlarm.stopAlarm();

                System.out.println("XXXXX onDISABLED");
            }

            @Override
            public void onDeleted(Context context, int[] appWidgetIds) {
                super.onDeleted(context, appWidgetIds);

                System.out.println("XXXXX onDELETED");
            }

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

                context.startService(new Intent(context, UpdateService.class)); 

                remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                thisWidget = new ComponentName(context, mWidgetProvider.class );

                //preparamos el evento para cuando el usuario presiona en el widget, ese evento será recibido en onReceive()
                Intent active = new Intent(context, mWidgetProvider.class);
                active.setAction(ACTION_WIDGET_CLICK);
                PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
                remoteViews.setOnClickPendingIntent(R.id.fondo, actionPendingIntent);           

                AppWidgetManager.getInstance(context).updateAppWidget( thisWidget, remoteViews );       

                // start alarm
                widgetAlarm = new WidgetAlarm(context.getApplicationContext());
                widgetAlarm.startAlarm();

                System.out.println("XXXXX onUPDATE");       
            }       

            @Override
            public void onReceive(Context context, Intent intent){
                if (intent.getAction().equals(ACTION_WIDGET_CLICK)){
                    switchWidget(context);
                }else if(intent.getAction().equals(ACTION_AUTO_UPDATE)){

                    symbols = new DecimalFormatSymbols(Locale.GERMAN);
                    symbols.setDecimalSeparator('.');
                    tempDecimalFormat = new DecimalFormat("#.#", symbols);      

                    pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
                    remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                    thisWidget = new ComponentName(context, mWidgetProvider.class );

                    if (pm.isScreenOn()){               
                     /*
                                 here i do some work to update the textviews of the widget
                                 */

                        AppWidgetManager.getInstance(context).updateAppWidget( thisWidget, remoteViews );
                        System.out.println("XXXXX UPDATING WIDGET");
                    }
                }else{
                    super.onReceive(context, intent);
                }

                System.out.println("XXXXX onRECEIVE: "+intent.getAction());
            }

            @Override
            public void onEnabled(Context context) {
                super.onEnabled(context);           

                System.out.println("XXXXX onENABLED");
            }   

            public void switchWidget(Context context){
                symbols = new DecimalFormatSymbols(Locale.GERMAN);
                symbols.setDecimalSeparator('.');
                tempDecimalFormat = new DecimalFormat("#.#", symbols);      

                remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
                thisWidget = new ComponentName(context.getPackageName(), mWidgetProvider.class.getName());
                widgetAlarm = new WidgetAlarm(context.getApplicationContext());
                if (isMyServiceRunning(context)==true){ //si el widget está encendido la apagamos
                    context.stopService(new Intent(context, UpdateService.class));          
                    widgetAlarm.stopAlarm();
                    remoteViews.setTextViewText(R.id.freq,"HERE I CHANGE THE VALUE");
                }
                else{ //si el widget está apagado lo encendemos
                    context.startService(new Intent(context, UpdateService.class)); 
                    widgetAlarm.startAlarm();
                            remoteViews.setTextViewText(R.id.freq,"HERE I CHANGE THE VALUE");
                }
                AppWidgetManager.getInstance(context).updateAppWidget(thisWidget,remoteViews);
            }

            private boolean isMyServiceRunning(Context context) {
                manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
                    if (UpdateService.class.getName().equals(service.service.getClassName())) {
                        return true;
                    }
                }
                return false;
            }

            public void getpMaxFreq(){
                int freq = 0;
                try{            
                    //HERE I DO SOME WORK               
                }
                catch (Exception e){e.printStackTrace();}
            }
        }

PD:我也尝试将意图放入onEnabled()函数中,结果相同......在 Nexus One 2.3.6 Android 版本上不起作用

问候

4

0 回答 0