现在的位置: 首页 > 移动开发> 正文
Extending AIR for Android (3)
2011年07月02日 移动开发 暂无评论 ⁄ 被围观 2,735+

Widgets


Widgets in Android are the mini apps that can be displayed on the home screen of the device. There is a fairly limited amount of things that can be displayed in Widgets. So unfortunately Widgets can’t be built with AIR for Android. However a custom application Widget can be packaged with an AIR for Android application. To add a Widget to an AIR for Android application you can use the default AppEntry class instead of wrapping it with another class (MainApp in my example). (It doesn’t, however, do any harm to keep the MainApp class there.) To add a Widget simply add its definition to the AndroidManifest.xml file, create the Widget with Java, and create a corresponding layout resource.

1. First define the Widget in the application section of the AndroidManifest.xml file:

<receiver android:name=".AndroidWidget" android:label="app">
    <intent-filter>
         <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
   </intent-filter>
   <meta-data android:name="android.appwidget.provider" android:resource="@xml/airandroidwidget" />
</receiver>

2. You need an XML resource that provides metadata about the widget. Simply create a new file named airandroidwidget.xml in a new res/xml directory with the following contents:

<?xml version="1.0" encoding="utf-8"?>    
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
            android:minWidth="294dp"
            android:minHeight="72dp"
            android:updatePeriodMillis="86400000"
            android:initialLayout="@layout/main">
</appwidget-provider>

This tells the widget to use the main layout resource as the initial layout for the widget.

3. Create a res/layout/main.xml file that contains a simple text display:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                             android:id="@+id/widget"
                             android:orientation="vertical"
                             android:layout_width="fill_parent"
                             android:layout_height="fill_parent"
                             android:background="#ffffffff"    >
          <TextView  android:layout_width="fill_parent"
                             android:layout_height="wrap_content"
                             android:text="hello"    />
 </LinearLayout>

Next, you’ll need to create the AppWidgetProvider class specified in the AndroidManifest.xml file.
 

4. Create a new Java class named AndroidWidget with the following contents:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.jamesward;   
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import com.jamesward.MainApp;   
 
public class AndroidWidget extends AppWidgetProvider {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)     {        
     final int N = appWidgetIds.length;           
  // Perform this loop procedure for each App Widget that belongs to this provider        
  
  for (int i = 0; i < N; i++)  {            
     int appWidgetId = appWidgetIds[i];            
     Intent intent = new Intent(context, MainApp.class);            
     intent.setAction(Intent.ACTION_MAIN);            
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);            
    
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);            
     views.setOnClickPendingIntent(R.id.widget, pendingIntent);            
     appWidgetManager.updateAppWidget(appWidgetId, views);        
  }    
    }
}
package com.jamesward;   
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import com.jamesward.MainApp;   

public class AndroidWidget extends AppWidgetProvider {
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)     {        
     final int N = appWidgetIds.length;           
  // Perform this loop procedure for each App Widget that belongs to this provider        
  
  for (int i = 0; i < N; i++)  {            
     int appWidgetId = appWidgetIds[i];            
     Intent intent = new Intent(context, MainApp.class);            
     intent.setAction(Intent.ACTION_MAIN);            
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);            
    
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);            
     views.setOnClickPendingIntent(R.id.widget, pendingIntent);            
     appWidgetManager.updateAppWidget(appWidgetId, views);        
  }    
    }
}

This class will display the Widget when necessary and register a click handler that will open the MainApp application when the user taps on the Widget.

5. Run the application to verify that it works.

6. Now you can add the widget to the home screen by holding down on the home screen and following the Widget wizard.

7. Verify that tapping the widget launches the AIR application.

原文作者: James Ward

给我留言

留言无头像?


×
腾讯微博