0

I want to know that how to launch an application right after the phone ends and the application will only open if the user didn't able to pick up the call and also how to get the incoming phone number in any android phone with the help of broadcast receiver

I used this technique to get the incoming phone number

public class CallStateListener extends PhoneStateListener { private int prev_state;

private static final String TAG = "CallStateListener";
@Override
public void onCallStateChanged(int state, String phoneNumber) {
    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");
    Log.v(TAG, phoneNumber);

    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d(TAG, "RINGING");
            prev_state = state;
            break;
        case TelephonyManager.CALL_STATE_IDLE:
            Log.d(TAG, "CALL_STATE_IDLE==>"+phoneNumber);

            if((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)){
                prev_state=state;
                //Answered Call which is ended
            }
            if((prev_state == TelephonyManager.CALL_STATE_RINGING)){
                prev_state=state;
                //Rejected or Missed call
            }
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            Log.d(TAG, "CALL_STATE_OFFHOOK");
            prev_state=state;
            break;
    }
}

}

and I called it in the broadcast receiver class

Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");

TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

CallStateListener callStateListener = new CallStateListener(); telephony.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Bundle bundle = intent.getExtras();

String phoneNr= bundle.getString("incoming_number");

Log.v(TAG, "phoneNr: "+phoneNr);

but it didn't work

Please help me to solve both the issues.

I tried many techniques to overcome this isssue but nothing work.

I tried to open the application and getting the incoming phone number by using these codes

*Full Code *

CallDetector.java

public class CallDetector extends BroadcastReceiver {

private static final String TAG = "CallDetector";

@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void onReceive(Context context, Intent intent) {
    Log.v(TAG, "WE ARE INSIDE!!!!!!!!!!!");

TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

CallStateListener callStateListener = new CallStateListener();telephony.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

Bundle bundle = intent.getExtras();String phoneNr= bundle.getString("incoming_number");Log.v(TAG, "phoneNr: "+phoneNr);

if (intent.getAction().equals(ACTION_PHONE_STATE_CHANGED)) 
{
Log.e("Start","Call Started");
}else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Log.e("End","CallEnded");
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setComponent(new ComponentName("com.codewithrajat.detectcallstateusingbroadcastreceiver","com.codewithrajat.detectcallstateusingbroadcastreceiver.MainActivity"));

launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.codewithrajat.detectcallstateusingbroadcastreceiver");

launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {

Toast.makeText(context, "Package available, Successfully executed",Toast.LENGTH_SHORT).show();

context.startActivity(launchIntent);

Toast.makeText(context, "Executed?", Toast.LENGTH_SHORT).show();
}catch (ActivityNotFoundException e)
{
e.printStackTrace();
Toast.makeText(context, "Package not available ", Toast.LENGTH_SHORT).show();
}

PackageManager packageManager = context.getPackageManager();

Intent homeIntent = new Intent(Intent.ACTION_VIEW);homeIntent.setType("application/vnd.android.package.archive");

List list =       packageManager.queryIntentActivities(homeIntent,PackageManager.MATCH_DEFAULT_ONLY);

if(list.size()>0){intent.setDataAndType(Uri.parse(PACKAGE_PATH),"application/vnd.android.package.archive");

context.startActivity(intent);
}
} else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) 
{
Log.e("Incoming", "Incoming Call");
}

MainActivity.java

CallDetector receiver = new CallDetector();

IntentFilter filter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED); registerReceiver(receiver, filter);

Rajat Das
  • 1
  • 1

0 Answers0