1

我的问题是......当电池状态设置为满时如何获取信息。我已经尝试通过 ACTION_BATTERY_CHANGED 上的接收器执行此操作,但这不起作用,我几乎可以肯定接收器甚至没有收到此广播。也许我没有正确理解这一点,action_battery_changed 意味着当我们的电池得到另一个 % 时会发出信号?或者仅在断开或连接等特定事件中,因为此操作在我的应用程序中有效(在另一个类接收器中)。

<receiver android:name="com.lily.chargingscheduler.ChangingBatteryStatsOnReceiver"   >
      <intent-filter>
          <action android:name="android.intent.action.ACTION_BATTERY_CHANGED" />
      </intent-filter>
</receiver>

和类:

 public class ChangingBatteryStatsOnReceiver  extends BroadcastReceiver {
    static BDHelper db_helper;
    private String data;
    String level_;
    String answer;

@Override
public void onReceive(Context context, Intent intent) {
    db_helper = new BDHelper(context);
    data = (new SimpleDateFormat("MM/dd/yyyy HH:mm")).format(new Date());
    level_ = batteryLevel(context);

    if(db_helper.getLastRow().get_TO_D().equals("")){
        if(level_ == "100%"){// level return % as a String
            answer = calculate();//calculate returns date and other things
            ending_charging(context);//adding to table and notification of this event   
        }
    }
}

[编辑]

也许我不够具体,我在获取有关电池电量的信息方面没有问题,当我的电池充满时我在获取信息方面有问题。

4

1 回答 1

0
public void onReceive(Context context, Intent intent) {
             context.unregisterReceiver(this);
             int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
             int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
             int level = -1;
             if (currentLevel >= 0 && scale > 0) {
                 level = (currentLevel * 100) / scale; // level will returns 99 or 100 when battery is full
             }
             batteryPercent.setText("Battery Level Remaining: " + level + "%");
         }
     }; 
     IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
     registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}

另外,如果您想在低电量状态下收听电池,请添加<intent-filter/>

<receiver android:name=".ChangingBatteryStatsOnReceiver">
<intent-filter>
  <action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
  <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
  </intent-filter>
</receiver>
于 2015-03-04T13:38:49.423 回答