简体   繁体   中英

Android app not working properly for all android phone

I'm doing an android application to measure the property of battery of android phones. However, when generating the apk file to test, it works for some phones, but not for every android phone. Here is my code. Let me know if I need to change anything. Thanks so much.

@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);
  }
}

I think it's because of this piece of code:

if(Build.VERSION.SDK_INT >= 20){ BatteryManager battery =(BatteryManager)getSystemService(Context.BATTERY_SERVICE);
.......
.......
.......
}

Due to this your code will work only for Kitkat wear, Lollypop and above.

if(Build.VERSION.SDK_INT >= 20){ BatteryManager battery =(BatteryManager)getSystemService(Context.BATTERY_SERVICE);
.......
....... 
.
......
}

This line of code only works for Android SDK version 20 and above. While creating the project you must set the minimum SDK to a lower value. Otherwise only android users who have an android device running above this SDK will be able to run it properly. Your code will work only for devices running KitKat and above.

By using this: Build.VERSION.SDK_INT >= 20 you prevent your users to have your app working if they don't have at least Kitkat wear, Lollypop, and every following verison.

The thing is if you want it to work for your users, you have two solutions.

  • 1

You have to set your minimum sdk version in your manifest file in order to prevent the users with a sdk version lower then 20 to meet a crash.

To set your targetted and minimum API level, use this example of gradle file:

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 {
            ...
    }
}
  • 2

You have to find a way to make your process for your user using a sdk lower than 20 as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM