Android:主屏幕小部件:使用广播意图?

时间:2014-05-26 14:20:43

标签: android widget broadcastreceiver

我正在尝试了解与主屏幕上的小部件通信的应用。但我不明白以下代码在应用程序中的作用:

           Intent i = new Intent(this, AppWidget.class);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
            updateViews.setOnClickPendingIntent(R.id.phoneState, pi);
            return updateViews;

全班:

public class AppWidget extends AppWidgetProvider {

//  This is called for every broadcast. We normally don't need to implement this 
//  method because the default AppWidgetProvider implementation filters all App Widget 
//  broadcasts and calls the above methods as appropriate
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() == null) {
            context.startService(new Intent(context, ToggleService.class));
        } else {
            super.onReceive(context, intent);
        }
    }

    @Override
//  This is called to update the App Widget at intervals defined by 
//  the updatePeriodMillis attribute in the AppWidgetProviderInfo. This method is also called when 
//  the user adds the App Widget, so it should perform the essential setup, such as define event 
//  handlers for Views and start a temporary Service, if necessary. 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        context.startService(new Intent(context, ToggleService.class));
    }


//  This class is used to set up the intent service in order to provide views for 
//  the widget. It also supports to set up a pending intent. Furthermore, the app widget can be 
//  updated with a remote adapter.
    public static class ToggleService extends IntentService {
        public ToggleService() {
            super("AppWidget$ToggleService");
        }

        @Override
        protected void onHandleIntent(Intent intent) {
            ComponentName me = new ComponentName(this, AppWidget.class);
            AppWidgetManager mgr = AppWidgetManager.getInstance(this);
            mgr.updateAppWidget(me, buildUpdate(this));
        }

        private RemoteViews buildUpdate(Context context) {
            RemoteViews updateViews = new RemoteViews(context.getPackageName(),
                    R.layout.widget);
            AudioManager audioManager = (AudioManager) context
                    .getSystemService(Activity.AUDIO_SERVICE);
            if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
                updateViews.setImageViewResource(R.id.phoneState,
                        R.drawable.phone_state_normal);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            } else {
                updateViews.setImageViewResource(R.id.phoneState,
                        R.drawable.phone_state_silent);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            }
            //KNOW THIS CODE
            Intent i = new Intent(this, AppWidget.class);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
            updateViews.setOnClickPendingIntent(R.id.phoneState, pi);
            return updateViews;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

当用户点击viewId ToggleService时,它只会打开R.id.phoneState

这有点愚蠢的做法,更直接的是:

Intent i = new Intent(this, ToggleService.class);
PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
updateViews.setOnClickPendingIntent(R.id.phoneState, pi);

你看,你发布的这段代码调用BroadcastReceiver,然后调用该服务。毫无意义。