Android Widget之桌面组件App Widget案例

移动开发
Android Widget之桌面组件App Widget案例是本文要介绍的内容,主要Android Widget组件的学习教程,具体内容来看本文详解。

Android Widget之桌面组件App Widget案例是本文要介绍的内容,主要Android Widget组件的学习教程,具体内容来看本文详解。

模拟一个案例:把AppWidget添加到桌面后,点击AppWidget后AppWidget文本会轮回改变

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/tv" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"   
        android:text="程序入口"   
        android:textSize="50dip"/> 
</LinearLayout> 
 
res/xml/my_appwidget.xml布局文件  
 
<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="120dp"   
    android:minHeight="60dp" 
    android:updatePeriodMillis="1000" 
    android:initialLayout="@layout/main"> 
</appwidget-provider> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

清单文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.ljq.activity" android:versionCode="1" 
    android:versionName="1.0"> 
    <application android:icon="@drawable/icon" 
        android:label="@string/app_name"> 
        <receiver android:name=".TestActivity"> 
            <meta-data android:name="android.appwidget.provider" 
                android:resource="@xml/my_appwidget"> 
            </meta-data> 
            <intent-filter> 
                <action android:name="COM.LJQ.ACTION.WIDGET.CLICK"></action> 
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
            </intent-filter> 
        </receiver> 
    </application> 
    <uses-sdk android:minSdkVersion="7" /> 
 
</manifest> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

变量类UtilTool:用来控件文本改变

package com.ljq.activity;  
 
public class UtilTool {  
    public static boolean isChange=true;  
}  
 
TestActivity类,继承自AppWidgetProvider  
 
package com.ljq.activity;  
 
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 TestActivity extends AppWidgetProvider {  
    // 自定义一个Action名  
    private static final String ACTION_CLICK_NAME = "COM.LJQ.ACTION.WIDGET.CLICK";  
    private RemoteViews rv;  
 
    @Override  
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
        System.out.println("onUpdate");  
        //获取R.layout.main布局,通过类RemoteViews对布局R.layout.main里的控件进行操作  
        /*rv = new RemoteViews(context.getPackageName(), R.layout.main);  
        Intent intentClick = new Intent(ACTION_CLICK_NAME);  
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);  
        rv.setOnClickPendingIntent(R.id.tv, pendingIntent);  
          
        ComponentName cmp = new ComponentName(context, TestActivity.class);  
        AppWidgetManager myAppWidgetManager = AppWidgetManager.getInstance(context);  
        myAppWidgetManager.updateAppWidget(cmp, rv);*/  
        final int N = appWidgetIds.length;  
        for (int i = 0; i < N; i++) {  
            int appWidgetId = appWidgetIds[i];  
            updateAppWidget(context, appWidgetManager, appWidgetId);  
        }  
      
    }  
      
    //AppWidget生命周期: 每接收一次,广播执行一次为一个生命周期结束。  
    //也就是说在重写AppWidgetProvider类里面声明全局变量做状态判断,  
    //每次状态改变AppWidgetProvider再接收第二次广播时即为你重新初始化也就是说重新实例化了一次AppWidgetProvider。  
    //今天我因为在里面放了一个boolean值初始化为true,观察调试看到每次进入都为TRUE故你在设置桌面组件时,  
    //全局变量把它声明在另外一个实体类用来判断是没问题的,切忌放在本类。  
    @Override  
    public void onReceive(Context context, Intent intent) {  
        System.out.println("onReceive");  
        if (rv == null) {  
            rv = new RemoteViews(context.getPackageName(), R.layout.main);  
        }  
        if (intent.getAction().equals(ACTION_CLICK_NAME)) {  
            if (UtilTool.isChange) {  
                rv.setTextViewText(R.id.tv, "abc");  
            } else {  
                rv.setTextViewText(R.id.tv, "123");  
            }  
            UtilTool.isChange = !UtilTool.isChange;  
            AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(context);  
            int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(context, TestActivity.class));  
            appWidgetManger.updateAppWidget(appIds, rv);  
        }else{  
            super.onReceive(context, intent);  
        }  
          
    }  
      
    private void updateAppWidget(Context context,  
            AppWidgetManager appWidgeManger, int appWidgetId) {  
        rv = new RemoteViews(context.getPackageName(), R.layout.main);  
        Intent intentClick = new Intent();  
        intentClick.setAction(ACTION_CLICK_NAME);  
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);  
        rv.setOnClickPendingIntent(R.id.tv, pendingIntent);  
        appWidgeManger.updateAppWidget(appWidgetId, rv);  
    }  

  • 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.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.

小结:Android Widget之桌面组件App Widget案例的内容介绍完了,希望通过Android Widget组件的应用内容的学习能对你有所帮助。

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-09-07 10:58:07

Android wid

2011-09-08 15:51:33

Android Wid组件

2011-09-08 15:29:50

Android Wid界面GridView

2011-02-28 13:04:27

RelativeLayAndroid Wid

2011-09-08 15:40:45

Android Wid组件

2010-09-15 15:06:34

Google VoicWidgetAndroid

2011-09-09 13:23:17

Widget

2011-09-08 11:13:29

Widget

2011-09-08 15:07:10

Android Wid搭建

2010-07-13 09:02:19

Widget开发

2011-03-14 09:55:25

AndroidWidget

2011-09-09 20:14:58

Android Wid

2011-05-27 16:57:13

Android widget

2011-09-09 10:19:13

2011-09-13 15:35:40

Widget

2010-05-13 10:45:38

2011-09-09 10:00:20

Android Wid开发

2010-07-23 08:54:02

2011-09-07 14:01:41

Android Wid实例

2010-01-25 14:04:17

Android Wid
点赞
收藏

51CTO技术栈公众号