在安卓系统中如何中断来电

3
如何在来电时打开具有接听和拒绝按钮的自定义UI,我想显示自定义UI而不是默认拨号器。 我正在使用以下代码,但是拨号器已经打开并且我的活动没有打开:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.callintruptdemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".IncomingCall"></activity>

    <receiver android:name=".CallReceiver">
        <intent-filter >
            <action android:name="android.intent.action.PHONE_STATE"></action>
        </intent-filter>
    </receiver>
</application>

我有一个广播接收器,它监听来电,在接收器中:

@Override
public void onReceive(Context context, Intent intent) {
       Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: ");

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: " + state);
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
        {
            Intent i = new Intent(context, IncomingCall.class);
            i.putExtras(intent);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(i);
        }
}

来电类的代码如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

    setContentView(R.layout.activity_main);

    String number = getIntent().getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
    TextView text = (TextView)findViewById(R.id.text);
    text.setText("Incoming call from " + number);
}

但是我的自定义界面没有显示。

我还想在我的界面上放置一个按钮,并通过点击该按钮接收来电。 提前感谢您的帮助。

编辑: - 更新代码后,我能够打开我的自定义UI,现在我想通过单击其上的按钮接收电话。如何做到这一点,请帮忙。。


也许你的设备已经选择了默认的拨号器来接收所有的来电。请在网上搜索如何清除该应用程序的默认设置。 - npace
我正在模拟器上测试它,当我运行应用程序时,它显示拨号器,但是当我调试应用程序时,它显示我的自定义用户界面。所以我错过了什么。 - Ravi Bhandari
请查看以下链接:linklink1link - Imtiyaz Khalani
1个回答

3

首先,按照以下步骤进行:

try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

在调用您的活动之前,请调用abortBroadcast()方法,并确保在intentFilter中设置优先级,如下所示:

 <intent-filter android:priority="99999" >
            <action android:name="android.intent.action.PHONE_STATE" />
  </intent-filter>

要在自定义用户界面接听来电,请执行以下操作:

answerButton = (Button) findViewById(R.id.pickup);
    answerButton.setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON);
            answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP,   KeyEvent.KEYCODE_HEADSETHOOK));
     context.sendOrderedBroadcast(answer, null);

        }
    });

并且要拒绝电话,需要执行以下操作:

rejectButton= (Button) findViewById(R.id.pickup);
    rejectButton= .setOnClickListener(new OnClickListener() {
        public void onClick(final View v) {
            Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);
    buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
    getApplicationContext().sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");


    }
});

希望这有所帮助。

尝试了您的代码,但仍然在来电时打开相同的默认拨号器。 - Ravi Bhandari
在使用您的代码并在调用我的自定义UI之前添加延迟后,我能够显示我的UI,现在我如何通过点击按钮接收呼叫。 - Ravi Bhandari
这是一个新问题 :) 请给我一个正确的答案并开始一个新问题。 - user957654
在添加延迟之前,你的代码单独运行不起来,但是添加了延迟后它可以正常工作。或者我可以说你的答案不完整。这并不是一个新问题,而是我的初始问题的一部分。 - Ravi Bhandari
我已经尝试了你的代码,但是在点击按钮接收调用时发现了异常。异常信息为-android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.ANSWER flg=0x10000000 }。 - Ravi Bhandari
尝试了您的代码后,我添加了另一段代码以使其正常工作,在接受呼叫之前,我必须设置意图Intent.ACTION_HEADSET_PLUG,这样它对我有效。因此,我将您的答案标记为正确。但是还有一件事,当前拒绝按钮无法工作。那么该怎么做呢? - Ravi Bhandari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接