Getting battery status even when the application i

2019-08-20 04:20发布

Hey I am trying to make an application that tells me when the battery is full when the phone is connected to power. I made a service and used a broadcast receiver in that to check the battery status. Whenever i connect or disconnect the phone from the power, my app crashes. The service still runs and i get the notification when battery is full.

I dont know why is the app crashing again and again. Here is my code

public class Srvc extends Service{

@Override
public int onStartCommand(Intent intent, int flags, int startId){
    // TODO Auto-generated method stub

     final IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); 
     this.registerReceiver(this.br, ifilter);


    System.out.println("started the service");


    return super.onStartCommand(intent,flags,startId);
}



public final BroadcastReceiver br = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        System.out.println("inside on recieve");

        chckbttry(intent);

    }
};

private void chckbttry(Intent intent) {
    // TODO Auto-generated method stub

    System.out.println("inside chckbttry method");

    final int currLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    final int maxLevel = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharged = status == BatteryManager.BATTERY_STATUS_FULL;
    final int percentage = (int) Math.round((currLevel * 100.0) / maxLevel);

    if (status == BatteryManager.BATTERY_STATUS_CHARGING)
    {
         System.out.println("calling the BrdCst_strt");


     if (percentage == 100 || isCharged==true) {

         System.out.println("bttry fully chrged");
         Intent i = new Intent(getBaseContext(),PlayMus.class); //call another activity
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         i.putExtra("msg", "Battery fully charged");
         getApplication().startActivity(i);
     }
    else
     {
        System.out.println("not yet charged");
        Intent i = new Intent(getBaseContext(), Test.class);
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         i.putExtra("msg", "Battery not fully charged");
         getApplication().startActivity(i);


    } 
    }

    if (status == BatteryManager.BATTERY_STATUS_DISCHARGING)
    {
        System.out.println("unregistering the service");
        unregisterReceiver(br);
    }

} 

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    System.out.println("service stopped");
    unregisterReceiver(br);
    super.onDestroy();

}

}

Please help me.

Thanks.

2条回答
Bombasti
2楼-- · 2019-08-20 04:50

Write your code as a service. Its simple to use

public class ClassName extends Service{

         //write your code


       }

also add it on manifest

<service android:name="com.test.ClassName">               
           </service>

/////////////////// New Codes ///////////////////

public class BatCheck extends Service {
  public void onCreate() {
    super.onCreate();


            BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                  boolean isCharged = status == BatteryManager.BATTERY_STATUS_FULL;

                      if(isCharged){

                         // write your code

                          }
        }
    };

}

}

//////////////// Another One ///////////////////

In your manifest

   <receiver android:name=".BatCheck">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
  </intent-filter>
</receiver>

In your activity

    public class BatCheck extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) { 
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharged = status == BatteryManager.BATTERY_STATUS_FULL;

              if(isCharged){

                 // write your code

                  }

    }
}
查看更多
淡お忘
3楼-- · 2019-08-20 04:55

System messages like the battery level are sent as a Broadcast through the whole system. You can receive them with a so called BroadcastReceiver.

While Activities have an UI, you need to use a Service. They are meant to be used for doing longer work in the background while your App is not visible. Your App has to be started at least once to start this Service out of an Activity. Some additional information for you: There are also AsyncTasks. Those are meant to be used for stuff while your App is visible and working (like loading data from the Internet into a ListView or something). You have to Tasks, because your UI will freeze if you do not do so. Almost Everything you program without using Tasks, will be executed in the UI Thread. If the UI Thread gets busy, the user will not be able to do anything.

Now here some code:

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private MyService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (service == null) {
            // start service
            Intent i = new Intent(this, MyService.class);
            startService(i);
        }
        // finish our activity. It will be started when our battery is full.
        finish();
    }
}

MyService.java

public class MyService extends Service {

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService", "onStartCommand");
        // do not receive all available system information (it is a filter!)
        final IntentFilter battChangeFilter = new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED);
        // register our receiver
        this.registerReceiver(this.batteryChangeReceiver, battChangeFilter);
        return super.onStartCommand(intent, flags, startId);
    }

    private final BroadcastReceiver batteryChangeReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(final Context context, final Intent intent) {
            checkBatteryLevel(intent);
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        // There are Bound an Unbound Services - you should read something about
        // that. This one is an Unbounded Service.
        return null;
    }

    private void checkBatteryLevel(Intent batteryChangeIntent) {
        // some calculations
        final int currLevel = batteryChangeIntent.getIntExtra(
                BatteryManager.EXTRA_LEVEL, -1);
        final int maxLevel = batteryChangeIntent.getIntExtra(
                BatteryManager.EXTRA_SCALE, -1);
        final int percentage = (int) Math.round((currLevel * 100.0) / maxLevel);

        Log.d("MySerive", "current battery level: " + percentage);

        // full battery
        if (percentage == 100) {
            Log.d("MySerive", "battery fully loaded");
            Intent intent = new Intent(getBaseContext(), SecondActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getApplication().startActivity(intent);
        }
        // do not forget to unregister
        unregisterReceiver(batteryChangeReceiver);
    }

}

Manifest.xml: You need an entry for your Serivice - just like for an Activity.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mybatteryservice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mybatteryservice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.mybatteryservice.SecondActivity" >
        </activity>
        <service android:name="com.example.mybatteryservice.MyService" >
        </service>
    </application>

</manifest>

Please let me know if you have any more questions!

Best regards Vincent

查看更多
登录 后发表回答