BatteryState가 변경 될 때마다 배터리 상태에 대한 정보를 표시하고 자체를 새로 고침하는 알림을 보내려고합니다. BatteryStateChangeBroadcastReceiver 및 알림이 작동하지 않는 서비스
은 우선은 Manifest.xml에 서비스를 등록 :<service android:name=".BatteryStatusNotification"/>
는 다음 나는 클래스를 만들어 서비스에 대한 방법을 구현했습니다. 그 후 NotificationBuilder 및 BatteryStateChange BroadcastReceiver를 작성했습니다. 내 MainActivity에서
public class BatteryStatusNotification extends Service {
int level;
String pluggedString;
String temperature;
String voltage;
@Override
public IBinder onBind(Intent intent) {
return null;
}
private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context c, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int plugged= intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
DecimalFormat tempformatter = new DecimalFormat("##,##,#");
int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
temperature = tempformatter.format(temp);
DecimalFormat volformatter = new DecimalFormat("#,###");
int vol = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
voltage = volformatter.format(vol);
switch (plugged) {
case 0: pluggedString = (getResources().getString(R.string.batteryUncharging));
break;
case 1: pluggedString = (getResources().getString(R.string.batteryChargingAC));
break;
case 2: pluggedString = (getResources().getString(R.string.batteryChargingUSB));
break;
case 4: pluggedString = (getResources().getString(R.string.batteryChargingWireless));
break;
}
batteryStatNotification();
}
};
@Override
public int onStartCommand(Intent intent, int flags, int StartId) {
this.registerReceiver(this.batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return START_STICKY;
}
public void batteryStatNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_battery)
.setContentTitle(pluggedString + " | " + level + " %")
.setContentText(getResources().getString(R.string.notiTemp) + " " + temperature + " | " + getResources().getString(R.string.notiVol) + " " + voltage + getResources().getString(R.string.volts))
.setOngoing(true);
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, builder.build()); //Notification Id 2 = Battery
}
@Override
public void onDestroy() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(2);
}
}
나는 ToggleButton이 있습니다
앱이 시작될 때까지 자신의 상태와 수익을 저장하고 이러한 두 가지 방법 호출//Toggle Battery Notification
ToggleButton batteryToggle = (ToggleButton) findViewById(R.id.batToggleNotification);
batteryToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("batteryToggle", 1);
prefEditor.commit();
enableBatteryNotification();
} else {
enableBatteryNotification();
disableBatteryNotification();
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("batteryToggle", 0);
prefEditor.commit();
}
}
});
int batteryTogglePref = sharedPref.getInt("batteryToggle", 0);
switch (batteryTogglePref) {
case 0: toggle.setChecked(false);
break;
case 1: toggle.setChecked(true);
break;
}
:
public void enableBatteryNotification() {
startService = new Intent(this, BatteryStatusNotification.class);
}
public void disableBatteryNotification() {
stopService(new Intent(getBaseContext(), BatteryStatusNotification.class));
}
I을 또한 경보로 새로 고침되는 WifiNotification이 있습니다. WifiNotification은 완벽하게 작동하지만 내 BatteryNotification은 그렇지 않습니다. @Sreekumar R에 대한
는편집 : 여기
getBaseContext().startService(startService);
을 : 내가 호출 할 필요는 없습니다 이유
public void enableWifiNotification() {
SharedPreferences sharedPref = getSharedPreferences("FileName", MODE_PRIVATE);
int numberSP3 = sharedPref.getInt("numberSP2", 1);
int unitSP3 = sharedPref.getInt("unitSP2", 1);
startService = new Intent(this, WifiNotificationService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
pendingIntent = PendingIntent.getService(this, 0, startService, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * numberSP3 * unitSP3 , pendingIntent);
}
당신이 설명 할 수 있습니까?
logcat에서 뭔가 보이나요? 귀하의 BroadcastReceiver가 ACTION_BATTERY_CHANGED을 (를) 듣고 있는지 알고 있습니까? –
@axl 코더는 도움을 주셔서 감사하지만 Sreekumar R이 내 문제를 해결했습니다. –