2012-12-07 4 views
0

알림에서 배터리 상태를 업데이트하려고합니다. 서비스와 함께 알림을 생성합니다. 하지만 난 배터리 상태 Notofication를 업데이트하는 방법을 알고하지 않습니다 http://developer.android.com/guide/topics/ui/notifiers/notifications.html안드로이드에서 시스템 배터리 상태로 알림을 업데이트하는 방법

MainActivity

@Override 
    public void onCreate(Bundle savedInstanceState) { 

registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 
.. 
} 


private BroadcastReceiver batteryReceiver = new BroadcastReceiver() { 

     private int scale = -1; 
     private int level = -1; 
     private int voltage = -1; 
     private int temp = -1; 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 
      scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); 
      temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1); 
      voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1); 

      String ns = Context.NOTIFICATION_SERVICE; 
      NotificationManager mNotificationManager = (NotificationManager)context.getSystemService(ns); 
      long when = System.currentTimeMillis(); 

      String percent = ((level * 100)/scale) + "%"; 
      Notification notification = new Notification(R.drawable.call, percent, when); 
      /* <set your intents here> */ 
      mNotificationManager.notify(7331, notification); 

      Log.d(TAG, "Battery level is " + level + "/" + scale + ", temp is " + temp + ", voltage is " + voltage); 
     } 
    }; 

와 내가 서비스를 사용하고

같은 통지를 만듭니다

나는이 문서를 읽기

private void showNotification() { 
     Notification notification = new Notification(R.drawable.call, getString(R.string.notification_text), System.currentTimeMillis()); 
     Intent intent = new Intent(this, MainActivity.class); 

     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0); 
     notification.setLatestEventInfo(this, getString(R.string.notification_label), getString(R.string.notification_text_short), pi); 
     notification.flags |= Notification.FLAG_NO_CLEAR; 
     startForeground(7331, notification); 
    } 

오류 :

12-07 14:46:29.425: E/AndroidRuntime(25758): FATAL EXCEPTION: main 
12-07 14:46:29.425: E/AndroidRuntime(25758): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000010 (has extras) } in [email protected] 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:737) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.os.Handler.handleCallback(Handler.java:605) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.os.Looper.loop(Looper.java:137) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.app.ActivityThread.main(ActivityThread.java:4511) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at java.lang.reflect.Method.invokeNative(Native Method) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at java.lang.reflect.Method.invoke(Method.java:511) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at dalvik.system.NativeStart.main(Native Method) 
12-07 14:46:29.425: E/AndroidRuntime(25758): Caused by: java.lang.IllegalArgumentException: contentView required: pkg=com.xxx.xxx id=7331 notification=Notification(contentView=null vibrate=null,sound=null,defaults=0x0,flags=0x0) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.os.Parcel.readException(Parcel.java:1331) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.os.Parcel.readException(Parcel.java:1281) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at 
android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:317) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.app.NotificationManager.notify(NotificationManager.java:127) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.app.NotificationManager.notify(NotificationManager.java:106) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at com.xxx.xxx.MainActivity$1.onReceive(MainActivity.java:72) 
12-07 14:46:29.425: E/AndroidRuntime(25758): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:728) 
12-07 14:46:29.425: E/AndroidRuntime(25758): ... 9 more 
+1

: 당신은 통지를 업데이트 할 경우 하고, 당신이

여기 PendingActivity.getActivity

에 플래그로 PendingIntent.FLAG_UPDATE_CURRENT를 사용해야하는 것은 내 응용 프로그램에서 일부 코드입니다) 메소드를 호출합니다. –

+0

이전 주석 작성자가 정확합니다. 모든 통지에는 contentView가 필요합니다. 알림 객체를 생성하려면 Notification.Builder 클래스 (API 11에서 사용 가능하거나 이전 플랫폼의 경우 support-v4 라이브러리에서 사용 가능)를 사용하십시오. – dsandler

답변

1

사용 권한 BATTERY_STATS가 필요하다고 생각합니다. 나는 (당신이에서 onCreate에있는 contentView를 설정하지 않은 생각

final NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

Notification note = new Notification(R.drawable.icon, text, System.currentTimeMillis()); 
note.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE; 
note.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; 

Intent intent = null; 
PendingIntent pendingIntent = null; 

intent = new Intent(context, MailActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

note.setLatestEventInfo(context, "nyxdroid", text, pendingIntent); 
notificationMgr.notify(1980 + type, note); 
+0

고맙습니다. 아이콘 (R.drawable.icon) 대신 텍스트를 표시 할 수 있습니까? 59 %와 배터리 아이콘이 좋아? – senzacionale

관련 문제