【问题标题】:How to change screen after call is rejected or disconnected in android如何在android中拒绝或断开呼叫后更改屏幕
【发布时间】:2015-07-18 08:36:13
【问题描述】:

我正在制作一个随叫随到的 Dialer 应用程序演示,我已经成功地完成了传入和传出功能。现在我的问题是我想在呼叫被拒绝或断开连接时更改活动。我已经尝试了很多东西,也挖掘了很多链接。任何人都可以帮助我如何在通话断开时切换屏幕。我的 call_state 接收器如下:

package com.example.dialingdemo;

import Utils.Const;
import Utils.Pref;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.provider.CallLog;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class MyCallReceiver extends BroadcastReceiver {

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

        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

            final String savedNumber = intent.getExtras().getString(
                    "android.intent.extra.PHONE_NUMBER");

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent(context, CallScreen.class);
                    i.putExtra("incomingNumber", savedNumber);
                    i.putExtras(intent);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    context.startActivity(i);

                    Toast.makeText(context, "OUTGOING CAL to : " + savedNumber,
                            Toast.LENGTH_LONG).show();
                }
            }, 1000);
        }

        else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {

            final String incomingNumber = intent
                    .getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Toast.makeText(context, "Call from:" + incomingNumber,
                    Toast.LENGTH_LONG).show();

            new Handler().postDelayed(new Runnable() {

                public void run() {
                    Intent i = new Intent(context, IncomingCall.class);
                    i.putExtra("incomingNumber", incomingNumber);
                    i.putExtras(intent);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                    context.startActivity(i);

                }
            }, 1000);

        } else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_IDLE)
                || intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                        TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            // This code will execute when the call is disconnected
            if (Const.call_state.equals("1")) {

                new Handler().postDelayed(new Runnable() {

                    public void run() {
                        Intent i = new Intent(context, CallScreen.class);

                        i.putExtras(intent);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        context.startActivity(i);

                    }
                }, 1000);
            } else if (Const.call_state.equals("2")) {

                new Handler().postDelayed(new Runnable() {

                    public void run() {
                        Intent i = new Intent(context, CallScreen.class);

                        i.putExtras(intent);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        context.startActivity(i);

                    }
                }, 1000);

            }

        }

    }

}

【问题讨论】:

  • 那么,你遇到了什么问题?

标签: android telephonymanager phone-state-listener


【解决方案1】:

使用TelephonyManager.EXTRA_STATE 检测通话是否响铃、摘机或掉线

String callState= receiverIntent.getStringExtra(TelephonyManager.EXTRA_STATE);

if (callState.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
             //call is disconnected or rejected
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多