【移動(dòng)應(yīng)用開發(fā)技術(shù)】安卓電量Widget_第1頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】安卓電量Widget_第2頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】安卓電量Widget_第3頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】安卓電量Widget_第4頁(yè)
【移動(dòng)應(yīng)用開發(fā)技術(shù)】安卓電量Widget_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

【移動(dòng)應(yīng)用開發(fā)技術(shù)】安卓電量Widget

按照壇友的教程改了一下。教程:/2337751/762776AppWidget系統(tǒng)框架:/thl789/article/details/7879257WidgetUpdateService.javapackagecom.hy.study;importandroid.app.Service;importandroid.appwidget.AppWidgetManager;importandroid.content.BroadcastReceiver;importandroid.content.ComponentName;importandroid.content.Context;importandroid.content.Intent;importandroid.content.IntentFilter;importandroid.os.BatteryManager;importandroid.os.IBinder;importandroid.widget.RemoteViews;//用以根據(jù)電池電量更新而刷新桌面Widget的服務(wù)publicclassWidgetUpdateServiceextendsService{

@Override

publicIBinderonBind(Intentintent){

returnnull;

}

@Override

publicvoidonStart(Intentintent,intstartId){

//向系統(tǒng)注冊(cè)電池電量更新廣播,只接受帶有ACTION_BATTERRY_CHANGED事件的Intent

registerReceiver(this.bCR,newIntentFilter(Intent.ACTION_BATTERY_CHANGED));

bCR=newBroadcastReceiver(){

@Override

publicvoidonReceive(Contextcontext,Intentintent){

intlevel=intent.getIntExtra("level",0);

intscale=intent.getIntExtra("scale",100);

intstatus=intent.getIntExtra("status",0);

intbatteryHealth=level*100/scale;

inticon=0;

AppWidgetManagerwidgetManager=AppWidgetManager

.getInstance(context);

RemoteViewswidgetView=newRemoteViews(

context.getPackageName(),

R.layout.monitor_widget_layout);

//若正在充電

if(status==BatteryManager.BATTERY_STATUS_CHARGING){

icon=getChargingIcon(batteryHealth);

widgetView.setImageViewResource(R.id.monitor_widget_view,

icon

);

}else

widgetView.setImageViewResource(R.id.monitor_widget_view,

getStateIcon(batteryHealth));

widgetManager.updateAppWidget(newComponentName(context,

MonitorWidgetProvider.class),widgetView);

}

};

}

privateBroadcastReceiverbCR=null;

//獲取電池狀態(tài)圖標(biāo)

privateintgetStateIcon(intbatteryHealth){

if(batteryHealth>=0||batteryHealth<=100)

returnbatteryStateIcon[batteryHealth];

returnR.drawable.stat_sys_battery_unknown;

}

//獲得對(duì)應(yīng)的充電狀態(tài)圖標(biāo)

privateintgetChargingIcon(intbatteryHealth){

if(batteryHealth>=0&&batteryHealth<10)

returnR.drawable.stat_sys_battery_charge_anim5;

if(batteryHealth>=10&&batteryHealth<20)

returnR.drawable.stat_sys_battery_charge_anim15;

if(batteryHealth>=20&&batteryHealth<30)

returnR.drawable.stat_sys_battery_charge_anim25;

if(batteryHealth>=30&&batteryHealth<40)

returnR.drawable.stat_sys_battery_charge_anim35;

if(batteryHealth>=40&&batteryHealth<50)

returnR.drawable.stat_sys_battery_charge_anim45;

if(batteryHealth>=50&&batteryHealth<60)

returnR.drawable.stat_sys_battery_charge_anim55;

if(batteryHealth>=60&&batteryHealth<70)

returnR.drawable.stat_sys_battery_charge_anim65;

if(batteryHealth>=70&&batteryHealth<80)

returnR.drawable.stat_sys_battery_charge_anim75;

if(batteryHealth>=80&&batteryHealth<90)

returnR.drawable.stat_sys_battery_charge_anim85;

if(batteryHealth>=90&&batteryHealth<100)

returnR.drawable.stat_sys_battery_charge_anim95;

if(batteryHealth==100)

returnR.drawable.stat_sys_battery_charge_animfull;

returnR.drawable.stat_sys_battery_unknown;

}

privateintbatteryStateIcon[]={R.drawable.stat_sys_battery_0,

R.drawable.stat_sys_battery_1,R.drawable.stat_sys_battery_2,

R.drawable.stat_sys_battery_3,R.drawable.stat_sys_battery_4,

R.drawable.stat_sys_battery_5,R.drawable.stat_sys_battery_6,

R.drawable.stat_sys_battery_7,R.drawable.stat_sys_battery_8,

R.drawable.stat_sys_battery_9,R.drawable.stat_sys_battery_10,

R.drawable.stat_sys_battery_11,R.drawable.stat_sys_battery_12,

R.drawable.stat_sys_battery_13,R.drawable.stat_sys_battery_14,

R.drawable.stat_sys_battery_15,R.drawable.stat_sys_battery_16,

R.drawable.stat_sys_battery_17,R.drawable.stat_sys_battery_18,

R.drawable.stat_sys_battery_19,R.drawable.stat_sys_battery_20,

R.drawable.stat_sys_battery_21,R.drawable.stat_sys_battery_22,

R.drawable.stat_sys_battery_23,R.drawable.stat_sys_battery_24,

R.drawable.stat_sys_battery_25,R.drawable.stat_sys_battery_26,

R.drawable.stat_sys_battery_27,R.drawable.stat_sys_battery_28,

R.drawable.stat_sys_battery_29,R.drawable.stat_sys_battery_30,

R.drawable.stat_sys_battery_31,R.drawable.stat_sys_battery_32,

R.drawable.stat_sys_battery_33,R.drawable.stat_sys_battery_34,

R.drawable.stat_sys_battery_35,R.drawable.stat_sys_battery_36,

R.drawable.stat_sys_battery_37,R.drawable.stat_sys_battery_38,

R.drawable.stat_sys_battery_39,R.drawable.stat_sys_battery_40,

R.drawable.stat_sys_battery_41,R.drawable.stat_sys_battery_42,

R.drawable.stat_sys_battery_43,R.drawable.stat_sys_battery_44,

R.drawable.stat_sys_battery_45,R.drawable.stat_sys_battery_46,

R.drawable.stat_sys_battery_47,R.drawable.stat_sys_battery_48,

R.drawable.stat_sys_battery_49,R.drawable.stat_sys_battery_50,

R.drawable.stat_sys_battery_51,R.drawable.stat_sys_battery_52,

R.drawable.stat_sys_battery_53,R.drawable.stat_sys_battery_54,

R.drawable.stat_sys_battery_55,R.drawable.stat_sys_battery_56,

R.drawable.stat_sys_battery_57,R.drawable.stat_sys_battery_58,

R.drawable.stat_sys_battery_59,R.drawable.stat_sys_battery_60,

R.drawable.stat_sys_battery_61,R.drawable.stat_sys_battery_62,

R.drawable.stat_sys_battery_63,R.drawable.stat_sys_battery_64,

R.drawable.stat_sys_battery_65,R.drawable.stat_sys_battery_66,

R.drawable.stat_sys_battery_67,R.drawable.stat_sys_battery_68,

R.drawable.stat_sys_battery_69,R.drawable.stat_sys_battery_70,

R.drawable.stat_sys_battery_71,R.drawable.stat_sys_battery_72,

R.drawable.stat_sys_battery_73,R.drawable.stat_sys_battery_74,

R.drawable.stat_sys_battery_75,R.drawable.stat_sys_battery_76,

R.drawable.stat_sys_battery_77,R.drawable.stat_sys_battery_78,

R.drawable.stat_sys_battery_79,R.drawable.stat_sys_battery_80,

R.drawable.stat_sys_battery_81,R.drawable.stat_sys_battery_82,

R.drawable.stat_sys_battery_83,R.drawable.stat_sys_battery_84,

R.drawable.stat_sys_battery_85,R.drawable.stat_sys_battery_86,

R.drawable.stat_sys_battery_87,R.drawable.stat_sys_battery_88,

R.drawable.stat_sys_battery_89,R.drawable.stat_sys_battery_90,

R.drawable.stat_sys_battery_91,R.drawable.stat_sys_battery_92,

R.drawable.stat_sys_battery_93,R.drawable.stat_sys_battery_94,

R.drawable.stat_sys_battery_95,R.drawable.stat_sys_battery_96,

R.drawable.stat_sys_battery_97,R.drawable.stat_sys_battery_98,

R.drawable.stat_sys_battery_99,R.drawable.stat_sys_battery_100};}BatteryMonitor.javapackagecom.hy.study;importandroid.app.Activity;importandroid.content.Intent;importandroid.os.Bundle;publicclassBatteryMonitorextendsActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

startService(newIntent(this,WidgetUpdateService.class));

}}MonitorWidgetProvider.javapackagecom.hy.study;importandroid.appwidget.AppWidgetManager;importandroid.appwidget.AppWidgetProvider;importandroid.content.Context;importandroid.content.Intent;publicclassMonitorWidgetProviderextendsAppWidgetProvider{

//當(dāng)Widget第一次被啟動(dòng)時(shí),啟動(dòng)配套服務(wù)

@Override

publicvoidonEnabled(Contextcontext){

context.startService(newIntent(context,WidgetUpdateService.class));

}

//當(dāng)Widget更新時(shí),啟動(dòng)配套服務(wù)

@Override

publicvoidonUpdate(Contextcontext,AppWidgetManagerappWidgetManager,

intappWidgetIds[]){

super.onUpdate(context,appWidgetManager,appWidgetIds);

context.startService(newIntent(context,WidgetUpdateService.class));

}

//當(dāng)桌面上這個(gè)widget的所有實(shí)體都被刪除后,停止配套服務(wù)

@Override

publicvoidonDisabled(Contextcontext){

context.stopService(newIntent(context,WidgetUpdateService.class));

}}res\layout\monitor_widget_layout.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="72dip"

android:layout_height="72dip">

<ImageViewandroid:id="@+id/monitor_widget_view"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:src="@drawable/stat_sys_battery_unknown"/>

</LinearLayout>res\xml\monitor_widget_info.xml<?xmlversion="1.0"encoding="utf-8"?><appwidget-providerxmlns:android="/apk/res/android"

android:minWidth="294dp"

android:minHeight="72dp"

android:updatePeriodMillis="1000"

android:initialLayout="@layout/monitor_widget_layout"/>AndroidManifest.xml<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="/a

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論