【问题标题】:Android detect incoming call on backgroundAndroid在后台检测来电
【发布时间】:2016-03-09 01:42:13
【问题描述】:

我在检测来电时遇到问题。我尝试了很多教程,但它不起作用。我真的不知道哪里错了。

我已添加权限 READ_PHONE_STATE,但仍在 Android 设备监视器中看到此警告(从它调用时):

Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com.development.test.rejectcall/.background.IncomingCall requires android.permission.READ_PRIVILEGED_PHONE_STATE due to sender android (uid 1000)

Permission Denial: receiving Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } to com.development.test.rejectcall/.background.IncomingCall requires android.permission.READ_PHONE_STATE due to sender android (uid 1000)

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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=".background.IncomingCall">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

IncomingCall.java

package com.development.test.rejectcall.background;

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

public class IncomingCall extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Phone state call hook", Toast.LENGTH_LONG).show();
        Log.d("mujlog", "phoneNumber received");
    }
}

MainActivity.java

package com.development.test.rejectcall;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("mujlog", "Created");
        Toast.makeText(this, "started", Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

【问题讨论】:

标签: android android-permissions phone-call android-broadcastreceiver incoming-call


【解决方案1】:

不要使用 compileSdkVersion、targetSdkVersion、buildToolsVersion 23 或更高版本。因为在 API 级别 23 中,广播接收器中的操作 PHONE_STATE 授予权限被拒绝 使用 21 或更低。以下配置适用于我

编译SdkVersion 21
buildToolsVersion "21.1.2"
targetSdkVersion 21

【讨论】:

  • 当我在“项目结构”中更改它时,出现以下错误:pastebin.com/3FrnqWTm
  • 它正在工作,如何处理android studio不使用compileSdkVersion 23?即使我选择了 21,它也会自动添加到项目中
  • 降低版本不是解决办法,好像有人要在21以上使用,这个解决方案没有用。
猜你喜欢
  • 1970-01-01
  • 2017-05-27
  • 1970-01-01
  • 2014-06-09
  • 2020-10-20
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多