Using BroadcastReceiver with PhoneStateListener Functionality

I am trying to make a MissCall application that sends a message automatically when I receive a missed call. I completed my application and everything worked out fine!
Here is the full scenario:
Problem: The
application worked fine , but when I restarted the device, the application did not work! . It only worked when I started my application at least once, after it worked fine until it shuts down.
Here is my code:

package com.example.misscallapp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class Pref_Main extends PreferenceActivity {
    int checkIt = 0;
    TelephonyManager tm;
    CallStateListener callStateListener = new CallStateListener();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        tm = (TelephonyManager) getBaseContext().getSystemService(
                Context.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    private class CallStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) { 
           // Is called whenever there is a change in call state
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone

                Toast.makeText(getBaseContext(), "Incoming: " + incomingNumber,
                        Toast.LENGTH_LONG).show();
                checkIt = 1;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                checkIt = 0;
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if (checkIt == 1) {
                    Intent i = new Intent(getBaseContext(),MyService.class);
                    i.putExtra("phno", incomingNumber);
                    startService(i); // service that sends the SMS
                }
                break;
            }
        }

    }



}

:
, - BroadcastReceiver. BroadcastReceiver, PhoneStateListener
, , , BroadcastReceivers , - PhoneStateListener, onCallStateChanged , :

package com.example.misscallapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class CallReceiverBroadcast extends BroadcastReceiver {
    int checkIt = 0;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            String incomingNumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                Toast.makeText(context , "Incoming: " + incomingNumber,
                        Toast.LENGTH_LONG).show();
                checkIt = 1;
            }

            if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
                // Call received
                checkIt = 0;
            }

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
                if (checkIt == 1) {
                    Toast.makeText(context , "This is not shown ",
                            Toast.LENGTH_LONG).show();
                    Intent i = new Intent(context,MyService.class);
                    i.putExtra("phno", incomingNumber);
                    context.startService(i);
                }
            }

        }

    }
}

, , :

package com.example.misscallapp;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class CallReceiverBroadcast extends BroadcastReceiver {
    int checkIt = 0;
    Context contextt;
    TelephonyManager tm;
    CallStateListener callStateListener = new CallStateListener();
    @Override
    public void onReceive(Context context, Intent intent) {
        contextt = context;
        tm = (TelephonyManager) context.getSystemService(
                Context.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    }

    private class CallStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // called when someone is ringing to this phone

                Toast.makeText(contextt, "Incoming: " + incomingNumber,
                        Toast.LENGTH_LONG).show();
                checkIt = 1;
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                checkIt = 0;
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                if (checkIt == 1) {
                 //startting the service
                    break;
                }
            }

        }

    }
}

, , , 1, , 10 !
, .


1:
, , onReceive() TelphoneManager instance is created Phoone.
:
CallReceiverBroadcast static! !! , , , , 2 , , . - , , , .

+4
3

, , , AT TelephonyManager.CALL_STATE_IDLE, , , 10 .

+1

bootbroadcast, . , .

+1

( ), . .

onReceive() , , , ,

  <receiver android:name=".CallReceiverBroadcast" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
  </receiver>

Android onReceive(), , checkIt onReceive(). , .

, onReceive(). ( , tm ), .

, , , / CallStateListener, .

To solve this problem, you must unregister the call status listener by overriding the onDestroy()broadcast receiver

@Override
public void onDestroy() {
        tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
}

However, note that there is no guarantee that the onReceive()process receiving the receiver will not be destroyed after the return . The second version, which does all the processing before returning onReceive(), is preferred if you can fix it.

0
source

Source: https://habr.com/ru/post/1532465/


All Articles