我试图编译一个Android应用程序来从设备获取电池信息。 应用程序成功编译,但当我在设备上使用它时,它无法打开。
我使用的代码如下:
package com.nitin.batteryinfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView batLevel;
private TextView voltage;
private TextView temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView batLevel = findViewById (R.id.batLevelView);
TextView voltage = findViewById(R.id.voltView);
TextView temp = findViewById(R.id.tempView);
registerReceiver(this.batinf, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver batinf = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
int volt = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
int temps = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
batLevel.setText("Battery Status:"+String.valueOf(level)+"%");
voltage.setText("Battery Voltage:"+String.valueOf(volt));
double t = (double) temps/10;
temp.setText("Battery Temp:"+ String.valueOf(t));
}
};}
答案 0 :(得分:0)
如果您在安装应用时尝试安装APK文件并收到错误,那可能是因为您使用的是app-debug.apk而不是app-release.apk。
您需要通过生成签名的APK
来创建app-release.apk