2012-02-23 57 views

回答

0

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

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

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

如果还有问题随时问。

+0

对不起,我的意思是这些人恰恰是出现在通知酒吧。 https://lh3.ggpht.com/ZKaSQ1xmJBV_RZVSOTS6kFIAMLV3nivGPWOqCzJ5-0WU4XBeLN9OZZXcegDE5WLwNgs – 2012-02-23 13:34:56

+0

是的。这是您在我的第二个链接中看到的通知。但是你需要一个上下文来通知。这意味着一项服务或活动。 – Moritz 2012-02-23 16:14:07

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是类的名称上意图ü想要显示通知

+0

我的意思是显示电池电量,而不是一个字符串。 – 2012-02-23 16:13:25

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); 

合并上面的代码片断