The Appwidget advanced effect _android of Android learning

Source: Internet
Author: User
Tags stub

Then appwidget Basic Learning, today is a "advanced version" of the small example, used to test their own learning effects. So I made a roll of the dice widget.

Convenient for everyone to watch, the first screenshot is as follows:


Note that in the Drawable folder there are several pictures, I was downloaded on the Internet of other people's material.

Now let's start our study tour.

First step:
is to create a folder named XML in the res/directory (in fact, the name is random, do not have to adhere to this name), and then create a appwidget_info.xml file inside, its role is to declare to the system.

<appwidget-provider
  xmlns:android= "http://schemas.android.com/apk/res/android"
  android:minheight= " 72DP "
  android:minwidth=" 294DP "
  android:updateperiodmillis=" 86400000 "
  android:initiallayout=" @ Layout/app_widget_layout "
  >
</appwidget-provider>

Step Two:
To set the layout interface, I set the following App_widget_layout.xml file:

<?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" >

  <imageview 
    android:id= "@+id/imageview_widget"
    Match_parent "
    android:layout_height=" wrap_content "
    android:src=" @drawable/ic_launcher "
    />
  <button 
    android:id= "@+id/button_widget"
    android:layout_width= "Match_parent"
    android: layout_height= "Wrap_content"
    android:gravity= "center"
    android:text= "Shake"
    android:textcolor= "# 6663c6 "
    />


</LinearLayout>

Step Three:
Create a class that supports widgets to complete the next logical operation, such as the Widgetprovider.java below.

Package com.summer.mywidget;
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.util.Log;

Import Android.widget.RemoteViews; public class Widgetprovider extends appwidgetprovider{private static final String my_update_action= "Com.summer.APP_WID

  Get_action "; * * Random to get a picture of the int value, for the next transformation of the image of the foundation * * private static int getrandompicture () {int[] picturearray=new int[]{r.drawabl
    E.dice_1,r.drawable.dice_2,r.drawable.dice_3, r.drawable.dice_4,r.drawable.dice_5,r.drawable.dice_6};

    int randomnumber= (int) ((Math.random () *100)%6);
  return Picturearray[randomnumber]; * * * is used to receive action, branching is an effective way to differentiate between custom action and system action * * @Override public void onreceive, Intent inten
    T) {//TODO auto-generated method Stub String responseaction=intent.getaction (); LOG.I ("Summer", "------------->>>>>>>>>>>>>>>>>>>>>

    >>>>>>>> "+responseaction); if (My_update_action.equals (responseaction)) {remoteviews remoteviews=new remoteviews (),

      R.layout.app_widget_layout);

      Remoteviews.setimageviewresource (R.id.imageview_widget,getrandompicture ());
      Appwidgetmanager appwidgetmanager=appwidgetmanager.getinstance (context);
      ComponentName componentname=new componentname (context,widgetprovider.class);
    Appwidgetmanager.updateappwidget (ComponentName, remoteviews);
    }else{super.onreceive (context, intent); }/* * A loop is used to respond to the addition of several such widgets on the desktop * * @Override public void onUpdate (context, Appwidgetmanage R Appwidgetmanager, int[] appwidgetids) {//TODO auto-generated method stub for (int i=0;i<appwidgetids.
      length;i++) {Intent intent=new Intent (); Intent.setaction (My_updatE_action);

      Pendingintent Pendingintent=pendingintent.getbroadcast (context,-1, intent, 0);
      Remoteviews remoteviews=new remoteviews (Context.getpackagename (), r.layout.app_widget_layout);
      Remoteviews.setonclickpendingintent (r.id.button_widget,pendingintent);
    Appwidgetmanager.updateappwidget (Appwidgetids[i], remoteviews);
  } super.onupdate (Context, Appwidgetmanager, appwidgetids); @Override public void ondeleted (context context, int[] appwidgetids) {//TODO auto-generated method stub s
    Uper.ondeleted (context, appwidgetids);
  System.out.println ("My app widget----------------------------->>>>>>>ondeleted"); @Override public void onenabled (context context) {//TODO auto-generated the method stub System.out.println ("
    My app widget----------------------------->>>>>>>onenabled ");
  super.onenabled (context); @Override public void ondisabled (context context) {//TODO auto-generated method Stub System.out.println ("The My app widget----------------------------->>>>>>>
    Ondisabled ");
  super.ondisabled (context);

 }


}

  Fourth:
The declaration of related items in the manifest file Manifest.xml file. Details are as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.summer.mywidget "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk droid:minsdkversion= "8" android:targetsdkversion= "a"/> <application android:allowbackup= "true" Andr oid:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <act Ivity android:name= "com.summer.mywidget.MainActivity" android:label= "@string/app_name" > <intent-f ilter> <action android:name= "Android.intent.action.MAIN"/> <category "android:name=" Ntent.category.LAUNCHER "/> </intent-filter> </activity> <receiver android:name=" c Om.summer.mywidget.WidgetProvider "> <intent-filter > <action android:name=" android.appwidget.act Ion. Appwidget_update "/> </intent-filter> <intent-filter > <action android:name= "Com.summer.APP_WIDGET_ACTION" ></act ion> </intent-filter> <meta-data android:name= "Android.appwidget.provider" Android : resource= "@xml/appwidget_info" > </meta-data> </receiver> </application> &LT;/MANIFEST&G

 T

Fifth Step:
Done, run the code, and then manually add App_widget, then you can have a "dice" slightly.

Summary:

This small program, although implemented, is still not more complex. Need to have a certain understanding of the knowledge of broadcast news and so on.
Improved direction: Add animation to the completion of each click event to gain a better user experience. (Requires the help of the relevant methods within Remoteviews).
The code will inevitably appear some errors and deficiencies, I hope that the vast number of Bo friends to see after the point, I hope to progress with you!

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.