当前位置:   article > 正文

App Wiget(Android窗体挂件)_android 窗口挂件

android 窗口挂件

App Widget

了解AppWidget前可以先看几个不错的Blog:
传送门:
1. Google App Widget介绍
2. App Widget初探


  • 每个AppWidget都有一个AppWidgetProviderInfo对象,该对象描述了每个AppWidget的基本数据(meta-data)信息 ,其定义在<appwidget-provider>节点信息。
  • 每个AppWidget都对应一个RemoteViews视图对象,该RemoteViews提供了特定AppWidget的展示(View视图)和操作 (例如,点击该RemoteViews会跨进程处理一些事情)。
  • AppWidgetManager类维护了应用程序中所有的AppWidget,并且为给每个AppWidget特定的Id去标识他们(一般我们用 appWidgetId去标识)。通过给定的appWidgetIdAppWidgetManager可以管理对应的AppWidget,例如:更新该AppWidgetIdRemoteViews视图,删除该AppWidget对象等 。
  • AppWidgetProvider广播类从来说是一个监听器,系统把对AppWidget的操作(例如,创建和更新等)分发给AppWidgetProvider类去处理。 对每个AppWidget,我们可以创建多个其多个实例,当然这些实例对应于不同的appWidgetId。 假设存在这么个MyAppWidgetProvider广播类,以及对应的MyAppWidgetProviderInfo对象。 那么,则存在如下关系:MyAppWidgetProvider.class: 代表了由该MyAppWidgetProvider创建的窗口小部件(AppWidget)的类型,一般用
    CompontentName对象形式表示 。 那么存在如下关系:

这里写图片描述
从上图可是,每个appWidget都对应于一个MyAppWidgetProvider类,于是当任何一个appWidgetId发生变化时,我们需要同步其他实例,保持同步性。


简单介绍

以下模拟一下App Widget的应用
通过两种方式启动应用程序

  1. App Widget启动

长按空白的桌面主屏幕会弹出“添加到主屏幕”,然后选择“窗口小部件”选项进入“选择窗口小部件”,最后选择想要的小部件就会添加到桌面主屏幕,当点击刚才添加的桌面控件就会进入到程序主入口。

  1. App启动:跟普通的Activity一样

以下为实现代码
main.xml布局文件,程序入口类的界面
my_layout.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:layout_width="fill_parent"

android:layout_height="wrap_content" 
android:text="程序入口" />  

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

类MainActivity程序入口类

package eoe.demo;  



import android.app.Activity;  

import android.os.Bundle;  


/**  

* 主程序入口类  

*   
* @author jiqinlin  

*  

*/


public class MainActivity extends Activity{  


@Override  

protected void onCreate(Bundle savedInstanceState) {  

super.onCreate(savedInstanceState);  

setContentView(R.layout.main);  

}  

}
  • 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

下面的代码才是开发AppWidget用到的代码

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



<!-- <ImageView  

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/imageView" 
android:gravity="center"

android:layout_width="fill_parent"

android:layout_height="wrap_content" /> -->  


<Button   
android:id="@+id/btn"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:background="@drawable/png1"/>  

</LinearLayout>
  • 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

my_appwidget.xml布局文件:

<?xml version="1.0" encoding="utf-8"?>  


<!-- AppWidgetProvderInfo: 描述AppWidget的大小、更新频率和初始界面等信息,以XML文件形式存在于应用的res/xml/目录下。  


注意:SDK1.5之后此android:updatePeriodMillis就失效了,要自己创建service更新  

-->  


<appwidget-provider  

xmlns:android="http://schemas.android.com/apk/res/android"

android:minWidth="75dip"

android:minHeight="45dip"

android:updatePeriodMillis="1000"

android:initialLayout="@layout/my_layout"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

清单文件

<?xml version="1.0" encoding="utf-8"?>  

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="eoe.demo" android:versionCode="1"

android:versionName="1.0">  

<application android:icon="@drawable/icon" android:label="@string/app_name">  

<activity android:name=".MainActivity" android:label="主程序">  

<intent-filter>  

<action android:name="android.intent.action.MAIN" />  

<category android:name="android.intent.category.LAUNCHER" />  

</intent-filter>  

</activity>  

<!-- TestActivity类为一个广播接收器,因为TestActivity继承自AppWidgetProvider -->  

<receiver android:name=".TestActivity" android:label="添加桌面控件">  

<intent-filter>  

<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />  

</intent-filter>  

<meta-data android:name="android.appwidget.provider"

android:resource="@xml/my_appwidget"/>  

</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
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

TestActivity类

package eoe.demo;  


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;  


/**  

* 为手机添加桌面控件,当点击桌面控件时则进入主程序  

*   
* AppWidgetProvider:继承自BroadcastRecevier,在AppWidget应用update、enable、disable和delete时接收通知。  

* 其中,onUpdate、onReceive是最常用到的方法,它们接收更新通知  

*   
* @author jiqinlin  

*  

*/


public class TestActivity extends AppWidgetProvider {  


/**  

* 用来间隔的更新App Widget,间隔时间用AppWidgetProviderInfo里的updatePeriodMillis属性定义(单位为毫秒)。  

* 注意:SDK1.5之后此android:updatePeriodMillis就失效了,要自己创建service更新。  

* 这个方法也会在用户添加App Widget时被调用,因此它应该执行基础的设置,比如为视图定义事件处理器并启动一个临时的服务Service,如果需要的话。  

* 但是,如果你已经声明了一个配置活动,这个方法在用户添加App Widget时将不会被调用,  

* 而只在后续更新时被调用。配置活动应该在配置完成时负责执行第一次更新。  

*/


@Override  

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  


System.out.println("onUpdate");  

//点击桌面组件时进入主程序入口  

Intent intent=new Intent(context, MainActivity.class);  

PendingIntent pendingIntent=PendingIntent.getActivity(context, 0, intent, 0);  

//RemoteViews类描述了一个View对象能够显示在其他进程中,可以融合layout资源文件实现布局。  

//虽然该类在android.widget.RemoteViews而不是appWidget下面,但在Android Widgets开发中会经常用到它,  

//主要是可以跨进程调用(appWidget由一个服务宿主来统一运行的)。  


RemoteViews myRemoteViews = new RemoteViews(context.getPackageName(), R.layout.my_layout);  

//myRemoteViews.setImageViewResource(R.id.imageView, R.drawable.png1);//设置布局控件的属性(要特别注意)  

myRemoteViews.setOnClickPendingIntent(R.id.btn, pendingIntent);  

ComponentName myComponentName = new ComponentName(context, TestActivity.class);  

//负责管理AppWidget,向AppwidgetProvider发送通知。提供了更新AppWidget状态,获取已经安装的Appwidget提供信息和其他的相关状态  

AppWidgetManager myAppWidgetManager = AppWidgetManager.getInstance(context);  

myAppWidgetManager.updateAppWidget(myComponentName, myRemoteViews);  

}  


/**  

* 当App Widget从宿主中删除时被调用。  

*/


@Override  

public void onDeleted(Context context, int[] appWidgetIds) {  

System.out.println("onDeleted");  

super.onDeleted(context, appWidgetIds);  

}  


/**  

* 当一个App Widget实例第一次创建时被调用。  

* 比如,如果用户添加两个App Widget实例,只在第一次被调用。  

* 如果你需要打开一个新的数据库或者执行其他对于所有的App Widget实例只需要发生一次的设置,  

* 那么这里是完成这个工作的好地方。  

*/


@Override  

public void onEnabled(Context context) {  

System.out.println("onEnabled");  

super.onEnabled(context);  

}  


/**  

* 当你的App Widget的最后一个实例被从宿主中删除时被调用。你应该在onEnabled(Context)中做一些清理工作,比如删除一个临时的数据库  

*/


@Override  

public void onDisabled(Context context) {  

System.out.println("onDisabled");  

super.onDisabled(context);  

}  


/**  

* 接收到每个广播时都会被调用,而且在上面的回调函数之前。  

* 你通常不需要实现这个方法,因为缺省的AppWidgetProvider实现过滤所有App Widget广播并恰当的调用上述方法。  

* 注意: 在Android 1.5中,有一个已知问题,onDeleted()方法在调用时不被调用。  

* 为了规避这个问题,你可以像Group post中描述的那样实现onReceive()来接收这个onDeleted()回调。  

*/


@Override  

public void onReceive(Context context, Intent intent) {  

System.out.println("onReceive");  

super.onReceive(context, intent);  

}  


}
  • 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
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175

常用方法

  1. AppWidgetProviderInfo类补充说明:
    publicComponentNameconfigure: 一般为一个Activity,表明该Activity复杂需要管理
  2. AppWidget的创建操作。
    public int updatePeriodMillis:用来更新AppWidget,但该属性在SDK1.5已废除

    AppWidgetProvider类介绍
    常用方法:

    • onDeleted() : 当该类型的AppWidget每次被删除时,调用此方法
    • onDisabled() : 当该类型的窗口小部件(AppWidget)全被删除时,调用此方法
    • onEnabled() : 当第一次创建该类型的AppWidget时,调用此方法
    • onReceive() : 广播接受者方法 , 用来接受广播消息
    • onUpdate() : 每次创建该类型的AppWidget都会调用此方法 , 通常来说我们需要在该方法里为该AppWidget指定 RemoteViews对象。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/313165
推荐阅读
相关标签
  

闽ICP备14008679号