如何在“呼叫死亡”之前检测“呼叫接受”?

时间:2013-03-12 14:58:05

标签: android broadcastreceiver call telephonymanager phone-state-listener

我尝试为android编写程序。我想用我的程序拨打电话,然后识别呼叫状态。使用流动功能我可以识别挂出状态和回答状态它不起作用。我尝试这个功能,但它也不起作用。

我希望在“呼叫死亡”之前检测“呼叫接受”。

public class OutgoingCallReceiver extends BroadcastReceiver {
    protected static final String CLASS_TAG = "OutgoingCallReceiver : ";
    private static final String LOG_TAG = "MyActivity";
    private ConnectivityManager connectivityManager;
    private TelephonyManager telephonyManager;
    private NetworkInfo networkInfo;
    private String networkStauts = "Not connected !!";



    //************************************
    @Override
    public void onReceive(Context context, Intent intent) {


        //Toast.makeText(getApplicationContext(),String.valueOf(isPhoneCalling), Toast.LENGTH_SHORT).show();
        Bundle bundle = intent.getExtras();
        if(null == bundle) return;          

        String phonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        //Log.i(LOG_TAG, CLASS_TAG + "phone number " + phonenumber);
        //Toast.makeText(getApplicationContext(),String.valueOf("phone number " + phonenumber), Toast.LENGTH_SHORT).show();
            tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            tm.listen(new PhoneStateListener() {                    
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {

                    switch (state) {
                    case TelephonyManager.CALL_STATE_RINGING:
                        Context context = getApplicationContext();
                        Toast.makeText(getApplicationContext(), "RINGING", Toast.LENGTH_SHORT).show();
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:

                        Toast.makeText(getApplicationContext(), "OFFHOOK", Toast.LENGTH_SHORT).show();

                        isPhoneCalling = true;
                        break;
                    case TelephonyManager. CALL_STATE_IDLE:
                        if (isPhoneCalling) {
                            Toast.makeText(getApplicationContext(),
                                    "Your call is disconnected by receiver", 10000)
                                    .show();
                            Intent i = getBaseContext().getPackageManager()
                                    .getLaunchIntentForPackage(
                                             getBaseContext().getPackageName());
                             i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                             startActivity(i);

                             isPhoneCalling = false;
                             //duration=c.getLong(c.getColumnIndex(CallLog.Calls.DURATION));
                             Toast.makeText(getApplicationContext(), "IDLE", Toast.LENGTH_SHORT).show();
                            call();
                        }

                        break;
                    default:
                        Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT).show();
                        //Log.d(LOG_TAG, CLASS_TAG + "Default: " + state);
                        break;
                    }
                }

            }, PhoneStateListener.LISTEN_CALL_STATE )
            //registerReceiver(phoneInfo, new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL));
        }

}

1 个答案:

答案 0 :(得分:0)

此问题无法解决。 Android中没有可以检测到成功放置的外拨电话的API。