0

我正在研究显示剩余电池电量的android小部件。

它似乎在模拟器上工作,除了模拟器的电池电量没有移动,所以我无法进一步测试。在真实设备 (ICS) 上,它只显示初始 textview("TEST") 并且不执行任何其他操作。

显现

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.battery"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="main.MyBatteryWidget"
        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_info" />
    </receiver>
    <service android:name="main.MyBatteryWidget$BatteryUpdateService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.example.battery.action.UPDATE" />
        </intent-filter>
    </service>
</application>

小部件信息.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:updatePeriodMillis="0"
    android:minWidth="40dip"
    android:minHeight="72dip"
    android:initialLayout="@layout/widget_layout" >

</appwidget-provider>

小部件布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/textView"
        android:text="TEST"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>

MyBatteryWidget.java

package main;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
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.util.Log;
import android.widget.RemoteViews;

import com.example.battery.R;

public class MyBatteryWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
        Log.d("TEST", "onUpdate");
        context.getApplicationContext().startService(new Intent("com.example.battery.action.UPDATE"));
    }

    public static class BatteryUpdateService extends Service {
        private static int level = 0;
        private static int scale = 0;

        private BroadcastReceiver receiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                    level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                    scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                    Log.d("TEST", "receive");
                    updateViews(context);
                }
            }
        };

        private void updateViews(Context context) {
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
            views.setTextViewText(R.id.textView, String.valueOf(level / scale));
            ComponentName componentName = new ComponentName(context, MyBatteryWidget.class);
            AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
            appWidgetManager.updateAppWidget(componentName, views);
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            Log.d("TEST", "start");
            registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

    }

}

日志猫

03-27 19:51:34.963: D/dalvikvm(608): Not late-enabling CheckJNI (already on)
03-27 19:51:35.043: E/Trace(608): error opening trace file: No such file or directory (2)
03-27 19:51:35.083: D/TEST(608): onUpdate
03-27 19:51:35.103: D/TEST(608): start
03-27 19:51:35.133: D/TEST(608): receive
4

2 回答 2

0
Intent.ACTION_BATTERY_CHANGED

您必须在 Android Manifest 中收听

<receiver android:name=".myReceiver" >
    <intent-filter>
        <action android:name="Intent.ACTION_BATTERY_CHANGED" />
    </intent-filter>
</receiver>
于 2013-03-28T01:15:30.287 回答
0

有关工作示例,请查看我的迷你状态小部件,请参阅代码

于 2013-03-28T20:02:53.853 回答