Search code examples
androidandroid-mediaplayerlocationmanagersmsmanager

App works normally but crashes on clicking logout button


I am new to android,application is crashing when i click logout button.I am not understanding why.

I released the mp player resources but still it crashes.

For logout button

if(view==logoutbtn)
        {
            if (mp.isPlaying()) { mp.stop();mp.release();}
            firebaseAuth.signOut();
            finish();
            Intent intent=new Intent(ProfileActivity.this,LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }

And the function which runs on the current activity is

request.setInterval(5000);
client.requestLocationUpdates(request, new LocationCallback() {
                        @SuppressLint("NewApi") @Override
                        public void onLocationResult(LocationResult locationResult) {
                            Location location = locationResult.getLastLocation();
                            if (location != null) {


                                play_ringtone();
                                play_flashlight();
                                send_location_message(location.getLatitude(), location.getLongitude());


                            }
                        }
                    }, null);



Logcat when i run activity is:

W/MessageQueue: Handler (android.telephony.PhoneStateListener$1) {ba9c4a5} sending message to a Handler on a dead thread
    java.lang.IllegalStateException: Handler (android.telephony.PhoneStateListener$1) {ba9c4a5} sending message to a Handler on a dead thread

And the sms function is

private void send_location_message(Double latitude,Double longitude)
    {
        String msg = "Emergency!!\n\nLocation\n\n"+"https://www.google.com/maps/search/?api=1&query="+latitude+","+longitude ;
        for (int i = 0; i < emergency_phones.size(); i++)
        {
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(emergency_phones.get(i), null, msg, null, null);
            Toast.makeText(ProfileActivity.this, "Message Sent To  "+emergency_phones.get(i)+"  "+latitude+" "+longitude, Toast.LENGTH_SHORT).show();
        }
    }

Solution

    1. enclose the mp.isPlaying() in Try catch to avoid null pointer exception...

    2. stop the request object used in request.setInterval() object too in onDestroy

    3. release or unregister the location receiver in OnDestroy as i can see you using client. requestLocationUpdates , because even though your activity is stopped the broadcast receiver will still be there and with new location update it will run the code inside , hence its always necessary to stop those threads and BroadCastReceivers

      1. that error log is of Telephony and could be outside your app and not related...