我希望从一项活动到另一项活动获得电池电量百分比。
我无法从“BroadcastReciver”获取它,也无法从文本视图中提取它。
这是我的广播接收者:
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
float percentage = level/ (float) scale;
int mProgressStatus = (int)((percentage)*100);
bl.setText("" + mProgressStatus + "%");
}
};
这就是我提取它的方式:
public int batt(){
String input = bl.getText().toString();
input = input.replace( "\n", "" );
input = input.replace( " ", "" );
int total = getValue(input);
return total;
}
public int getValue(String line){
int value = 0;
if( line.contains( "+" ) ) {
String[] lines = line.split( "\\+" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value += getValue( lines[i] );
return value;
}
if( line.contains( "-" ) ) {
String[] lines = line.split( "\\-" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value -= getValue( lines[i] );
return value;
}
if( line.contains( "*" ) ) {
String[] lines = line.split( "\\*" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value *= getValue( lines[i] );
return value;
}
if( line.contains( "/" ) ) {
String[] lines = line.split( "\\/" );
value += getValue( lines[0] );
for( int i = 1; i < lines.length; i++ )
value /= getValue( lines[i] );
return value;
}
return Integer.parseInt( line );
}
答案 0 :(得分:2)
Android 2.0
或更高,您可以对BatterLevelActivity类的电池电量方法进行以下更改:
private void batteryLevel() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (rawlevel >= 0 && scale > 0) {
level = (rawlevel * 100) / scale;
}
batterLevel.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
有关整个项目的更多详情:battery-level-and-state。
或者您只需检查上述内容是否有效,SirDarius给出答案,动态设置ACTION_BATTERY_CHANGED
意图的广播接收器。
public class Main extends Activity {
private TextView batteryTxt;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context ctxt, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
batteryTxt.setText(String.valueOf(level) + "%");
}
};
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
batteryTxt = (TextView) this.findViewById(R.id.batteryTxt);
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}
答案 1 :(得分:0)
要将数据从一个活动传输到另一个活动,您可以执行此操作
Intent intent = new Intent(getBaseContext(), NewActivity.class);
intent.putExtra("BATTERY_LEVEL", mProgressStatus);
startActivity(intent);
然后在新活动中
String s = getIntent().getStringExtra("BATTERY_LEVEL");
OR
您可以使用sharedpreferences
您可以在此处详细了解共享偏好设置https://developer.android.com/reference/android/content/SharedPreferences.html