public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context,MyPhoneStateListener.class));
MyPhoneStateListener lst=new MyPhoneStateListener(context);
TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(lst, PhoneStateListener.LISTEN_CALL_STATE);
}
public IBinder onBind(Intent arg0)
{ // TODO Auto-generated method stub return null;
}
}
// class MyPhoneStateListener extends PhoneStateListener
{
//private static final String TAG = "PhoneStateChanged";
Context context;
//Context to make Toast if required
public MyPhoneStateListener(Context context)
{
super();
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE: //when Idle i.e no call
Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK: //when Off hook i.e in call //Make intent and start your service here Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING: //when Ringing Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
break;
default: break;
} } }
想要开发一个在用户调用时结束调用的应用程序,首先它应该从数据库中检测是否存储了该号码。这将是更好的主意。使用服务或广播接收器。结束通话请帮助我在过去一周尝试的代码。我使用了广播接收器,但是当呼叫进入时,这种情况没有帮助,其他活动除了接收器之外还有效。
答案 0 :(得分:2)
我假设您要拨打拨出电话以防被叫号码不在列表中:
您需要在清单中注册接收器:
<receiver
android:exported="true"
android:name=".receiver.OutgoingCall" >
<intent-filter android:priority="0" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
然后关注this