我正在做一个电话拨号器,我在Android Manifest中使用Receiver和intent-filter。在Receiver中,我让TelephonyManager听取我的通话状态。我的问题是:我如何以及在哪里(在Receiver或其他地方)使TelephonyManager能够收听原始呼叫状态?
Android Manifest中的接收器
<receiver android:name="com.SetAndGet.amadey.myapplication5.MyReceiver2" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
代码:
@Override
public void onReceive(final Context context, Intent intent) {
Log.i("Point_1","onReceive");
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(state==0){context.startActivity(new Intent(context,MyActivity.class));
}else if(state==2){context.stopService(new Intent(context,MyService.class));
}else if(state==1){context.stopService(new Intent(context,MyService.class));}
}
},PhoneStateListener.LISTEN_CALL_STATE);
}