我正在尝试一个电池小部件,但安装后它没有出现。我尝试了其他帖子中提到的所有解决方案来解决这个问题。我在清单中添加了虚拟活动。虽然,活动已启动,但未显示小部件。除了被告知的内容之外,我是否必须将其链接到其他地方?
这是我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.batteryapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name=".androidmain"
android:label="@string/app_name">
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<receiver android:name="BatteryWidget" >
<intent-filter>
<action android:name="android.appWidget.action.APPWIDGET_UPDATE" >
</action>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/battery_widget_info" >
</meta-data>
</receiver>
</application>
</manifest>
这是虚拟类。包 com.example.batteryapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class androidmain extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
sendBroadcast(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
this.finish();
//setContentView(R.layout.battery_widget);
}
}
//这里是.java类
package com.example.batteryapp;
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.util.Log;
import android.view.View;
import android.widget.RemoteViews;
public class BatteryWidget extends AppWidgetProvider {
private final int BATTERY_STEPS=10;
private RemoteViews widgetViews=new RemoteViews("com.example.batteryapp",R.layout.battery_widget);
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int []appWidgetIds){
//register for the receiver when the battery changes
Intent received=context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
String receivedAction=received.getAction();
if(receivedAction.equals(Intent.ACTION_BATTERY_CHANGED)){
//get the level amount,pass default value
int level=received.getIntExtra("level", 0);
//indicate the level amount within the text view
this.widgetViews.setTextViewText(R.id.text_level, level+"%");
Log.d("onUpdate_BW1","Before showBars()");
//call helper to show the level bars
showBars(level);
Log.d("onUpdate_BW2","After showBars()");
ComponentName appComponent=new ComponentName(context,BatteryWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(appComponent,this.widgetViews);
}
}
private void showBars(int currLevel){
if(currLevel>=10){
this.widgetViews.setImageViewResource(R.id.bar1, R.drawable.level_shape_low);
this.widgetViews.setViewVisibility(R.id.bar1,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar1, View.INVISIBLE);
}
if(currLevel>=20){
this.widgetViews.setImageViewResource(R.id.bar2,R.drawable.level_shape_low);
this.widgetViews.setViewVisibility(R.id.bar2, View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar2, View.INVISIBLE);
}
if(currLevel>=30){
this.widgetViews.setImageViewResource(R.id.bar3,R.drawable.level_shape_low);
this.widgetViews.setViewVisibility(R.id.bar3,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar3, View.INVISIBLE);
}
if(currLevel>=40){
this.widgetViews.setImageViewResource(R.id.bar4,R.drawable.level_shape_mid);
this.widgetViews.setViewVisibility(R.id.bar4,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar4, View.INVISIBLE);
}
if(currLevel>=50){
this.widgetViews.setImageViewResource(R.id.bar5,R.drawable.level_shape_mid);
this.widgetViews.setViewVisibility(R.id.bar5,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar5, View.INVISIBLE);
}
if(currLevel>=60){
this.widgetViews.setImageViewResource(R.id.bar6,R.drawable.level_shape_mid);
this.widgetViews.setViewVisibility(R.id.bar6,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar6, View.INVISIBLE);
}
if(currLevel>=70){
this.widgetViews.setImageViewResource(R.id.bar7,R.drawable.level_shape_high);
this.widgetViews.setViewVisibility(R.id.bar7,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar7, View.INVISIBLE);
}
if(currLevel>=80){
this.widgetViews.setImageViewResource(R.id.bar8,R.drawable.level_shape_high);
this.widgetViews.setViewVisibility(R.id.bar8,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar8, View.INVISIBLE);
}
if(currLevel>=90){
this.widgetViews.setImageViewResource(R.id.bar9,R.drawable.level_shape_high);
this.widgetViews.setViewVisibility(R.id.bar9,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar9, View.INVISIBLE);
}
if(currLevel>=100){
this.widgetViews.setImageViewResource(R.id.bar10,R.drawable.level_shape_high);
this.widgetViews.setViewVisibility(R.id.bar10,View.VISIBLE);
}
else{
this.widgetViews.setViewVisibility(R.id.bar10, View.INVISIBLE);
}
}
}
//布局/battery_widget.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/battery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_width="1dp"
android:layout_height="30dp"/>
<TextView
android:id="@+id/text_level"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/level"
android:gravity="center"
android:textSize="12sp"
android:textStyle="bold" >
</TextView>
<ImageView
android:background="@drawable/top_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_gravity="center">
</ImageView>
<ImageView
android:id="@+id/bar10"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar9"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar8"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar7"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar6"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar5"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar4"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar3"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar2"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
<ImageView
android:id="@+id/bar1"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="invisible">
</ImageView>
</LinearLayout>
//xml/battery_widget_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/battery_widget"
android:minHeight="72dp"
android:minWidth="72dp"
android:updatePeriodMillis="1800000" >
</appwidget-provider>
该活动刚刚启动,但在我的情况下没有小部件。