Implementing the AppWidgetProvider



Implementing the AppWidgetProvider is fairly straightforward.


Open Eclipse and open the Silent Mode Toggle application. Add a new class to the com.dummies.android.silentmodetoggle package and provide a name; I prefer to use AppWidget.java. To add a new class, right-click com.dummies.android.silentmodetoggle in the src/ folder and choose New➪Class. This opens the New Java Class dialog box.


In this dialog box, provide the name of the class and set its superclass to android.appwidget.AppWidgetProvider,


Click Finish when you are complete. A new class has been added to the selected package, and the code with the name you chose should now be visible. The AppWidgetProvider does all the work of responding to events from the RemoteView, but how so? If you look at the AppWidgetProvider Android documentation, you can see that it is a direct subclass of a BroadcastReceiver. At a high level, a BroadcastReceiver is a component that can receive broadcast messages from the Android system.


 


When a user taps a clickable view in the RemoteView on the home screen (such as a button), the Android system broadcasts a message informing the receiver that the view was clicked. The message is broadcast to a particular destination in the Android system. After the message is broadcast, the AppWidgetProvider can handle that message.


 


Note that these messages are broadcast, meaning that they are sent systemwide. If the payload of the message and the destination address information are vague enough, various BroadcastReceiver objects might handle the message. The AppWidgetProvider I am building in this section will be addressed to a single destination. This is similar to walking into a room full of building contractors and asking whether any contractors in the room could do some work for you. Everyone would respond. This would be an example of a vague message address and payload. However, if you asked the same group for a small electronics electrician contractor by the name of Bob Smith, only one might respond (if he were there, of course). This is an example of a specifically addressed message with detailed address and payload information.

Editor: ankita Added on: 2013-03-01 12:36:25 Total View:282







Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---