窗口小部件onClick无法正常工作

时间:2013-02-26 23:09:40

标签: android android-intent android-widget widget android-pendingintent

惠康全部

我希望当用户按下窗口小部件时,窗口小部件会自行停止,当用户再次按下时,窗口小部件会显示其功能

我正在使用此帖子的解决方案实现onClick on my widget:Clickable widgets in 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()函数中,结果相同......在带有2.3.6 Android版本的Nexus One上无效

问候

0 个答案:

没有答案