서비스랑 브로드캐스트를 이용해서 noti로 배터리 퍼센트 표시해주고 바뀌고 그렇게 해주게
만들려고 하는데 잘 안되네요...ㅠ
package exam.AndroidFirst;
import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.widget.*;
public class NewService extends Service {
boolean mQuit;
static final int NAPNOTI = 1;
NotificationManager mNotiManager;
String str;
int ratio = 0;
public void onCreate() { //배터리 변화 감지 관련 브로드캐스트
super.onCreate();
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(mBRBattery,filter);
Log.d("C", "onCreate");
}
BroadcastReceiver mBRBattery = new BroadcastReceiver() { // 배터리 변화 감지했을경우 호출
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
onBatteryChanged(intent);
}
}
public void onBatteryChanged(Intent intent) {//noti 로 알려줌, 업데이트해줌
int plug, status, scale, level, ratio;
Notification noti = new Notification(R.drawable.icon,
"Battery ratio",System.currentTimeMillis());
String sPlug = "";
String sStatus = "";
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
ratio = level * 100 / scale;
noti.number = ratio;
noti.flags = Notification.FLAG_ONGOING_EVENT;
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent content = PendingIntent.getActivity(
NewService.this, 0, intent, 0);
noti.setLatestEventInfo(NewService.this, "Battery ratio",
str, content);
str = String.format("%d%%",ratio);
mNotiManager.notify(NewService.NAPNOTI, noti);
Toast.makeText(NewService.this, "dfsfsdf!", 0).show();
}
};
public void onDestroy() { // 해제
super.onDestroy();
unregisterReceiver(mBRBattery);
Toast.makeText(this, "Service End", 0).show();
mQuit = true;
Log.d("C", "onDestroy");
}
public int onStartCommand (Intent intent, int flags, int startId) { //서비스 시작
super.onStartCommand(intent, flags, startId);
Log.d("C", "onStartCommand");
mQuit = false;
NewThread thread = new NewThread(this, mHandler);
thread.start();
return START_STICKY;
}
public IBinder onBind(Intent intent) {
Log.d("C", "onBind");
return null;
}
class NewThread extends Thread {
NewService mParent;
Handler mHandler;
public NewThread(NewService parent, Handler handler) {
mParent = parent;
mHandler = handler;
}
public void run() {
Log.d("C", "run");
Message msg = new Message();
msg.what = 0;
mHandler.sendMessage(msg);
}
}
//}
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
Log.d("C", "Handler");
int plug, status, scale, level;
Intent intent = new Intent(NewService.this, AndroidFirst.class);
String action = intent.getAction();
Notification noti = new Notification(R.drawable.icon,
"Battery ratio",System.currentTimeMillis());
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
ratio = level * 100 / scale;
String sPlug = "";
String sStatus = "";
Intent intent_2 = new Intent();
if (msg.what == 0) {
noti.number = ratio;
noti.flags = Notification.FLAG_ONGOING_EVENT;
intent_2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent content = PendingIntent.getActivity(
NewService.this, 0, intent_2, 0);
noti.setLatestEventInfo(NewService.this, "Battery ratio",
str, content);
str = String.format("%d%",ratio);
mNotiManager.notify(NewService.NAPNOTI, noti);
Toast.makeText(NewService.this, "dfsfsdf!", 0).show();
}
}
}
};
}