安卓電量Widget

按照壇友的教程改了一下。

為桐廬等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及桐廬網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、桐廬網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

教程:http://billhoo.blog.51cto.com/2337751/762776
AppWidget系統(tǒng)框架:http://blog.csdn.net/thl789/article/details/7879257

WidgetUpdateService.java
package com.hy.study;
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.BatteryManager;
import android.os.IBinder;
import android.widget.RemoteViews;

//用以根據(jù)電池電量更新而刷新桌面Widget的服務(wù)
public class WidgetUpdateService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // 向系統(tǒng)注冊(cè)電池電量更新廣播,只接受帶有ACTION_BATTERRY_CHANGED事件的Intent
        registerReceiver(this.bCR, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

        bCR = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                int level = intent.getIntExtra("level", 0);
                int scale = intent.getIntExtra("scale", 100);
                int status = intent.getIntExtra("status", 0);
                int batteryHealth = level * 100 / scale;
                int icon = 0;

                AppWidgetManager widgetManager = AppWidgetManager
                        .getInstance(context);
                RemoteViews widgetView = new RemoteViews(
                        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(new ComponentName(context,
                        MonitorWidgetProvider.class), widgetView);
            }
        };
    }

    private BroadcastReceiver bCR = null;

    // 獲取電池狀態(tài)圖標(biāo)
    private int getStateIcon(int batteryHealth) {
        if (batteryHealth >= 0 || batteryHealth <= 100)
            return batteryStateIcon[batteryHealth];
        return R.drawable.stat_sys_battery_unknown;
    }

    // 獲得對(duì)應(yīng)的充電狀態(tài)圖標(biāo)
    private int getChargingIcon(int batteryHealth) {
        if (batteryHealth >= 0 && batteryHealth < 10)
            return R.drawable.stat_sys_battery_charge_anim5;
        if (batteryHealth >= 10 && batteryHealth < 20)
            return R.drawable.stat_sys_battery_charge_anim15;
        if (batteryHealth >= 20 && batteryHealth < 30)
            return R.drawable.stat_sys_battery_charge_anim25;
        if (batteryHealth >= 30 && batteryHealth < 40)
            return R.drawable.stat_sys_battery_charge_anim35;
        if (batteryHealth >= 40 && batteryHealth < 50)
            return R.drawable.stat_sys_battery_charge_anim45;
        if (batteryHealth >= 50 && batteryHealth < 60)
            return R.drawable.stat_sys_battery_charge_anim55;
        if (batteryHealth >= 60 && batteryHealth < 70)
            return R.drawable.stat_sys_battery_charge_anim65;
        if (batteryHealth >= 70 && batteryHealth < 80)
            return R.drawable.stat_sys_battery_charge_anim75;
        if (batteryHealth >= 80 && batteryHealth < 90)
            return R.drawable.stat_sys_battery_charge_anim85;
        if (batteryHealth >= 90 && batteryHealth < 100)
            return R.drawable.stat_sys_battery_charge_anim95;
        if (batteryHealth == 100)
            return R.drawable.stat_sys_battery_charge_animfull;
        return R.drawable.stat_sys_battery_unknown;
    }

    private int batteryStateIcon[] = { 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.java
package com.hy.study;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class BatteryMonitor extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startService(new Intent(this, WidgetUpdateService.class));
    }
}

-------------------------------------------------------------------------------------------
MonitorWidgetProvider.java
package com.hy.study;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
public class MonitorWidgetProvider extends AppWidgetProvider {
    // 當(dāng)Widget第一次被啟動(dòng)時(shí),啟動(dòng)配套服務(wù)
    @Override
    public void onEnabled(Context context) {
        context.startService(new Intent(context, WidgetUpdateService.class));
    }
    // 當(dāng)Widget更新時(shí),啟動(dòng)配套服務(wù)
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int appWidgetIds[]) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        context.startService(new Intent(context, WidgetUpdateService.class));
    }
    // 當(dāng)桌面上這個(gè)widget的所有實(shí)體都被刪除后,停止配套服務(wù)
    @Override
    public void onDisabled(Context context) {
        context.stopService(new Intent(context, WidgetUpdateService.class));
    }
}


-------------------------------------------------------------------------------------------
res\layout\monitor_widget_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="72dip"
    android:layout_height="72dip">
    <ImageView android: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
<?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="1000"
   android:initialLayout="@layout/monitor_widget_layout"
/>

-------------------------------------------------------------------------------------------
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hy.study"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.hy.study.BatteryMonitor"
            android:label="@string/app_name"
            android:launchMode="singleTask" >
        </activity>
        <service
            android:name=".WidgetUpdateService"
            android:enabled="true" />
        <receiver android:name=".MonitorWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <action android:name="android.intent.action.BATTERY_CHANGED" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/monitor_widget_info" />
        </receiver>
    </application>
</manifest>

安卓電量Widget

附件:http://down.51cto.com/data/2364570

分享題目:安卓電量Widget
分享地址:http://muchs.cn/article40/pjjdho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、外貿(mào)建站、定制開(kāi)發(fā)虛擬主機(jī)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)