我正在寻找制作一个 AppWidget 来检查电池状态(电量、充电/未充电)。
我写了很多代码行,但是,即使它工作得很好,我发现把它放在屏幕上会使电话变慢,有时甚至崩溃。
我想是因为我做错了什么。有人能帮我吗?谢谢
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.bisneff.widgetone"
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" >
<receiver android:name="MyWidgetProvider" >
<intent-filter >
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
</application>
</manifest>
这是我的主要课程:
package it.bisneff.widgetone;
import it.bisneff.widgetone.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
public class MyWidgetProvider extends AppWidgetProvider {
//Unused String
private static final String ACTION_CLICK = "ACTION_CLICK";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
ComponentName thisWidget = new ComponentName(context,
MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
Intent batteryStatus =context.getApplicationContext().registerReceiver(this,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
//Battery Level
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
//Max Battery Level
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
// %
float batteryPct = level / (float)scale;
batteryPct=batteryPct*100;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
//Charging or not
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
//integer convertion
int batteryPctz=(int)batteryPct;
Log.w("WidgetExample", String.valueOf(batteryPctz)+"%");
//USB or AC charge
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
//update view
remoteViews.setTextViewText(R.id.update, String.valueOf(batteryPctz)+"%");
//select the image for the battery level
String res=new String();
res="it.bisneff.widgetone:drawable/battery";
if (batteryPct >= 90)
{ res+="6";
}
if (batteryPct >= 75 && batteryPct < 90)
{
res+="5";
}
if (batteryPct >= 60 && batteryPct < 75 )
{
res+="4";
}
if (batteryPct >= 35 && batteryPct < 60 )
{
res+="3";
}
if (batteryPct >= 15 && batteryPct < 35 )
{
res+="2";
}
if (batteryPct < 15 )
{
res+="1";
}
if(isCharging){
if(acCharge)
{
res+="cha";
}
if(usbCharge)
{
res+="usb";
}
if (batteryPctz==100){res="it.bisneff.widgetone:drawable/battery6full";}
}
//check the resource
int reso= context.getResources().getIdentifier(res, null, null);
//put the right image
remoteViews.setImageViewResource(R.id.imageView1, reso);
//Set Click Listener
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
//check received intent action
if( ((intent.getAction()).equals(Intent.ACTION_BATTERY_CHANGED)) || ((intent.getAction()).equals(Intent.ACTION_POWER_CONNECTED)) || ((intent.getAction()).equals(Intent.ACTION_POWER_DISCONNECTED))){
//get Bundle
Bundle extras = intent.getExtras();
//if extras
if(extras!=null) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName thisAppWidget = new ComponentName(context.getPackageName(), MyWidgetProvider.class.getName());
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
//call the onUpdate
onUpdate(context, appWidgetManager, appWidgetIds);
}
}
}
}
编辑:
我知道问题出在哪里。
如果我有
....
Intent batteryStatus =context.getApplicationContext().registerReceiver(this,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
context.getApplicationContext().unregisterReceiver(this);//this line
....
小部件停止循环。但它停止更新电池电量变化......
我添加了
@Override
public void onEnabled(Context context){
context.getApplicationContext().registerReceiver(this, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
一定要注册我的接收器,但它不起作用,我无法从 XML 注册它,因为文档说它不能在那里注册。
我能怎么做?