2012-02-10 14 views
0

アプリケーションでバッテリレベルをパーセンテージで表示するように通知を作成しました。私のマニフェストファイルは、私はバッテリーレベルを受け取り、ステータスバーに通知するBroadcastReceiverを拡張する別のクラスを作成しブロードキャストでバッテリの変更値が更新されない

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.create.bg" 
android:versionCode="1" 
android:versionName="1.0" > 

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

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name=".CreateBgActivity" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver android:name=".BatteryReceive" > 
     <intent-filter> 

      <action android:name="android.intent.action.BATTERY_CHANGED"></action> 
     </intent-filter> 
    </receiver> 
</application> 

です。私のコードは次のとおりです。

public class BatteryReceive extends BroadcastReceiver { 
private String bat = "android.intent.action.BATTERY_CHANGED"; 

@Override 
public void onReceive(Context context, Intent intent) { 
    //Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)); 
    if(intent.getAction().equals(bat)) { 
    Toast.makeText(context, "Battery Level"+intent.getIntExtra("level", 0), Toast.LENGTH_SHORT).show(); 
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    final Notification notifyDetails = new Notification(R.drawable.ic_launcher,""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1),System.currentTimeMillis()); 
    //notifyDetails.flags = Notification.FLAG_AUTO_CANCEL; 


    notifyDetails.setLatestEventInfo(context, "Batter Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1), null); 
    mNotificationManager.notify(1, notifyDetails); 
    } 
} 

} 

私は任意の通知を取得したり、バッテリーレベル用の出力をログ:(それは私のコードに何か問題ですいくつかのいずれかが、このうち私を助けてcould't

+0

ここではhttp://code.google.com/p/batterywidget/source/checkout – Rishi

+0

@Rishi私はすでに1つを見たバッテリーウィジェットの完全なソースです。私のコードで実装したのと同じことですが、うまくいきません。 –

+0

下記のクラスを参照してください://code.google.com/p/batterywidget/source/browse/trunk/a_batterywidget/src/com/geekyouup/android/widgets/battery/BatteryWidget.java?r = 5 Remoteviewビルドのアップデートあなたはあなたのバッテリーレベルを取得 – Rishi

答えて

0

@Override ます。public void onReceive(コンテキストコンテキスト、テント意図){

//Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)); 

if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { 
    // This is called when Your battery is changed 

} 

}

+0

これも試しました。私は初めてそれを実行するとき、それはバッテリーレベルを与える。それは後でそれを与えません。 –

+0

この回答を確認してください:http://stackoverflow.com/questions/11277302/i-cant-receive-broadcast-on-battery-state-change – benchuk

0

変更:。。

if(intent.getAction().equals(bat)) 

へ:

if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) 
+0

私もあなたの答えを使用しました。しかし、私はバッテリーレベルではありませんでした。 –

関連する問題