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
精彩评论