Android:appwidget's Tabletop small flashlight

Source: Internet
Author: User

Many of the controls in Android development are widget classes, but the widgets we often refer to are appwidget, which are widgets that can be placed on the desktop.

Here are two examples of how this appwidget to be used.

Example one: basic use of Appwidget

① Create a appwidget layout file Appwidget_layout.xml

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent ">    <Button        android:id= "@+id/button"        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:text= "Play Tiger Tonight" >    </Button></LinearLayout></span>
② Create a new XML folder under the Res folder and add an XML file inside it Appwidgetprovider_info.xml
<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><appwidget-provider xmlns:android= "http://schemas.android.com/ Apk/res/android "    android:initiallayout=" @layout/appwidget_layout "   //Assign layout, this is the most important    android:minheight = "72DP"                     //min height    android:minwidth= "294DP"                     //min width    android:updateperiodmillis= "86400000" >      / /Refresh Time </appwidget-provider></span>
③ Create a new class to inherit Appwidgetprovider, overriding several important methods

<span style= "FONT-SIZE:14PX;" >package Com.example.d_appwidget;import Android.appwidget.appwidgetmanager;import Android.appwidget.appwidgetprovider;import Android.content.context;import Android.content.intent;public Class Appwidget extends Appwidgetprovider {@Overridepublic void onenabled (Context context) {//First add appwidget//TODO auto-ge Nerated method stubsuper.onenabled (context); SYSTEM.OUT.PRINTLN ("---> onenabled");} @Overridepublic void OnUpdate (context context, Appwidgetmanager Appwidgetmanager,//update appwidgetint[] appwidgetids) {// TODO auto-generated Method Stubsuper.onupdate (context, Appwidgetmanager, appwidgetids); SYSTEM.OUT.PRINTLN ("---> onUpdate");}  @Overridepublic void ondeleted (context context, int[] appwidgetids) {//delete appwidget//TODO auto-generated method Stubsuper.ondeleted (context, appwidgetids); SYSTEM.OUT.PRINTLN ("---> ondeleted");} @Overridepublic void ondisabled (Context context) {//delete all appwidget//TODO auto-geneRated method stubsuper.ondisabled (context); SYSTEM.OUT.PRINTLN ("---> ondisabled");} @Overridepublic void OnReceive (context context, Intent Intent) {//Receive broadcast, more important method//TODO auto-generated Metho D stubsuper.onreceive (context, intent); SYSTEM.OUT.PRINTLN ("---> onreceive");}} </span>
④ Registers Receiver in the Andoridmanifest file and adds it to the application node

<span style= "FONT-SIZE:14PX;" ><receiver android:name= "Appwidget" > <intent-filter> <action android:name= "Android.appwidget. Action. Appwidget_update "> </action> </intent-filter> <meta-data android:name=" ANDROID.APPW Idget.provider "android:resource=" @xml/appwidgetprovider_info "/></receiver></span>

Result: You can see our custom widgets in the widget after running



To perform the add-delete operation, I understand that the system emits different broadcasts when performing different operations, then executes the appropriate method, and the system sends out a specific broadcast after execution.



Example two: Flashlight operation on the desktop, a lot of flashlights need to open the app and then turn off the lights, this is a bit of trouble, so after learning this appwidget I write this. It should be noted that the program itself and widgets belong to two processes, the use of the control and in the program itself is different, this is the most important point, that is, the Pendingintent and remoteviews written in the following two classes. The previous steps are roughly the same as the instance, and the main difference is the method overridden in the inheriting class.

① Create a new layout file Appwidget_layout.xml

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/button"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "torch-open"/></linearlayout></span>
Create a new XML folder under the ②res folder and create a new Appwidgetprovider_info.xml file

<span style= "FONT-SIZE:14PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><appwidget-provider xmlns:android= "http://schemas.android.com/ Apk/res/android "    android:initiallayout=" @layout/appwidget_layout "></appwidget-provider></span >
③ Create a new class to inherit Appwidgetprovider, overriding several important methods

<span style= "FONT-SIZE:14PX;" >package Com.example.e_appwidget_flashlight;import Android.app.pendingintent;import Android.appwidget.appwidgetmanager;import Android.appwidget.appwidgetprovider;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.hardware.camera;import Android.hardware.camera.parameters;import Android.widget.remoteviews;public Class     Appwidget extends Appwidgetprovider {private static final String Action_tag = "Leelit.action.flash"; Outgoing broadcast private static Boolean Flashon = False;private static Camera camera;private static Camera.parameters parameters;@ overridepublic void OnUpdate (context context, Appwidgetmanager appwidgetmanager,int[] appwidgetids) {//TODO Auto-generated method Stubsuper.onupdate (context, Appwidgetmanager, appwidgetids); SYSTEM.OUT.PRINTLN ("---> onUpdate"); Intent Intent = new Intent (Action_tag); Pendingintent pendingintent = pendingintent.getbroadcast (context, 0,//The intention is to trigger under certain conditions, this intention isIssued intent, 0); Broadcast Remoteviews remoteviews = new Remoteviews (Context.getpackagename (),//Get Appwidget of all control objects R.layout.appwid  Get_layout); Remoteviews.setonclickpendingintent (R.id.button, pendingintent);      Here you can click Trigger above intent, bind to Button control Appwidgetmanager.updateappwidget (Appwidgetids, remoteviews); Perform update, the first parameter represents the part ID, usually a} @Overridepublic void OnReceive (context context, Intent Intent) {//TODO auto-generated Method Stubsuper.onreceive (context, intent); if (Intent.getaction (). Equals (Action_tag)) {//Click to issue a broadcast               , here you can receive remoteviews remoteviews = new Remoteviews (Context.getpackagename (), r.layout.appwidget_layout); Because you want to manipulate the Appwidget control, get the//rem again Oteviews and perform the corresponding steps Appwidgetmanager Appwidgetmanager = Appwidgetmanager. getinstance (context); ComponentName Provider = new ComponentName (context, appwidget.class); Flashon =!flashon;   Turn on or off the torch if (Flashon) {turnonlight (); Remoteviews.settextviewtext (R.id.button, "torch-off"); Change control state appwidgetmanager.updateappwidget (provider, remoteviews);} else {turnofflight (); Remoteviews.settextviewtext (R.id.button, "torch-on"); Appwidgetmanager.updateappwidget (provider, remoteviews);}}} private void Turnonlight () {camera = Camera.open ();p arameters = Camera.getparameters ();p Arameters.setflashmode (   Parameters.flash_mode_torch); camera.setparameters (Parameters); Camera.release (); The resource must be released here, otherwise the flashlight will not be turned off because the camera resource has been occupied}private void Turnofflight () {camera = Camera.open ();p arameters = Camera.getparameters ();p Arameters.setflashmode (Parameters.flash_mode_off); camera.setparameters (parameters); Camera.release ();}} </span>

④ registers receiver in the Andoridmanifest file and adds it to the application node. Add the appropriate permissions.

<span style= "FONT-SIZE:14PX;" ><receiver android:name= "Com.example.e_appwidget_flashlight. Appwidget ">    <intent-filter>        <action android:name=" Android.appwidget.action.APPWIDGET_UPDATE "/>    </intent-filter>    <intent-filter>        <action android:name=" Leelit.action.flash "/ >    <!--This broadcast is custom--    </intent-filter>    <meta-data        android:name= " Android.appwidget.provider "        android:resource=" @xml/appwidgetprovider_info "/>  <!--resource file-- </receiver></span>

Result: Add a widget to the desktop and click to turn the flashlight on or off.

There are some other details to see the upload demo.

Appwidget_flashlight.rar


Summary: Four steps: ① part layout, ② loading layout XML file, ③ inheriting Appwidgetprovider class, overriding method, ④ configuration andoridmanifest file




Android:appwidget's Tabletop small flashlight

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.