Explain the creation of Widget and AppWidget gadgets in Android

  • 2021-06-28 13:52:07
  • OfStack

1. Concepts of Widget, App Widget, Web App

The concept of Widget was originally proposed by an Apple engineer named Rose in 1998 and was not officially known until 2003, but it was accepted and applied by countless large companies.Now we see that Widget is the widget in Dashboard that pops up when F4 is pressed on the Apple system and Gadget is the beautiful widget in the sidebar of Windows 7.In addition, there are yahoo Widget and other Widget products.One of their common features is that they use foreground Web technology (e.g. HTML, CSS, Javascript) to make gadgets and widgets.

In the Android system, almost every visualized View component is called Widget, which may have been named for fashion.

App Widget is an application widget that has existed since Android 1.5 and can be placed on the Android desktop.At this point, it looks like the sidebar gadget of windows, but unfortunately he does not use technology like HTML.Of course, App Widget is the hero of our talk. Originally he should be called Widget, or at least Gadget. Unfortunately, this name has already been occupied by his own system, so he has to rename it App Widget.

Finally, let's talk about Web App, or Android Web Application, which may be more accurate than mobile web application.We find that there are many platforms for smart machine systems, such as iOS, Android, Windows Phone, WebOS, BlackBerry and so on. They use different technical frameworks. Is there a way to write a program that can run on each system?The answer is yes, write an application based on Webkit browser.Web Application written by us using HTML5, CSS3, JavaScript, WebKit and other technologies may be the next major trend.I have the opportunity to talk about the development of Android Web Application.

2. Create a simplest Widget

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 Processing ) --> 
   * <intent-filter> 
   * <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> <!--  Designated  --> 
   * </intent-filter> 
   * <meta-data android:name="android.appwidget.provider"android:resource="@xml/hello_widget_provider" /> <!--  For the one specified above widget --> 
  *  </receiver> 
  </application> 
</manifest> 

4) HelloWidgetProvider.java


public class HelloWidgetProvider extends AppWidgetProvider {   
 * /** Called when the activity is first created. */ 
 * @Override 
 * public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
 * Timer timer = new Timer(); 
 * timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000); 
 * } 

 * 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(new Date())); 
 * appWidgetManager.updateAppWidget(thisWidget, remoteViews); 
 * } 
 * } 
} * 

Code explanation: RemoteView is used to describe an view that is displayed across processes, that is, the view is displayed in another process.It inflate is in the layout resource file.It also provides a few simple basic operations that can modify the contents of view.

AppWidget - - RemoteView, AppWidgetProvider is an BrocaseReceiver, only accept Enable, Update, disale, delete these message, the real interface is AppWidgetHostView (this is achieved in Launcher), which is communicated through RemoteView.Tell Launcher what you want AppWidget to look like through RemoteView.


Related articles: