Widget for further use, using Appwidgetprovider to implement the clock pendant on the desktop

Source: Internet
Author: User

1, through the introduction of the basic application of the widget outside know that the system up to 30 minutes to update the contents of the pendant, in order to update the clock in real time we must send their own broadcast to achieve the update of the contents of the pendant, the method is to overwrite the parent class Appwidgetprovider OnReceive () method , and add the Intent-fliter to receive their broadcasts in the registered broadcast.

2, used to view Mainfest file know broadcast accept the original only registered Android.appwidget.action.APPWIDGET_UPDATE, but can receive Android.appwidget.action.APPWIDGET _enabled,android.appwidget.action.appwidget_disenabled, guess that the receiver in the Appwidgetprovider in the system has registered to receive these broadcasts, and I wrote the class inherit it, Therefore can also receive.

3, when I remove the registered Android.appwidget.action.APPWIDGET_UPDATE only keep my own added update time of the broadcast but not receive android.appwidget.action.APPWIDGET_ Update and so on some broadcast and in the widget can not find my definition of the pendant. Conjecture in the system defines the corresponding rules, must register the broadcast contains Android.appwidget.action.APPWIDGET_UPDATE, and the registered class must inherit the Appwidgetprovider to register a pendant in the system, and inherited classes can receive Appwidgetprovider-accepted broadcasts.

The following demo shows the implementation:

Widget class:

Package Com.example.user.appwidget;import Android.app.pendingintent;import Android.appwidget.AppWidgetManager; Import Android.appwidget.appwidgetprovider;import Android.content.context;import Android.content.intent;import Android.os.bundle;import Android.widget.remoteviews;import Android.widget.textview;import java.util.ArrayList; Import Java.util.calendar;import java.util.comparator;/** * Implementation of APP Widget functionality. */public class Newappwidget extends Appwidgetprovider {private final static String Action_updatetime = "Android.appwi    Dget.action.APPWIDGET_UPDATE ";    Private final static String action_enable = "Android.appwidget.action.APPWIDGET_ENABLED";    private static arraylist<integer> Widgetids = new arraylist<> ();    private static Boolean hasupdatethread = false;    Thread upwidget= null;        @Override public void OnReceive (context context, Intent Intent) {int size = Widgetids.size ();    int [] Widgetidarray = new int [size];    for (int i = 0; i < size; i++) {Widgetidarray[i] = Widgetids.get (i);        } onUpdate (Context, appwidgetmanager.getinstance (context), widgetidarray);        System.out.println ("received broadcast" + intent.getaction ());    Super.onreceive (context, intent); } static void Updateappwidget (context context, Appwidgetmanager Appwidgetmanager, int AP        Pwidgetid) {Calendar c = calendar.getinstance ();        int hour = C.get (Calendar.hour_of_day);        int minute = C.get (Calendar.minute);        int seconds = C.get (Calendar.second);        Charsequence Widgettext = "Time:" + hour + ":" + Minute + ":" + seconds;//context.getstring (r.string.appwidget_text); Construct the Remoteviews object remoteviews views = new Remoteviews (Context.getpackagename (), R.layout.new_        App_widget);        Views.settextviewtext (R.id.appwidget_text, Widgettext); Instruct the Widget Manager to update the widget APPWIdgetmanager.updateappwidget (Appwidgetid, views); }//arrive at the specified update time (specified in New_app_widget_info.xml, greater than 30 minutes is valid, less than 30 minutes will take 30 minutes to execute)//or add widget to the desktop when executing @Override public voi D onUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {//There may be multiple Widge TS Active, so update all of the them for (int appwidgetid:appwidgetids) {Updateappwidget (context,            Appwidgetmanager, Appwidgetid);                if (!widgetids.contains (Integer) appwidgetid)) {System.out.println ("does not contain a pendant" +appwidgetid);            Widgetids.add ((Integer) appwidgetid);        } else System.out.println ("contains pendant" +appwidgetid); } if (! Hasupdatethread) {System.out.println ("created a process");            Hasupdatethread =true;            Upwidget = new Updatewiget (context);        Upwidget.start ();      }}//When the first widget is created @Override public void onenabled (context context) {  Enter relevant functionality for if the first widget is created//System.out.println ("1111111111111111111111    111111111111111111111111 "); }//When the last widget instance is deleted, execute @Override public void ondisabled (context context) {//Enter relevant Functionalit        Y for while the last widget was disabled} class Updatewiget extends Thread {//public int widgetid;        public context context;            Public Updatewiget (Context context) {//This.widgetid = Widgetid;        This.context = context; } @Override public void Run () {while (true) {Intent Intent = new Intent ("Android                . Appwidget.action.APPWIDGET_UPDATETIME ");//Intent.putextra (" Wigetid ", Widgetid);                try {sleep (1000);                } catch (Interruptedexception e) {e.printstacktrace ();                } System.out.println ("sent a broadcast"); Context.sendbroaDcast (Intent); }        }    }}

Layout file for the pendant:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:background=" #09C "    android:padding=" @dimen/ Widget_margin ">    <textview        android:id=" @+id/appwidget_text "        android:layout_width=" Wrap_ Content "        android:layout_height=" wrap_content "        android:layout_centerhorizontal=" true "        Android: Layout_centervertical= "true"        android:layout_margin= "8DP"        android:background= "#09C"        android: contentdescription= "@string/appwidget_text"        android:text= "Ca"        android:textcolor= "#ffffff"        Android:textsize= "20DP"        android:textstyle= "Bold|italic"/></relativelayout>

Mainfset registered in the broadcast:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.user.appwidget "> <application android:allowbackup=" true "android:i con= "@mipmap/ic_launcher" android:label= "@string/app_name" android:supportsrtl= "true" Android:theme= " @style/apptheme "> <activity android:name=". Mainactivity "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/&G                T <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity&        Gt <receiver android:name= ". Newappwidget "> <intent-filter> <action android:name=" Android.appwidget.action.APPWID Get_update "/> <action android:name=" Android.appwidget.action.APPWIDGET_UPDATETIME "/> </      Intent-filter> <meta-data          Android:name= "Android.appwidget.provider" android:resource= "@xml/new_app_widget_info"/> </receiver> </application></manifest>

  

Widget for further use, using Appwidgetprovider to implement the clock pendant on the desktop

Related Article

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.