Jacob Moulton Rhodes
Jacob Moulton Rhodes

Reputation: 93

Android - Start App Upon Phone Call

Is there any way to develop an app that starts up when a user receives a phone call? I can't really go into details about the idea but was wondering if there was some call that would allow that to happen.

Upvotes: 0

Views: 143

Answers (3)

Vikrant_Dev
Vikrant_Dev

Reputation: 390

public class CallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        final Context cont = context;
        final Intent in = intent;

        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        Intent i = new Intent(cont, MainActivity.class);
                        i.putExtras(in);
                        i.addCategory(Intent.CATEGORY_LAUNCHER);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                        cont.startActivity(i);
                    }
                }, 1000);
        }
    }
}

You must implement this handler with postDelayed so that your activity screen can be on top of native call screen. If you dont want this to happen then you must remove this handler.

Add these to your manifest--

<receiver
     android:name=".CallReceiver"
     android:enabled="true" >
     <intent-filter android:priority="1000" >
     <action android:name="android.intent.action.PHONE_STATE" />
     </intent-filter>
</receiver>

and

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />

You have to use another extra_state for onreceiving call. This is for ringing state.

Upvotes: 1

Arun Thakur
Arun Thakur

Reputation: 55

Yes We can start an diffrent app when User receives a phone call.Example :- Truecaller,Mobile no. Tracker apps. You can use Services for this.

Upvotes: 1

Lal
Lal

Reputation: 14810

You can use a Broadcast Receiver for thatlike this

public class PhoneStatReceiver extends BroadcastReceiver{        
    private static final String TAG = "PhoneStatReceiver"; 
    private static boolean incomingFlag = false;
    private static String incoming_number = null;
    @Override
    public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                        
                    incomingFlag = false;
                    String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);        
                    Log.i(TAG, "call OUT:"+phoneNumber);                        

            }else{                        
                    TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                        
                    switch (tm.getCallState()) {
                    case TelephonyManager.CALL_STATE_RINGING:
                            incomingFlag = true;//标识当前是来电
                            incoming_number = intent.getStringExtra("incoming_number");
                        Log.i(TAG, "RINGING :"+ incoming_number);
                            break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:                                
                            if(incomingFlag){
                                    Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
                            }
                            break;
                    case TelephonyManager.CALL_STATE_IDLE:                                
                            if(incomingFlag){
                                    Log.i(TAG, "incoming IDLE");                                
                            }
                            break;
                    } 
            }

    }

}

Register this receiver in AndroidManifest like this

<receiver android:name=".filter.PhoneStatReceiver">  

            <intent-filter>

                 <action android:name="android.intent.action.PHONE_STATE"/>           

                 <action android:name="android.intent.action.NEW_OUTGOING_CALL" />

            </intent-filter>

</receiver>

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>

Upvotes: 2

Related Questions