通话后回到应用程序 [英] Back to the App after call

查看:170
本文介绍了通话后回到应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试拨打应用程序中的电话



,我希望它在通话后返回应用程序



我在这个论坛上问了这个问题,但我不明白答案

如何在android中打个电话并回到我的活动当电话完成?

  public void call(){
尝试{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(tel:048598077));
getContext()。startActivity(callIntent);

} catch(ActivityNotFoundException activityException){
Log.e(dialing-example,Call failed,activityException);}
finally {
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM =(TelephonyManager)getContext()。getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener,PhoneStateListener.LISTEN_CALL_STATE);




$ b Private class EndCallListener扩展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 ==状态){
//等待电话摘机(可能设置一个布尔标志),以便知道您的应用发起了呼叫。
Log.i(LOG_TAG,OFFHOOK);

if(TelephonyManager.CALL_STATE_IDLE == state){
//当这种状态发生,并且你的标志被设置时,重新启动你的应用程序
Log.i(LOG_TAG,闲);


$ / code $ / pre

我知道这样的事情,但我没有' t在任何时候开始 CallListener
所以我该怎么做?

解决方案

好的,我找到了答案
这里是代码:

  import android.app.Activity; 
导入android.content.Context;
导入android.content.Intent;
导入android.net.Uri;
导入android.os.Bundle;
导入android.telephony.PhoneStateListener;
导入android.telephony.TelephonyManager;
导入android.util.Log;
导入android.view.View;
导入android.view.View.OnClickListener;
导入android.widget.Button;

public class MainActivity extends Activity {

final Context context = this;
私人按钮按钮;

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button =(Button)findViewById(R.id.buttonCall);

//添加PhoneStateListener


//添加按钮侦听器
button.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0){

call();

}

});



private void call()
{
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager =(TelephonyManager)this
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(tel:0377778888));
startActivity(callIntent);

}

私人类PhoneCallListener扩展PhoneStateListener {

private boolean isPhoneCalling = false;

字符串LOG_TAG =LOGGING 123;

@Override
public void onCallStateChanged(int state,String incomingNumber){

if(TelephonyManager.CALL_STATE_RINGING == state){
// phone响铃
Log.i(LOG_TAG,RINGING,number:+ incomingNumber); (TelephonyManager.CALL_STATE_OFFHOOK ==状态){
//活动
Log.i(LOG_TAG,OFFHOOK);
}

if

isPhoneCalling = true;
}

if(TelephonyManager.CALL_STATE_IDLE == state){
//在类初始化和电话结束时运行,需要检测标志
//从CALL_STATE_OFFHOOK
Log.i(LOG_TAG,IDLE);

if(isPhoneCalling){

Log.i(LOG_TAG,restart app);

//重新启动应用程序
意图i = getBaseContext()。getPackageManager()
.getLaunchIntentForPackage(
getBaseContext()。getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

isPhoneCalling = false;
}

}
}
}
}

,并且在制造商中我们需要添加

 < uses-permission android:name = android.permission.CALL_PHONE/> 
<使用权限android:name =android.permission.READ_PHONE_STATE/>


i'm trying to make a phone call from the app

and I want it to return back to the app after the call

i asked that question in this forum but i didn't understand the answer

How to make a phone call in android and come back to my activity when the call is done?

public void call() {
        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:048598077"));
            getContext().startActivity(callIntent);

        } catch (ActivityNotFoundException activityException) {
            Log.e("dialing-example", "Call failed", activityException);}
        finally {           
            EndCallListener callListener = new EndCallListener();
            TelephonyManager mTM = (TelephonyManager)getContext().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");
            }
        }

i understand some thing like that, but i didn't start the CallListener at any point. So how can i do that ?

解决方案

ok , I found the answer here is the code :

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    final Context context = this;
    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonCall);

        // add PhoneStateListener


        // add button listener
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                call();

            }

        });

    }

    private void call()
    {
        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);

        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:0377778888"));
        startActivity(callIntent);

    }

    private class PhoneCallListener extends PhoneStateListener {

        private boolean isPhoneCalling = false;

        String LOG_TAG = "LOGGING 123";

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                // phone ringing
                Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                // active
                Log.i(LOG_TAG, "OFFHOOK");

                isPhoneCalling = true;
            }

            if (TelephonyManager.CALL_STATE_IDLE == state) {
                // run when class initial and phone call ended, need detect flag
                // from CALL_STATE_OFFHOOK
                Log.i(LOG_TAG, "IDLE");

                if (isPhoneCalling) {

                    Log.i(LOG_TAG, "restart app");

                    // restart app
                    Intent i = getBaseContext().getPackageManager()
                            .getLaunchIntentForPackage(
                                    getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);

                    isPhoneCalling = false;
                }

            }
        }
    }
}

and in the manufest we need to add

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

这篇关于通话后回到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆