본문 바로가기

JAVA/Android

안드로이드 Notification 배터리 표시기 만들기 예제 따라하기

- 마켓에서 흔히 볼 수 있는 통지 배터리 표시기를 만들어 보자.
- 이 코드를 이용해서 귀여운 배터리 표시기 및 배터리 위젯등을 개발해서 서비스를 제공해 보자. 
- 기본적으로 이미지 100개가 있어야 한다 숫자 표기가 된,,,

- Java 소스
public class ScreenReceiver extends BroadcastReceiver {
public static final String ACTION_RESTART_PERSISTENTSERVICE = "ACTION.Restart.PersistentService";

Context con;
@Override
public void onReceive(Context context, Intent intent) {
/*
* 안드로이드 시스템의 알람 매니저는 전원이 오프 되면 삭제 된다. 따라서
*/
con = context;
if (intent.getAction().equals(ACTION_RESTART_PERSISTENTSERVICE)) {
Intent i = new Intent(context, Myservice.class);
// Intent i = new Intent(this, PersistentService.class);
context.startService(i);

}

SharedPreferences pref = context.getSharedPreferences("Prefee1", 0); //SharedPreferences를 이용한 베터리 표시기 설정하기(사용/미사용)
int val = pref.getInt("how", 1);// name을 못읽었을때 guest

if (val == 1) {

if (intent.getAction().equals(
"android.intent.action.BOOT_COMPLETED")) {
Intent i2 = new Intent(context, Myservice.class);
// Intent i = new Intent(this, PersistentService.class);
context.startService(i2);
}

if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {

Notification notify = null;
NotificationManager notifier;
PendingIntent launchIntent;

String mon1 = "베터리 많다고 마이하지마라.";
String mon2 = "배터리도 없는데 고마해라.";
String mon3 = "슬슬 베터리 없어진다 고마해라.";
int level = intent.getIntExtra("level", 0);
notifier = (NotificationManager) con
.getSystemService(Context.NOTIFICATION_SERVICE);

switch (level) {
case 0:
notify = new Notification(R.drawable.allsbu0, "올스",
System.currentTimeMillis());
break;
case 1:
notify = new Notification(R.drawable.allsbu1, "올스",
System.currentTimeMillis());
break;
case 2:
notify = new Notification(R.drawable.allsbu2, "올스",
System.currentTimeMillis());
break;
case 3:
notify = new Notification(R.drawable.allsbu3, "올스",
System.currentTimeMillis());
break;
case 4:
notify = new Notification(R.drawable.allsbu4, "올스",
System.currentTimeMillis());
break;
case 5:
notify = new Notification(R.drawable.allsbu5, "올스",
System.currentTimeMillis());
break;
case 6:
notify = new Notification(R.drawable.allsbu6, "올스",
System.currentTimeMillis());
break;
case 7:
notify = new Notification(R.drawable.allsbu7, "올스",
System.currentTimeMillis());
break;
case 8:
notify = new Notification(R.drawable.allsbu8, "올스",
System.currentTimeMillis());
break;
case 9:
notify = new Notification(R.drawable.allsbu9, "올스",
System.currentTimeMillis());
break;
case 10:
notify = new Notification(R.drawable.allsbu10, "올스",
System.currentTimeMillis());
break;
case 11:
notify = new Notification(R.drawable.allsbu11, "올스",
System.currentTimeMillis());
break;
case 12:
notify = new Notification(R.drawable.allsbu12, "올스",
System.currentTimeMillis());
break;
case 13:
notify = new Notification(R.drawable.allsbu13, "올스",
System.currentTimeMillis());
break;
case 14:
notify = new Notification(R.drawable.allsbu14, "올스",
System.currentTimeMillis());
break;
case 15:
notify = new Notification(R.drawable.allsbu15, "올스",
System.currentTimeMillis());
break;
case 16:
notify = new Notification(R.drawable.allsbu16, "올스",
System.currentTimeMillis());
break;
case 17:
notify = new Notification(R.drawable.allsbu17, "올스",
System.currentTimeMillis());
break;
case 18:
notify = new Notification(R.drawable.allsbu18, "올스",
System.currentTimeMillis());
break;
case 19:
notify = new Notification(R.drawable.allsbu19, "올스",
System.currentTimeMillis());
break;
case 20:
notify = new Notification(R.drawable.allsbu20, "올스",
System.currentTimeMillis());
break;
case 21:
notify = new Notification(R.drawable.allsbu21, "올스",
System.currentTimeMillis());
break;
case 22:
notify = new Notification(R.drawable.allsbu22, "올스",
System.currentTimeMillis());
break;
case 23:
notify = new Notification(R.drawable.allsbu23, "올스",
System.currentTimeMillis());
break;
case 24:
notify = new Notification(R.drawable.allsbu24, "올스",
System.currentTimeMillis());
break;
case 25:
notify = new Notification(R.drawable.allsbu25, "올스",
System.currentTimeMillis());
break;
case 26:
notify = new Notification(R.drawable.allsbu26, "올스",
System.currentTimeMillis());
break;
case 27:
notify = new Notification(R.drawable.allsbu27, "올스",
System.currentTimeMillis());
break;
case 28:
notify = new Notification(R.drawable.allsbu28, "올스",
System.currentTimeMillis());
break;
case 29:
notify = new Notification(R.drawable.allsbu29, "올스",
System.currentTimeMillis());
break;
case 30:
notify = new Notification(R.drawable.allsbu30, "올스",
System.currentTimeMillis());
break;
case 31:
notify = new Notification(R.drawable.allsbu31, "올스",
System.currentTimeMillis());
break;
case 32:
notify = new Notification(R.drawable.allsbu32, "올스",
System.currentTimeMillis());
break;
case 33:
notify = new Notification(R.drawable.allsbu33, "올스",
System.currentTimeMillis());
break;
case 34:
notify = new Notification(R.drawable.allsbu34, "올스",
System.currentTimeMillis());
break;
case 35:
notify = new Notification(R.drawable.allsbu35, "올스",
System.currentTimeMillis());
break;
case 36:
notify = new Notification(R.drawable.allsbu36, "올스",
System.currentTimeMillis());
break;
case 37:
notify = new Notification(R.drawable.allsbu37, "올스",
System.currentTimeMillis());
break;
case 38:
notify = new Notification(R.drawable.allsbu38, "올스",
System.currentTimeMillis());
break;
case 39:
notify = new Notification(R.drawable.allsbu39, "올스",
System.currentTimeMillis());
break;
case 40:
notify = new Notification(R.drawable.allsbu40, "올스",
System.currentTimeMillis());
break;
case 41:
notify = new Notification(R.drawable.allsbu41, "올스",
System.currentTimeMillis());
break;
case 42:
notify = new Notification(R.drawable.allsbu42, "올스",
System.currentTimeMillis());
break;
case 43:
notify = new Notification(R.drawable.allsbu43, "올스",
System.currentTimeMillis());
break;
case 44:
notify = new Notification(R.drawable.allsbu44, "올스",
System.currentTimeMillis());
break;
case 45:
notify = new Notification(R.drawable.allsbu45, "올스",
System.currentTimeMillis());
break;
case 46:
notify = new Notification(R.drawable.allsbu46, "올스",
System.currentTimeMillis());
break;
case 47:
notify = new Notification(R.drawable.allsbu47, "올스",
System.currentTimeMillis());
break;
case 48:
notify = new Notification(R.drawable.allsbu48, "올스",
System.currentTimeMillis());
break;
case 49:
notify = new Notification(R.drawable.allsbu49, "올스",
System.currentTimeMillis());
break;
case 50:
notify = new Notification(R.drawable.allsbu50, "올스",
System.currentTimeMillis());
break;
case 51:
notify = new Notification(R.drawable.allsbu51, "올스",
System.currentTimeMillis());
break;
case 52:
notify = new Notification(R.drawable.allsbu52, "올스",
System.currentTimeMillis());
break;
case 53:
notify = new Notification(R.drawable.allsbu53, "올스",
System.currentTimeMillis());
break;
case 54:
notify = new Notification(R.drawable.allsbu54, "올스",
System.currentTimeMillis());
break;
case 55:
notify = new Notification(R.drawable.allsbu55, "올스",
System.currentTimeMillis());
break;
case 56:
notify = new Notification(R.drawable.allsbu56, "올스",
System.currentTimeMillis());
break;
case 57:
notify = new Notification(R.drawable.allsbu57, "올스",
System.currentTimeMillis());
break;
case 58:
notify = new Notification(R.drawable.allsbu58, "올스",
System.currentTimeMillis());
break;
case 59:
notify = new Notification(R.drawable.allsbu59, "올스",
System.currentTimeMillis());
break;
case 60:
notify = new Notification(R.drawable.allsbu60, "올스",
System.currentTimeMillis());
break;
case 61:
notify = new Notification(R.drawable.allsbu61, "올스",
System.currentTimeMillis());
break;
case 62:
notify = new Notification(R.drawable.allsbu62, "올스",
System.currentTimeMillis());
break;
case 63:
notify = new Notification(R.drawable.allsbu63, "올스",
System.currentTimeMillis());
break;
case 64:
notify = new Notification(R.drawable.allsbu64, "올스",
System.currentTimeMillis());
break;
case 65:
notify = new Notification(R.drawable.allsbu65, "올스",
System.currentTimeMillis());
break;
case 66:
notify = new Notification(R.drawable.allsbu66, "올스",
System.currentTimeMillis());
break;
case 67:
notify = new Notification(R.drawable.allsbu67, "올스",
System.currentTimeMillis());
break;
case 68:
notify = new Notification(R.drawable.allsbu68, "올스",
System.currentTimeMillis());
break;
case 69:
notify = new Notification(R.drawable.allsbu69, "올스",
System.currentTimeMillis());
break;
case 70:
notify = new Notification(R.drawable.allsbu70, "올스",
System.currentTimeMillis());
break;
case 71:
notify = new Notification(R.drawable.allsbu71, "올스",
System.currentTimeMillis());
break;
case 72:
notify = new Notification(R.drawable.allsbu72, "올스",
System.currentTimeMillis());
break;
case 73:
notify = new Notification(R.drawable.allsbu73, "올스",
System.currentTimeMillis());
break;
case 74:
notify = new Notification(R.drawable.allsbu74, "올스",
System.currentTimeMillis());
break;
case 75:
notify = new Notification(R.drawable.allsbu75, "올스",
System.currentTimeMillis());
break;
case 76:
notify = new Notification(R.drawable.allsbu76, "올스",
System.currentTimeMillis());
break;
case 77:
notify = new Notification(R.drawable.allsbu77, "올스",
System.currentTimeMillis());
break;
case 78:
notify = new Notification(R.drawable.allsbu78, "올스",
System.currentTimeMillis());
break;
case 79:
notify = new Notification(R.drawable.allsbu79, "올스",
System.currentTimeMillis());
break;
case 80:
notify = new Notification(R.drawable.allsbu80, "올스",
System.currentTimeMillis());
break;
case 81:
notify = new Notification(R.drawable.allsbu81, "올스",
System.currentTimeMillis());
break;
case 82:
notify = new Notification(R.drawable.allsbu82, "올스",
System.currentTimeMillis());
break;
case 83:
notify = new Notification(R.drawable.allsbu83, "올스",
System.currentTimeMillis());
break;
case 84:
notify = new Notification(R.drawable.allsbu84, "올스",
System.currentTimeMillis());
break;
case 85:
notify = new Notification(R.drawable.allsbu85, "올스",
System.currentTimeMillis());
break;
case 86:
notify = new Notification(R.drawable.allsbu86, "올스",
System.currentTimeMillis());
break;
case 87:
notify = new Notification(R.drawable.allsbu87, "올스",
System.currentTimeMillis());
break;
case 88:
notify = new Notification(R.drawable.allsbu88, "올스",
System.currentTimeMillis());
break;
case 89:
notify = new Notification(R.drawable.allsbu89, "올스",
System.currentTimeMillis());
break;
case 90:
notify = new Notification(R.drawable.allsbu90, "올스",
System.currentTimeMillis());
break;
case 91:
notify = new Notification(R.drawable.allsbu91, "올스",
System.currentTimeMillis());
break;
case 92:
notify = new Notification(R.drawable.allsbu92, "올스",
System.currentTimeMillis());
break;
case 93:
notify = new Notification(R.drawable.allsbu93, "올스",
System.currentTimeMillis());
break;
case 94:
notify = new Notification(R.drawable.allsbu94, "올스",
System.currentTimeMillis());
break;
case 95:
notify = new Notification(R.drawable.allsbu95, "올스",
System.currentTimeMillis());
break;
case 96:
notify = new Notification(R.drawable.allsbu96, "올스",
System.currentTimeMillis());
break;
case 97:
notify = new Notification(R.drawable.allsbu97, "올스",
System.currentTimeMillis());
break;
case 98:
notify = new Notification(R.drawable.allsbu98, "올스",
System.currentTimeMillis());
break;
case 99:
notify = new Notification(R.drawable.allsbu99, "올스",
System.currentTimeMillis());
break;
case 100:
notify = new Notification(R.drawable.allsbu100, "올스",
System.currentTimeMillis());
break;

}

Intent intent2 = new Intent(con, SiProjectActivity.class);
launchIntent = PendingIntent.getActivity(con, 0, intent2, 0);
if (level >= 0 && level <= 30) {
notify.setLatestEventInfo(con,
"배터리 " + Integer.toString(level) + "%", mon2,
launchIntent);

}
if (level >= 31 && level <= 70) {
notify.setLatestEventInfo(con,
"배터리 " + Integer.toString(level) + "%", mon3,
launchIntent);

}
if (level >= 71 && level <= 100) {
notify.setLatestEventInfo(con,
"배터리 " + Integer.toString(level) + "%", mon1,
launchIntent);

}
notify.flags = Notification.FLAG_ONGOING_EVENT;
notifier.notify(0, notify);

}
}
}
// }

}
----------------------------------------------------------------------------------------------------------------
public class Myservice extends Service {

private final IBinder mBinder = new MyBinder();
IntentFilter filter;
BroadcastReceiver mReceiver;
BroadcastReceiver mBatInfoReceiver;

@Override
public void onCreate() {
// TODO Auto-generated method stub
ScreenBattery();
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.d("Service", "service destroy call");
Intent intent = new Intent(Myservice.this, ScreenReceiver.class);
intent.setAction(ScreenReceiver.ACTION_RESTART_PERSISTENTSERVICE);
PendingIntent sender = PendingIntent.getBroadcast(Myservice.this,0,intent,0);
}

@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
ScreenBattery();
}

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.d("MY_TAG", "Service onBind()");
return mBinder;
}

public class MyBinder extends Binder {
Myservice getService() {
return Myservice.this;
}
}
void ScreenBattery(){
filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
mBatInfoReceiver = new ScreenReceiver();
registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}
----------------------------------------------------------------------------------------------------------------
- 실행화면