2013-07-03 30 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())'我只想显示级别而不是图标。实际上,它只显示状态栏中的图标电池。有没有办法删除它? –