2015-03-08 54 views
0

我有一個應用程序,它在狀態欄和通知欄中顯示電池電量和溫度。當應用程序打開時,我可以看到狀態欄和通知中的數據更新。當我退出/關閉應用程序時,狀態欄和通知未更新其值。爲什麼關閉應用程序時狀態欄和通知欄不能更新?當應用程序關閉時Android狀態欄通知未更新

我使用BroadcastReceiver顯示通知。

public class MyReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    notificationMessage(context, intent); 
} 

private void notificationMessage(Context context, Intent intent){ 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

    Intent notificationIntent = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

    int icon = R.drawable.battery_level_images; 

    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 
    float cTemperature = (float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1)/10; 
    float fTemperature = celsiusToFahrenheit(cTemperature); 
    float voltage = (float) intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1)/1000; 
    int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1); 
    String healthText = getHealthText(health); 

    Notification notification = new NotificationCompat.Builder(context) 
    .setContentTitle("" + level + "% | " + healthText) 
    .setContentText(cTemperature + "\u2103 | " + fTemperature + "\u2109 | " + voltage + "V") 
    .setSmallIcon(icon, level) 
    .setWhen(0) 
    .setProgress(100, level, false) 
    .setOngoing(true) 
    .setContentIntent(pendingIntent) 
    .build(); 

    notificationManager.notify(0, notification); 
} 

在我的主要活動類中,我使用複選框打開通知。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ............................ 

    myReceiver = new MyReceiver(); 

    ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); 
    batteryStatus = this.registerReceiver(mBatteryInfoReceiver, ifilter); 

    .................... 

} 

public void onToggleClicked(View view) { 
    // Is the toggle on? 
    boolean on = ((CheckBox) view).isChecked(); 

    if (on) { 

     batteryStatus = this.registerReceiver(myReceiver, ifilter); 
     SavingData.setNotificationState(true); 

    } else { 

     try{ 
      this.unregisterReceiver(myReceiver); 
     }catch (IllegalArgumentException iae){ 
      iae.printStackTrace(); 
     } 

     NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.cancelAll(); 
     SavingData.setNotificationState(false); 
    } 
} 

這是我的AndroidManifest中的BroadcastReceiver。

<receiver android:name=".MyReceiver" /> 

回答

1

在您的服務中註冊您的BroadcastReceiver並在後臺運行它。隨時運行您的服務可能會耗盡用戶的電池電量。當心:)

+0

謝謝。在後臺運行服務並檢查電池狀態而不會耗盡用戶電池的好方法是什麼?在顯示電池電量百分比時,android系統如何做到這一點。 – EM2015 2015-03-08 19:13:45

+0

每當電池電量有任何變化時,android系統都會發送粘性廣播。請查看documentaion瞭解更多詳情:http://developer.android.com/training/monitoring-device-state/battery-monitoring.html和http://developer.android.com/reference/android/os /BatteryManager.html – 2015-03-09 03:06:57

0

一旦你的活動被破壞,通過registerReceiver()註冊您的BroadcastReceiver將會消失爲好。您可能在LogCat中發出警告或錯誤,指出這一點。一旦您的BroadcastReceiver消失,您將停止更新Notification

+0

我應該怎麼做,所以BroadCastReceiver不會消失。 – EM2015 2015-03-08 17:30:27

+0

@ EM2015正如Naresh R所指出的那樣,您需要在服務中註冊廣播接收器。 – JonasCz 2015-03-08 18:29:06

相關問題