有关电池的通知栏信息

时间:2012-02-23 12:54:31

标签: android

如何在电池上添加有关通知栏的信息?

可以添加文字,但我无法添加下载的电池。

3 个答案:

答案 0 :(得分:0)

嗯,你没有提供太多信息,你想做什么。所以我想你想创建一个后台服务来操纵它的状态栏图标来显示电池状态。因此,我建议以下有关Android服务和状态栏通知的网站。

http://developer.android.com/reference/android/app/Service.html

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

如果还有问题可以随意提问。

答案 1 :(得分:0)

老兄,这是通知代码

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

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

   notification.flags = Notification.FLAG_ONGOING_EVENT;

   Intent i = new Intent(this,KillerActivity.class); 

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

   notification.setLatestEventInfo(getApplicationContext(), "Varaha ", "Testing", penInt);

   notifi.notify(215,notification);

这里215是与通知栏相关联的唯一ID

Intent i = new Intent(this,KillerActivity.class); 

此处KillerActivity.class是您想要显示通知

的类的名称

答案 2 :(得分:0)

使用此代码可以获得使用的电池量,电池电压和温度。

@Override
public void onCreate() {
    BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        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);
            Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
        }
    };
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryReceiver, filter);
}

//使用此代码发布通知

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

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

   notification.flags = Notification.FLAG_ONGOING_EVENT;

   Intent i = new Intent(this,KillerActivity.class); 

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

   notification.setLatestEventInfo(getApplicationContext(), "Varaha ", "Testing", penInt);

   notifi.notify(215,notification);

合并上述代码段,您将获得所需的结果