如何从代码启动和停止电话呼叫

时间:2015-11-28 13:14:01

标签: android android-intent

我一直在浏览很多代码示例和问题/答案,以便从我的活动中拨打电话,但是,我找不到办法停止拨打电话。

目的是进行10秒的测试呼叫并停止。

以前有人这样做过,有什么想法吗?

2 个答案:

答案 0 :(得分:1)

@milapTank指导我,回答我的问题,让别人使用:

  • 根据Android版本下载ITelephony.java文件。
  • 创建一个名为:com.android.internal.telephony的包,并将接口文件放入其中。

守则

 //iTelephony
    try {
        TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        Class cl = Class.forName(telephonyManager.getClass().getName());
        Method method = cl.getDeclaredMethod("getITelephony");
        method.setAccessible(true);
        ITelephony telephonyService = (ITelephony) Method.invoke(telephonyManager);
        telephonyService.endCall();

    } catch (Exception e) {
        //manage exception...
    }

它的工作正常 - Android Studio 1.5 - Android 4.0作为最低SDK和Android 6.0作为Target和Compile SDK。

答案 1 :(得分:0)

package main

import (
    "net/http"
    "net/http/httptest"
    "net/http/httputil"
    "net/url"
    "testing"

    "github.com/go-martini/martini"
    "github.com/stretchr/testify/assert"
)

type closeNotifyingRecorder struct {
    *httptest.ResponseRecorder
    closed chan bool
}

func newCloseNotifyingRecorder() *closeNotifyingRecorder {
    return &closeNotifyingRecorder{
        httptest.NewRecorder(),
        make(chan bool, 1),
    }
}

func (c *closeNotifyingRecorder) close() {
    c.closed <- true
}

func (c *closeNotifyingRecorder) CloseNotify() <-chan bool {
    return c.closed
}

func TestReverseProxy(t *testing.T) {
    // Mock backend
    backendResponse := "I am the backend"
    backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte(backendResponse))
    }))
    defer backend.Close()
    backendURL, _ := url.Parse(backend.URL)

    // Frontend
    m := martini.Classic()
    m.Get("/", func(w http.ResponseWriter, r *http.Request) {
        proxy := httputil.NewSingleHostReverseProxy(backendURL)
        proxy.ServeHTTP(w, r)
    })

    // Testing
    req, _ := http.NewRequest("GET", "/", nil)
    res := newCloseNotifyingRecorder()
    m.ServeHTTP(res, req)

    assert.Equal(t, 200, res.Code, "should be equal")
}

必须在清单中

button.setOnClickListener(this);
@Override
public void onClick(View view) {
    if(view == ok){
        Intent intent = new Intent(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + num));
        activity.startActivity(intent);

}

...

<uses-permission android:name="android.permission.CALL_PHONE" />

在清单中:

EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);

private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    if(TelephonyManager.CALL_STATE_RINGING == state) {
        Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
    }
    if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
        //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
        Log.i(LOG_TAG, "OFFHOOK");
    }
    if(TelephonyManager.CALL_STATE_IDLE == state) {
        //when this state occurs, and your flag is set, restart your app
        Log.i(LOG_TAG, "IDLE");
    }
}
}