I am trying to open an intent when outgoing call is made. But the app crashes. Am i missing something or this cannot be done? Or is there a way to open a different screen (activity/fragment) between call creation event and receiver's receiving call event ?
From the Android Developers Blog :
Listening for outgoing call requests
Apps that provide phone calling services (such as VOIP or number management) can set up Intent filters to handle outgoing call requests, such as those made from the Dialer or other installed apps. This provides a seamless integration for the user, who can transition directly to the calling service without having to redial or launch another app.
When the user initiates a call, the system notifies interested apps by sending an ordered broadcast of the
NEW_OUTGOING_CALL
Intent, attaching the original phone number, URI, and other information as extras. This gives apps such as Google Voice and others a chance to modify, reroute, or cancel the call before it's passed to the system's default phone app.If you want your phone calling app to be able to handle outgoing call requests, implement a broadcast receiver that receives the
NEW_OUTGOING_CALL
Intent, processes the number, and initiates a call as needed. Make sure to declare an intent filter forNEW_OUTGOING_CALL
in the receiver, to let the system know that your app is interested in the broadcast. You'll also need to request thePROCESS_OUTGOING_CALLS
permission in order to receive the Intent.Note that the system broadcasts
NEW_OUTGOING_CALL
only for numbers that are not associated with core dialing capabilities such as emergency numbers. This means thatNEW_OUTGOING_CALL
can not interfere with access to emergency services the way your use ofCALL_PRIVILEGED
might.Here's an example broadcast receiver declared in an app's manifest file:
<manifest> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> <application> ... <receiver android:name=MyOutgoingCallHandler"> <intent-filter> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> ... </application> </manifest>
The implementation of the corresponding broadcast receiver would look something like this:
public class MyOutgoingCallHandler extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Extract phone number reformatted by previous receivers String phoneNumber = getResultData(); if (phoneNumber == null) { // No reformatted number, use the original phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); } // My app will bring up the call, so cancel the broadcast setResultData(null); // Start my app to bring up the call ... } }
Because the
NEW_OUTGOING_CALL
broadcast is ordered, your app can choose whether to consume the call request itself or simply process the number and pass the result data on to other apps that may be interested. In this example, the broadcast receiver brings up a phone call on it's own service and sets the result data to null. This prevents the call request from reaching the default phone app.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.