Android 通话后返回应用程序

Android 通话后返回应用程序,android,get,call,back,telephony,Android,Get,Call,Back,Telephony,我正在尝试从应用程序拨打电话 我希望它在通话后返回应用程序 我在这个论坛上问了这个问题,但我不明白答案 我理解这样的事情,但我没有在任何时候启动CallListener。 那么我该怎么做呢?我真的不知道你的应用程序应该如何工作,但是如果你在文本视图中有你的电话号码,只需设置(在.xml文件中)属性:android:autoLink=“phone”,一切都会正常工作。(点击后,您将能够拨打电话,当您点击结束通话时,它将返回到您的活动) 编辑:如果要自动执行,则应使用意图。请尝试以下操作: 好的,

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

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

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

我理解这样的事情,但我没有在任何时候启动
CallListener

那么我该怎么做呢?

我真的不知道你的应用程序应该如何工作,但是如果你在文本视图中有你的电话号码,只需设置(在.xml文件中)属性:
android:autoLink=“phone”
,一切都会正常工作。(点击后,您将能够拨打电话,当您点击结束通话时,它将返回到您的活动)

编辑:如果要自动执行,则应使用意图。请尝试以下操作: 好的,我找到了答案 代码如下:

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;
                }

            }
        }
    }
}
在manufest中,我们需要添加

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


我把它放在manifest.xml中,它仍然是待命日志。你不需要把它放在manifest.xml中。把它放在.xml文件中,在那里你有一个文本视图,其中包含你想调用的号码。这里有一个例子:我做得对吗?main.xml JAVA file private void call(){try{Intent callIntent=newintent(Intent.ACTION_CALL);callIntent.setData(Uri.parse(“tel:xxxxxxx”);startActivity(callIntent);}catch(ActivityNotFoundException activityException){Log.e(“拨号示例”,“调用失败”,activityException);}你不再需要这个意图。将电话号码作为文本放在文本视图中,android:autoLink=“phone”属性将为你完成所有工作。我知道它仍然没有将我发送回我的应用程序。而且它没有响,只将我发送到带有号码的“phone”
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />