如何在android中拒绝或断开呼叫后更改屏幕

时间:2015-07-18 08:36:13

标签: android telephonymanager phone-state-listener

我正在调用Dialer应用程序的演示,我已成功完成传入和传出功能。现在我的问题是我想在拒绝或断开呼叫时更改活动。我已经尝试了很多东西,并且通过许多链接进行挖掘。有人可以帮我解决断开呼叫时如何切换屏幕的问题。我的call_state的reciver如下:

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

            }

        }

    }

}

1 个答案:

答案 0 :(得分:0)

使用TelephonyManager.EXTRA_STATE检测来电正在响铃,摘机或下垂

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

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