Explain the creation of widgets and appwidget gadgets in Android _android

Source: Internet
Author: User

1.Widget, App Widget, Web app concept

The original concept of the widget was presented by an Apple engineer named Rose in 98 and was not officially known until 2003, but then numerous large companies began to accept and apply the idea. Now we see the gadget in the Apple system that presses the F4 pop-up Dashboard widget, and the nifty gadgets on the Windows 7 sidebar are called Gadget (widget variants), in addition to the Yahoo widget and other widget products. They have a common feature is the use of foreground web development technology (such as HTML, CSS, Javascript) to make small tools, widgets.

In the Android system, almost every visual view component is called a widget, and the name may have been a fad.

The app widget is from Android 1.5, which is the application team that can be placed on the Android desktop. His features look like Windows Sidebar gadgets, but unfortunately his adoption techniques are not HTML technology. Of course, the app widget is the protagonist of our story, originally he should be called the widget, at least to be called Gadget Bar, but the name has been his own system occupied, so had to change his name app Widget.

Finally, the Web App, or the Android Web application, may be called a mobile Web application more accurate. We found that there are many intelligent system platforms, such as iOS, Android, Windows Phone, WebOS, BlackBerry and so on, they use a different technical framework, there is no way to write a program on each system can run it? The answer is yes, write the application of WebKit based browsers. We use HTML5, CSS3, JavaScript, WebKit and other technologies to write the Web application may be a big trend in the future is also not sure AH. We have a chance to talk about the development of the Android Web application.

2. Create one of the simplest widgets

Code Case:

1) main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" 
  Fill_ Parent "> 
<textview android:id=" @+id/tvcurrtime "android:layout_width=" Wrap_content "Android:layout_ height= "Wrap_content" android:text= "@string/hello" 
  android:textcolor= "@color/black"/> 
</ Linearlayout> 

2) Hello_widget_provider.xml

<?xml version= "1.0" encoding= "Utf-8"?> <appwidget-provider xmlns:android= 
"http://schemas.android.com" /apk/res/android " 
android:minwidth=" 146dip "android:minheight=" 72dip "android:initiallayout=" @layout/main " > 
</appwidget-provider> 

3) Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= 
"http://schemas.android.com/apk/res/" Android "package=" Com.woody.testWidget "android:versioncode=" 1 " 
   android:versionname=" 1.0 "> 
  < Application android:icon= "@drawable/icon" android:label= "@string/app_name" > <receiver android:name= 
 ".    Hellowidgetprovider "android:label=" @string/app_name "> <!--hellowidgetprovider for that class (business process)--> 
  <intent-filter> 
  <action android:name= "Android.appwidget.action.APPWIDGET_UPDATE"/> <!--specified- > 
  </intent-filter> 
  <meta-data android:name= "Android.appwidget.provider" android:resource= "@ Xml/hello_widget_provider "/> <!--for the widgets specified above--> 
 </receiver> 
  </application> 
</manifest> 

4) Hellowidgetprovider.java

The public class Hellowidgetprovider extends Appwidgetprovider {/** called ' when the ' activity is ' is a. */@Ove  Rride public void OnUpdate (context context, Appwidgetmanager Appwidgetmanager, int[] appwidgetids) {Timer timer = 
    New Timer (); 
  Timer.scheduleatfixedrate (New MyTime (context, Appwidgetmanager), 1, 1000); 
    The public class MyTime extends TimerTask {remoteviews remoteviews; 
    Appwidgetmanager Appwidgetmanager; 
    ComponentName Thiswidget;   
    DateFormat format = simpledateformat.gettimeinstance (Simpledateformat.medium, Locale.getdefault ()); 
      Public MyTime (context context, Appwidgetmanager Appwidgetmanager) {this.appwidgetmanager = Appwidgetmanager; 
      Remoteviews = new Remoteviews (Context.getpackagename (), r.layout.main); 
    Thiswidget = new ComponentName (context, hellowidgetprovider.class); @Override public void Run () {Remoteviews.settextviewtext r.id.tvcurrtime, ' time = ' + Format.format (NE WDate ())); 
    Appwidgetmanager.updateappwidget (Thiswidget, remoteviews);  

 }   } }

Code explanation: RemoteView is used to describe a view that is displayed across processes, which means that the view is displayed in another process. It inflate to the layout resource file. It also provides some simple basics to modify the view content.

Appwidget---Remoteview,appwidgetprovider is a brocasereceiver, just accept to enable, update,disale,delete these message, And the real display of the interface is Appwidgethostview (which is implemented in launcher), the middle is through remoteview to communicate. Tell launcher by RemoteView what you want Appwidget to look like.

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.