-1
我想從我給出的數字撥打來電。總之我想讓假來電。因爲我是新的,所以我不瞭解如何完成這項任務。 我已創建incomingCallReceiver類,我正在監聽電話的狀態。如何使我們的應用程序來電
我的活動類內我呼籲incomingCallReceiver類。
這是我IncommingCallReceiver
public class IncommingCallReceiver extends BroadcastReceiver
{
Intent mintent;
Context mcontext;
Bundle bundle;
public static String name1, phoneNumber1;
@Override
public void onReceive(Context context, Intent intent)
{
mintent = intent;
mcontext = context;
bundle = mintent.getExtras();
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int events = PhoneStateListener.LISTEN_CALL_STATE;
bundle = intent.getExtras();
if(bundle !=null)
{
tm.listen(phoneStateListener, events);
}
}
private final PhoneStateListener phoneStateListener = new PhoneStateListener()
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
String callState = "UNKNOWN";
state = bundle.getInt(TelephonyManager.EXTRA_STATE);
phoneNumber1 = bundle.getString("phonenumber");
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.i("IncomingCallReceiver", "Incomng Number: " + phoneNumber1);
Toast.makeText(mcontext, "incomming call is idle", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i("IncomingCallReceiver", "Incomng Number: " + phoneNumber1);
Toast.makeText(mcontext, "incomming call is ringing", Toast.LENGTH_LONG).show();
Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON);
answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
mcontext.sendOrderedBroadcast(answer, "android.permission.CALL_PRIVILEGED");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("IncomingCallReceiver", "Incomng Number: " + phoneNumber1);
Toast.makeText(mcontext, "incomming call is offhook", Toast.LENGTH_LONG).show();
break;
}
Log.i(">>>Broadcast", "onCallStateChanged " + callState);
super.onCallStateChanged(state, incomingNumber);
}
};
}