开发者

android - widget help

I've recently trying to practice making a battery widget that shows the current battery status.

I've typed the source code :

The java file :

package cam.widget.batterywiz;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;

public class batwiz extends AppWidgetProvider {
    /** Called when the activity is first created. */
    @Override
    public void onUpdate (Context c, AppWidgetManager awm, int[] id) {
        c.startService(new Intent (c, UpdateService.class));
    }

    public static class UpdateService extends Service {
        public void onStart (Intent intent, int startId) {
            RemoteViews rv = new RemoteViews (getPackageName(), R.layout.bat);
            rv.setTextViewText(R.id.battery, "80%");
            ComponentName thiswidget = new ComponentName (this, batwiz.class);
            AppWidgetManager m = AppWidgetManager.getInstance(this);
            m.updateAppWidget(thiswidget, rv);
        }


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

    }
}

bat2.xml file :

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget"
    android:layout_width="fil开发者_StackOverflow中文版l_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <ImageView
        android:id="@+id/battery"
        android:src="@drawable/battery_icon"
        android:cropToPadding="true"
    >
    </ImageView>
</LinearLayout>

android manifest :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cam.widget.batterywiz"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <receiver android:name=".batwiz">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                       android:resource="@layout/bat" />
        </receiver>

    </application>
    <uses-sdk android:minSdkVersion="4" />

</manifest> 

can you guys try to figure out my mistake ??

because in my emulator it says "problem loading widget"

THX for the help..


The problem is in your manifest file, it says @xml/bat.

And your provider info should be in your /res/xml folder.

Also, you'll need to change your RemoteViews creation to RemoteViews rv = new RemoteViews (getPackageName(), R.layout.bat2);,

and ImageViews don't have a SetText method.

EDIT:

changes

RemoteViews rv = new RemoteViews (getPackageName(), R.layout.bat2);

android:resource="@xml/bat" />

(and move bat.xml from layout/ to xml/)

rv.setTextViewText(R.id.battery, "80%");

This will not work, ImageView is not a child of TextView and so it doesn't have a SetText method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜