[英]Android app not working properly for all android phone
我正在做一個android應用程序來測量android手機電池的屬性。 但是,當生成要測試的apk文件時,它適用於某些手機,但不適用於每部Android手機。 這是我的代碼。 讓我知道是否需要更改任何內容。 非常感謝。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
level=(TextView)findViewById(R.id.level);
voltage=(TextView)findViewById(R.id.volt);
status1=(TextView)findViewById(R.id.stat);
temp=(TextView)findViewById(R.id.temp);
health1=(TextView)findViewById(R.id.healt);
tech=(TextView)findViewById(R.id.tech);
sour=(TextView)findViewById(R.id.source);
Button b = (Button) findViewById(R.id.ex);
amp=(TextView)findViewById(R.id.current);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);}
});
this.registerReceiver(this.myBatteryReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver myBatteryReceiver
= new BroadcastReceiver(){
@SuppressLint("InlinedApi")
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED)){
int lv = arg1.getIntExtra("level", 0);
level.setText("Level: "
+ String.valueOf(lv) + "%");
voltage.setText("Voltage: "
+ String.valueOf((float)arg1.getIntExtra("voltage", 0)/1000) + "V");
temp.setText("Temperature: "
+ String.valueOf((float)arg1.getIntExtra("temperature", 0)/10) + "c");
tech.setText("Technology: " + arg1.getStringExtra("technology"));
int status = arg1.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
String strStatus;
if (status == BatteryManager.BATTERY_STATUS_CHARGING){
strStatus = "Charging";
} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING){
strStatus = "Dis-charging";
} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
strStatus = "Not charging";
} else if (status == BatteryManager.BATTERY_STATUS_FULL){
strStatus = "Full";
} else {
strStatus = "Unknown";
}
status1.setText("Status: " + strStatus);
//int source=arg1.getIntExtra("source", BatteryManager.BATTERY_STATUS_UNKNOWN);
if(Build.VERSION.SDK_INT >= 20){
BatteryManager battery = (BatteryManager)getSystemService(Context.BATTERY_SERVICE);
int current=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
int currentAvg=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
int energy=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
int capacity=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
int bCapacity=battery.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
String string1 = "Current: "+ current*1000+" uA"+"\n";
string1+="Average Current: "+currentAvg+" uA"+"\n";
string1+="Remaining energy: "+energy+" nWh"+"\n";
string1+="Capacity: "+capacity+" uAh"+"\n\n";
amp.setText(string1);
}
int health = arg1.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
String strHealth;
if (health == BatteryManager.BATTERY_HEALTH_GOOD){
strHealth = "Good";
} else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT){
strHealth = "Over Heat";
} else if (health == BatteryManager.BATTERY_HEALTH_DEAD){
strHealth = "Dead";
} else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){
strHealth = "Over Voltage";
} else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){
strHealth = "Unspecified Failure";
} else{
strHealth = "Unknown";
}
health1.setText("Health: " + strHealth);
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我認為是因為這段代碼:
if(Build.VERSION.SDK_INT> = 20){BatteryManager battery =(BatteryManager)getSystemService(Context.BATTERY_SERVICE);
.......
.......
.......
}
因此,您的代碼僅適用於Kitkat服裝,Lollypop及更高版本。
if(Build.VERSION.SDK_INT >= 20){ BatteryManager battery =(BatteryManager)getSystemService(Context.BATTERY_SERVICE);
.......
.......
.
......
}
此代碼行僅適用於Android SDK版本20及更高版本 。 創建項目時,必須將最小SDK設置為較低的值。 否則,只有運行此SDK之上的android設備的android用戶才能正常運行它。 您的代碼僅適用於運行KitKat及更高版本的設備。
通過使用以下命令: Build.VERSION.SDK_INT >= 20
如果用戶至少沒有Kitkat服裝,Lollypop和所有后續版本,就可以阻止您的應用正常運行。
問題是,如果您希望它為您的用戶服務,則有兩種解決方案。
您必須在清單文件中設置最低sdk版本,以防止sdk版本低於20的用戶遇到崩潰。
要設置目標API和最低API級別,請使用以下示例gradle文件:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
...
minSdkVersion 20 // THIS LINE ENSURE THAT THE USER WITH A SDK LOWER THAN 20 CANT USE THE APP
targetSdkVersion (your targetted version)
}
buildTypes {
release {
...
}
}
您還必須找到一種方法來使您的用戶也使用低於20的sdk。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.