其他分享
首页 > 其他分享> > 中断Android中的来电

中断Android中的来电

作者:互联网

如何在接听电话上打开带有接听和拒绝按钮的自定义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);
        }
}

IncomingCall类的代码是:

@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.

我还需要UI上的按钮以及如何通过单击该按钮来接听来电.
提前致谢.

编辑: –
更新代码后,我可以打开我的自定义UI,现在我想通过单击按钮来接听电话.如何做到这一点有帮助.

解决方法:

首先执行以下操作:

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>

并通过自定义UI接听来电,请执行以下操作:

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


    }
});

希望能有所帮助

标签:telephonymanager,phone-call,android
来源: https://codeday.me/bug/20191122/2058017.html