我可以获得在后台运行的Android应用程序代码,并持续检查电池百分比,并在电池电量达到7%时通知我, 我尝试了安装,但没有在屏幕上显示应用程序。
答案 0 :(得分:2)
这里我们听取意图(ACTION_BATTERY_CHANGED)并在触发意图时注册接收器。在ActionScript中,我认为这是DispatchEvent,我们将调用mBatInfoReceiver并获取当前电池寿命,并将该int值放入我们的textfield或TextView。
public class Main extends Activity {
private TextView contentTxt;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
int level = intent.getIntExtra("batterylevel", 7);
contentText.setText(String.valueOf(level) + "%");
}
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main_layout);
contentText = (TextView) this.findViewById(R.id.mspaceTxt);
this.registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}