Android电池级小部件

Android电池级小部件,android,service,android-appwidget,batterylevel,Android,Service,Android Appwidget,Batterylevel,我正在开发一个显示电池剩余电量的android小部件 它似乎在模拟器上工作,只是模拟器的电池电量没有移动,所以我无法进一步测试。在真实设备(ICS)上,它只显示初始文本视图(“测试”),不做任何其他事情 显示 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.battery" android:versionCode="1" android:versionName

我正在开发一个显示电池剩余电量的android小部件

它似乎在模拟器上工作,只是模拟器的电池电量没有移动,所以我无法进一步测试。在真实设备(ICS)上,它只显示初始文本视图(“测试”),不做任何其他事情

显示

<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>
Logcat

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
你必须在Android清单中收听它

<receiver android:name=".myReceiver" >
    <intent-filter>
        <action android:name="Intent.ACTION_BATTERY_CHANGED" />
    </intent-filter>
</receiver>


有关工作示例,请查看“我的迷你状态”小部件,请参见

您可以使用telnet在模拟器中更改电池电量:您不需要在那里注册。根据android官方教程,BatteryManager以粘性的方式进行广播。粘性意味着广播后会继续播放,但如果你想接收定期广播,你必须在清单中注册。我将
android.appwidget.action.appwidget_UPDATE
取出,并在上面进行了尝试,除了我输入
android:name=“main.MyBatteryWidget“
用于接收器。我是否需要修改代码中的其他内容?它在emulator上没有做任何事情。由于您对此有问题,我建议您单独尝试每个模块。而且,仅供参考,我不确定您为什么需要服务。我使用服务是因为我在周围看到了教程和示例代码。你能告诉我在这里使用服务有什么问题吗?
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
Intent.ACTION_BATTERY_CHANGED
<receiver android:name=".myReceiver" >
    <intent-filter>
        <action android:name="Intent.ACTION_BATTERY_CHANGED" />
    </intent-filter>
</receiver>