電池残量を告知するアプリ

218 views
Skip to first unread message

mol

unread,
Aug 31, 2011, 10:53:30 AM8/31/11
to 日本Androidの会
夜分遅くにすみません。molです。
今回友達と組んで電池残量を知らせるウィジェットを作ろうと思っています。
http://boco.hp3200.com/beginner/widget02-3.html
上記のサイトを参考にしてベースを作っているのですが
トークンに構文エラーがある。構成の位置が間違っているというエラーが出てしまい、
中々上手くいきません。

どなたか詳しい方ご教授お願いいたします。

------------------------------
package sample.android.battery.widget;

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.IBinder;
import android.widget.RemoteViews;


public class SampleBatteryWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context c, AppWidgetManager awm, int[] awi) {
Intent in = new Intent(c, WidgetService.class);
c.startService(in);
}

public static class WidgetService extends Service {
@Override
public void onStart(Intent in, int si) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryReceiver, filter);
}

@Override
public IBinder onBind(Intent in) {
return null;
}
}

private static BroadcastReceiver batteryReceiver = new
BroadcastReceiver() {
int scale = 100;
int level = 0;

@Override
public void onReceive(Context c, Intent in) {
String ac = in.getAction();
if (ac.equals(Intent.ACTION_BATTERY_CHANGED)){
level = in.getIntExtra("level", 0);
scale = in.getIntExtra("scale", 0);
}
}

AppWidgetManager awm = AppWidgetManager.getInstance(c);
ComponentName cn = new ComponentName(c,
SampleBatteryWidget.class);
RemoteViews rv = new RemoteViews(c.getPackageName(),
R.layout.main);
rv.setTextViewText(R.id.TextView, ""+(int)(level*100/scale));
awm.updateAppWidget(cn, rv);

};
}


package sample.android.battery.widget;

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.IBinder;
import android.widget.RemoteViews;


public class SampleBatteryWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context c, AppWidgetManager awm, int[] awi) {
Intent in = new Intent(c, WidgetService.class);
c.startService(in);
}

public static class WidgetService extends Service {
@Override
public void onStart(Intent in, int si) {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryReceiver, filter);
}

@Override
public IBinder onBind(Intent in) {
return null;
}
}

private static BroadcastReceiver batteryReceiver = new
BroadcastReceiver() {
int scale = 100;
int level = 0;

@Override
public void onReceive(Context c, Intent in) {
String ac = in.getAction();
if (ac.equals(Intent.ACTION_BATTERY_CHANGED)){
level = in.getIntExtra("level", 0);
scale = in.getIntExtra("scale", 0);
}
}

AppWidgetManager awm = AppWidgetManager.getInstance(c);
ComponentName cn = new ComponentName(c,
SampleBatteryWidget.class);
RemoteViews rv = new RemoteViews(c.getPackageName(),
R.layout.main);
rv.setTextViewText(R.id.TextView, ""+(int)(level*100/scale));
awm.updateAppWidget(cn, rv);

};
}

元木

unread,
Aug 31, 2011, 11:13:47 AM8/31/11
to android-g...@googlegroups.com
こんばんは、元木です。

参考にされているサンプルコードがJavaの文法的に間違っているためですよ。
BroadcastReceiverのonReceiveメソッドの下にある最後の5行を、onReceiveメソッドの中に
入れてやればとりあえずコンパイルは通るはずです。
(意図した動作になるかは確認していませんので悪しからず)

以上、よろしくお願いします。


--
このメールは Google グループのグループ「日本Androidの会」の登録者に送られています。
このグループに投稿するには、android-g...@googlegroups.com にメールを送信してください。
このグループから退会するには、android-group-j...@googlegroups.com にメールを送信してください。
詳細については、http://groups.google.com/group/android-group-japan?hl=ja からこのグループにアクセスしてください。


mol

unread,
Aug 31, 2011, 9:22:35 PM8/31/11
to 日本Androidの会
>元木様

エラーが消え、無事通す事が出来ました。
本当に有難うございました!
引き続き、アプリ制作に勤しみたいと思います。

Reply all
Reply to author
Forward
0 new messages