开发者

How can i get indication when the user get a phone call?

开发者 https://www.devze.com 2023-04-11 09:56 出处:网络
How can i ge开发者_Python百科t indication when the user receives incoming call? I also want to get the indication when the application is not running.

How can i ge开发者_Python百科t indication when the user receives incoming call?

I also want to get the indication when the application is not running.

?


Hi for this you have to use broadcast reciever

public class TestServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {
    TelephonyManager telephony = (TelephonyManager) context
    .getSystemService(Context.TELEPHONY_SERVICE);

    int state = telephony.getCallState();
    String incomingNumber =
        intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

    switch (state) {
    case TelephonyManager.CALL_STATE_IDLE:

        Log.d("@@@@@@@@@@@@@@@@@@@@@@@@@TestServiceReceiver", "IDLE");
        break;
    case TelephonyManager.CALL_STATE_OFFHOOK:

        Log.d("@@@@@@@@@@@@@@@@@@@@@@@@@TestServiceReceiver", "OFFHOOK");
        break;
    case TelephonyManager.CALL_STATE_RINGING:

        Log.d("@@@@@@@@@@@@@@@@@@@@@@@@TestServiceReceiver", "RINGING");
        break;
    }
}

And in manifest file you have to declare receiver

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

now declear some permissions

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

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

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

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

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

finally you can do anything with your phone calls

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号