在android中检测拨出电话

时间:2013-12-25 07:29:16

标签: android

我需要检测拨出电话并将号码作为短信发送到另一个号码。 有没有办法检测拨出电话。我尝试过phonestatelistener,但它不起作用。

4 个答案:

答案 0 :(得分:1)

我希望这会对你有所帮助

Uri allCalls = Uri.parse("content://call_log/calls");
Cursor c = managedQuery(allCalls, null, null, null, null);

String num= c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for  number
String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call     type, Incoming or out going

以及更多信息的教程Link

source

答案 1 :(得分:1)

请参阅检测拨出电话的以下链接:

Detecting incoming and outgoing phone calls on Android

ACTION_NEW_OUTGOING_CALL

public class OutgoingReceiver extends BroadcastReceiver {
     public OutgoingReceiver() {
     }

     @Override
     public void onReceive(Context context, Intent intent) {
         String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

         Toast.makeText(ctx, 
           "Outgoing: "+number, 
           Toast.LENGTH_LONG).show();
     }
}

注册广播接收器:

IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
ctx.registerReceiver(outgoingReceiver, intentFilter);

答案 2 :(得分:1)

试试此代码

private class CallStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                number = incomingNumber;
                Thread_calls.run();
                //Toast.makeText(ctx, "Incoming: " + incomingNumber,Toast.LENGTH_LONG).show();
                break;
            }
        }
    }

    public class OutgoingReceiver extends BroadcastReceiver {
        public OutgoingReceiver() {
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
            Thread_calls.run();
            //Toast.makeText(ctx, "Outgoing: " + number, Toast.LENGTH_LONG).show();
        }

    }

答案 3 :(得分:1)

如果你的意思是检测到拨打电话的时间开始在另一部电话上振铃,那么我可以告诉他们没有办法(遗憾地)这样做。

通过常规的非系统应用程序检测是不可能的 - 没有Android API。我找不到办法,我在很长一段时间内用Google搜索解决方案。