android中widgets开发入门(开发显示电源电量widgets) 参考网站: http://www.eoeandroid.com/viewthread.php?tid=19709&highlight=widget
Android 开发应用除了 程序应用,还有是 Widget应用。好多人会开发程序应用而不会开发Widget应用。

先简单说说Widget的原理。Widget是在桌面上的一块显示信息的东西,也通过单击Widget跳转到一个程序里面。而系统自带的程序,典型的Widget是music,这个Android内置的音乐播放小程序。这个是典型的Widget+app应用。就是一个程序既可以通过Widget启动,也可以通过App启动。Widget就是一个AppWidgetProvider+一个UI界面显示(预先绑定了好多Intent),界面上的信息可以通过程序控制而改变,单击Widget,上的控件只能激发发送一个Intent,或发出一个Service的启动通知。而AppWidgetProvider可以拦截这个Intent,而进行相应的处理(比如显示新的信息)。

我们先来看看AndroidManifest.xml中的代码:
<? xmlversion="1.0"encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package
="com.android"
android:versionCode
="1"
android:versionName
="1.0" >
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< receiver android:name =".BatteryMonitor"
android:label
="@string/app_name" >
< intent-filter >
< action android:name ="android.appwidget.action.APPWIDGET_UPDATE" />
</ intent-filter >
< meta-data
android:name ="android.appwidget.provider"
android:resource
="@xml/widget_provider" />
</ receiver >

</ application >
< uses-sdk android:minSdkVersion ="3" />
</ manifest > 这里很清楚的可以看出AppWidgetProvider就是一个receiver的东西。
在receiver中
< meta-data
android:name ="android.appwidget.provider"
android:resource
="@xml/widget_provider" /> 代表了一个widget_provider代码如下:
<? xmlversion="1.0"encoding="utf-8" ?>
< appwidget-provider
xmlns:android ="http://schemas.android.com/apk/res/android"
android:minWidth
="100dip"
android:minHeight
="100dip"
android:initialLayout
="@layout/main" >
</ appwidget-provider >

这个是Widget的显示设置,是对Widget属性的一个配置文件这个android:minHeight是Widget的高,这个android:minWidth
是Widget的宽。这个android:updatePeriodMillis属性是设置Widget页面的
更新页面的时间的频率。而这个android:initialLayout属性是表示的是初始化页面的布局,Android里画UI的地方都是通过xml文件,也可以通过代码程序来画,不过这样画的太麻烦了。

建一个类BatteryMonitor继承于AppWidgetProvider,而AppWidgetProvider继承与android.content.BroadcastReceiver,所以TestAppWidget就是一个拦截处理Intent的BroadcastReceiver,这些Intent只能在Androidmainfest里设置来拦截处理。
在onUpdate中设置窗口界面:

public void defaultWidget(Contextcontext) {

RemoteViewsupdateViews
=newRemoteViews(context.getPackageName(),R.layout.main);

//updateViews.setTextViewText(R.id.battery_level,BatteryLevel+"%");

updateViews.setImageViewResource(R.id.battery_img,R.drawable.batteryimg);

//Pushupdateforthiswidgettothehomescreen
ComponentNamebatteryWidget=newComponentName(context,BatteryMonitor.class);
AppWidgetManagermanager
=AppWidgetManager.getInstance(context);
manager.updateAppWidget(batteryWidget,updateViews);
}
启动一个服务来更新电源电量:
context.startService(new Intent(context, BatteryMonitorReceiver.class));
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;




public class BatteryMonitorReceiver extends Service
{
privateintBatteryLevel=0;

privateBroadcastReceiverbatteryReceiver=newBroadcastReceiver()
{
@Override
publicvoidonReceive(Contextcontext,Intentintent)
{
Stringaction
=intent.getAction();
if(action.equals(Intent.ACTION_BATTERY_CHANGED))
{
Log.d(
"BatteryLevel","BatteryCHANGED");
BatteryLevel
=intent.getIntExtra("level",0);
UpdateAppWidget(context);
}

}

}
;

publicvoidUpdateAppWidget(Contextcontext){
RemoteViewsupdateViews
=newRemoteViews(context.getPackageName(),R.layout.main);
updateViews.setTextViewText(R.id.battery_level,BatteryLevel
+"%");

//Pushupdateforthiswidgettothehomescreen
ComponentNamebatteryWidget=newComponentName(context,BatteryMonitor.class);
AppWidgetManagermanager
=AppWidgetManager.getInstance(context);
manager.updateAppWidget(batteryWidget,updateViews);
}


/**//*(non-Javadoc)
*@seeandroid.app.Service#onCreate()
*/

@Override
publicvoidonCreate(){
//TODOAuto-generatedmethodstub
super.onCreate();

IntentFilteritFilter
=newIntentFilter();
itFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryReceiver,itFilter);
}


@Override
publicvoidonStart(Intentintent,intstartId)
{

}



/**//*(non-Javadoc)
*@seeandroid.app.Service#onDestroy()
*/

@Override
publicvoidonDestroy(){
//TODOAuto-generatedmethodstub
super.onDestroy();
unregisterReceiver(batteryReceiver);
}


@Override
publicIBinderonBind(Intentintent)
{
//TODOAuto-generatedmethodstub
returnnull;
}


}
在xml中注册服务
< serviceandroid:name = " .BatteryMonitorReceiver " >
< intent - filter >
< actionandroid:name = " com.adroid.pp " />
< categoryandroid:name = " android.intent.category.default " />
</ intent - filter >
</ service > 代码下载

更多相关文章

  1. 安卓复习.Part1
  2. Android(安卓)APP优化—Android程序员必须掌握
  3. Android(安卓)EditText获取焦点后只显示光标不弹出软键盘
  4. Android程序如何实现换肤?
  5. eclipse 创建android项目 无法启动
  6. Android系统Surface机制的SurfaceFlinger服务简要介绍和学习计划
  7. Android新手引导库推荐
  8. android开发流程
  9. 一培训机构设计的学习android课程内容:供大家参考

随机推荐

  1. 第八章 列表、菜单以及其它视图——上
  2. 【Android】Binder架构深度解析
  3. Android实现圆形、圆角和椭圆自定义图片V
  4. android Task(任务)的简单理解
  5. Android新建工程出错-Android+Packaging+
  6. Android绯荤粺鏋舵瀯鍥惧強绠€鍗曠殑绯
  7. Android之自定义TextView学习笔记
  8. Android小项目之九 两种上下文的区别
  9. 鍦?Android(安卓)涓婇€氳繃妯℃嫙 HTTP
  10. android Activity之间数据传递 Parcelabl