我希望在来电后显示一个对话框,以便在接听电话时可以在后台运行我的应用程序。
如何在Android应用程序中捕获来电?
<receiver android:name="IncomingCallInterceptor">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
并声明权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
那么,
public class IncomingCallInterceptor extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
// Phone is ringing
}
}
}