public class MyReceiver extends BroadcastReceiver {
    private Context mcontext;
    TelephonyManager telephonyManager =
        (TelephonyManager) mcontext.getSystemService(Context.TELEPHONY_SERVICE);
    public void onReceive(Context context, Intent intent) {
        mcontext = context;
        PhoneStateListener callStateListener = new PhoneStateListener() {
            public void onCallStateChanged(int state, String incomingNumber) {
                if (state == TelephonyManager.CALL_STATE_RINGING) {
                    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
                    Intent batteryStatus = mcontext.registerReceiver(null, ifilter);

                    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                        status == BatteryManager.BATTERY_STATUS_FULL;

                    if(isCharging==true){
                        Toast.makeText(mcontext,"PLEASE UNPLUG", Toast.LENGTH_LONG).show();
                    }
                }
            }
        };
        telephonyManager.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}
0

There are 0 best solutions below