Appwidget realization of broadcasting mechanism

Source: Internet
Author: User

Package com.example.appwidget02;

Import com.example.appwidget02.r.drawable;

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.widget.RemoteViews;

public class appwidget02 extends appwidgetprovider{    private static final String my_action= " My.action.APPWIDGET_UPDATE ";//static global constants     @Override  public void OnReceive (context context, Intent Intent {  string action = intent.getaction ();   if (My_action.equals (action)) {    Remoteviews remoteviews = new Remoteviews (Context.getpackagename (), r.layout.example_appwidgit);    remoteviews.setimageviewresource (R.id.my_image, R.drawable.ic_launcher);    appwidgetmanager Appwidgetmanager = appwidgetmanager.getinstance (context);    componentname componentname = new ComponentName (context, appwidget02.class);    appwidgetmanager.updateappwidget (ComponentName, remoteviews);   }else{      super.onreceive (context, intent);  }  }     @Override  public void onUpdate (context context, Appwidgetmanager AppwidgetmanageR,    int[] appwidgetids) {  for (int i=0;i<appwidgetids.length;i++) {    Intent Intent = new Intent ();    intent.setaction (my_action);    pendingintent pendingintent = pendingintent.getbroadcast (context, 0, intent, 0);    remoteviews remoteviews = new Remoteviews (Context.getpackagename (), r.layout.example_appwidgit);    remoteviews.setonclickpendingintent (R.id.button1, pendingintent);    appwidgetmanager.updateappwidget (Appwidgetids[i], remoteviews);       }   super.onupdate (Context, Appwidgetmanager, appwidgetids);  }   @Override  public void onenabled (context context) {  //TODO auto-generated method Stub &nbs p; super.onenabled (context);  }   @Override  public void ondisabled (context context) {  //TODO auto-generated method Stub &NB sp; super.ondisabled (context);  }   @Override  public void ondeleted (context context, int[] appwidgetids) {  //TODO auto-generated method s Tub   super.ondeleted (context, appwidgetids);  }}

Configuration file

Mainfest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" com.example.appwidget02 "android:versioncode=" 1 "android:versionname=" 1.0 ">

<USES-SDK android:minsdkversion= "android:targetsdkversion="/>

    <application         android:allowbackup= "true"          android:icon= "@drawable/ic_launcher"          android:label= "@string/app_name"         android:theme= "@style/apptheme ">         <activity              android:name= ". Mainactivity "            android:label=" @string/app_name ">             <intent-filter>                  <action android:name= " Android.intent.action.MAIN "/>

                <category android: Name= "Android.intent.category.LAUNCHER"/>             </intent-filter>         </activity>         <receiver android:name= ". appwidget02" >              <intent-filter>                  <action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/>              </intent-filter>              <intent-filter>                  <action android:name= "My.action.APPWIDGET_UPDATE"/>              </intent-filter>              <meta-data                  android:name= "Android.appwidget.provider"                  android:resource= "@xml/appwidget_info"/>          </receiver>                             </application>

</manifest>

Xml/example.xml

<appwidget-provider xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:minwidth= "294DP"
android:minheight= "294DP"
Android:updateperiodmillis= "86400000"
android:initiallayout= "@layout/example_appwidgit"
>

</appwidget-provider>

Layout/example_appwidget.xml

<?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:orientation=" vertical ">    <textview         android:id= "@+id/textview01"        android:layout_width= "Fill_parent"        android:layout_height= "Wrap_content"        android:text= "First Widgit"        android:background= "#000000"        />       <button        android:id= "@+id/button1"        android:layout_width= "Fill_parent"         android:layout_height= "Wrap_content"        android:text="Click Test"       />    <imageview          android:id= "@+id/my_image"         android:layout_width= "Wrap_content"         android:layout_height= "Wrap_content"          android:layout_margin= "10dip"         android:src= "@drawable/yes"         />

</LinearLayout>

Appwidget realization of broadcasting mechanism

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.