Android 接收器忽略 NEW_OUTGOING_CALL [英] Android receiver ignore NEW_OUTGOING_CALL

查看:46
本文介绍了Android 接收器忽略 NEW_OUTGOING_CALL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有意图过滤器的 Broadcastreceiver 来捕获 android.intent.action.NEW_OUTGOING_CALL.

I have a with Broadcastreceiver with an intent-filter to catch android.intent.action.NEW_OUTGOING_CALL.

我阅读了许多教程并在此处回答了有关处理 NEW_OUTGOING_CALL 意图的问题,但我无法使这件事起作用.我的目标是记录我的 Broadcastreceiver 收到的意图 android.intent.action.NEW_OUTGOING_CALL.我无法让这件简单的事情奏效.

I read many tutorials and answer here about handling NEW_OUTGOING_CALL intent, but I was unable to make this thing works. My goal is to log intent android.intent.action.NEW_OUTGOING_CALL received by my Broadcastreceiver. I'm unable to make this simple thing works.

这是我的代码:

Manifest.xml

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.vannus.broadcasttest">

    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".TestReceiver">
            <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

这是广播接收器(TestReceiver.java)的代码

This is the code for the Broadcastreceiver (TestReceiver.java)

package net.vannus.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class TestReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.w("###TEST###",intent.getAction());
    }
}

该项目还包含一个没有功能的空 MainActivity.执行项目的主要活动是启动,但是当我尝试拨打或接听电话时没有写入日志.我在模拟器 (Android 7) 和摩托罗拉 G4 手机 (Android 6.0) 中测试了代码,但 logcat 上没有记录任何内容.我使用的是 Android Studio 2.3

The project also contains an empty MainActivity with no functionality. Executing the project the main activity is launch, but no log are written when I try to make or receive calls. I tested the code in the emulator (Android 7) and on a Motorola G4 phone (Android 6.0), but nothing was logged on logcat. I'm using Android Studio 2.3

我做错了什么?

谢谢

万纳斯

推荐答案

Android Oreo 的问题在于,对于危险"类型权限(其中 PROCESS_OUTGOING_CALLS 是其中之一),仅在清单中声明是不够的.您需要特别提示用户授予此权限.这可以使用此处讨论的框架来完成:

From android Oreo the problem is that for "dangerous" type permissions (of which PROCESS_OUTGOING_CALLS" is one), it is not enough to declare then in the manifest. You need to specifically prompt the user to grant this permission. This can be done using the framework as discussed here:

https://developer.android.com/training/permissions/requesting

具体来说,您基本上需要使用一组您实际需要的权限来调用requestPermissions",它会要求用户批准它们.

Specifically you need to basically call "requestPermissions" with an array of the permissions you actually need and it will ask the user to approve them.

我个人觉得这很烦人,但我明白背后的原理.

Personally I find it quite annoying but I get the rationale behind it.

这篇关于Android 接收器忽略 NEW_OUTGOING_CALL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆