2013-07-03 29 views
0

我被困在一個通知的創建..只想在狀態欄(通知欄)中顯示電池電量通知是永久的。這是代碼的一部分:如何創建一個永久通知來顯示電池電量

private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 

      int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 

       String[] status = { 
       "Level: "+level 
       . . . 
       }; 

      NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 

      Notification notification = new Notification(R.drawable.ic_launcher,"Level",System.currentTimeMillis()); 

      notification.flags = Notification.FLAG_ONGOING_EVENT; 

      Intent i = new Intent(context, MainActivity.class); 

      PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0); 

      notification.setLatestEventInfo(getApplicationContext(), +level+"%", penInt); 

      notifi.notify(215,notification); 


      } 
}; 

它是正確的嗎?例如在清單中是否還有其他要添加的內容?我沒有指定的通知類,所以我用這種方式寫下這行:Intent i = new Intent(context, MainActivity.class);,但我不知道它是否正確。

回答

0

是的,你需要添加:

<uses-permission android:name="android.permission.BATTERY_STATS" /> 

這裏的更多信息:

Get battery level only once using Android SDK

+0

確定它的工作表示感謝。有一件事,現在這裏'通知通知=新通知(R.drawable.ic_launcher,+級別+「%」,System.currentTimeMillis())'我只想顯示級別而不是圖標。實際上,它只顯示狀態欄中的圖標電池。有沒有辦法刪除它? –