中断来电的安卓

2023-09-06 04:15:02 作者:羙豹 lamΞ

如何打开的来电与答案自定义UI,拒绝按钮就可以了,我想给自定义用户界面,而不是默认的拨号器。 我使用低于code,但拨号器是开放的,我的行为是不打开:

How to open a custom UI on incoming call with answer and reject button on it,I want to show custom UI instead of default dialer. I am using below code but dialer is open and my activity is not opened:

<?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>

我有一个广播接收器,监听来电和接收器:

I have a broadcast receiver which listen for incoming call and in receiver is:

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

和$ C $下IncomingCall类:

and code for IncomingCall class is:

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

但不显示我的自定义用户界面。

but my custom UI is not displayed.

我也想对我的用户界面,以及如何通过点击该按钮,接收传入的呼叫按钮。 先谢谢了。

I also want button on my UI and how to receive a incoming call by click of that button. Thanks in advance.

编辑: - 更新我的code后,我能打开我的自定义UI,现在我想通过点击一个按钮来接收呼叫。如何做到这一点任何帮助。

edit:-- After updating my code I am able to open my custom UI, now I want to receive call by clicking a button on it. How to do this any help..

推荐答案

首先做到以下几点:

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

然后调用你的活动前致电 abortBroadcast()方法,并确保将优先在你的IntentFilter这样的:

then call the abortBroadcast() method before calling your activity and make sure to put a priority in your intentFilter like the following :

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

和解答自定义UI的到来的电话呼叫请执行以下操作:

and to answer the incoming phone call from the custom UI do the following :

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

        }
    });

和拒绝来电做到以下几点:

and to reject the call do the following :

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


    }
});

希望帮助

相关推荐