无法使用BroadcastReceiver检测到来电号码[棉花糖] [英] Unable to detect incoming call number using BroadcastReceiver [ Marshmallow]
问题描述
每当我使用 BroadcastReceiver
接到来电时,我都试图记录电话号码.但是,当我拨打自己的电话号码时,我没有得到任何 log
.这是代码:
I am trying to log the phone number whenever I get an incoming call using BroadcastReceiver
. However, I do not get any log
when I dial to my number. This is the code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.abc.broadcast_phone" >
<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" >
<receiver android:name=".MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent("tuet");
sendBroadcast(intent);
}
}
MyPhoneReceiver.java
public class MyPhoneReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null)
{
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.e("DEBUG", phoneNumber);
}
}
}
}
我正在使用SDK 23进行编译,并且目标也是棉花糖,在这种情况下我应该使用运行时权限吗?还是我错过了什么?
I am compiling using SDK 23 and target is also Marshmallow, should I be using run time permission in this case? Or am I missing something?
EDIT-1
似乎问题仅与棉花糖有关,当我降级目标sdk版本时,此方法工作正常: targetSdkVersion 22
.
Seems like the problem is only associated with Marshmallow onwards, when I downgraded the target sdk version, this is working fine : targetSdkVersion 22
.
那么,如何调整它以便在SDK23 +及更高版本中正常工作?
So, how to tweak this to work in SDK23+ onwards?
推荐答案
每个 来电电话号码.如果应用程序没有 incoming call phone number. If application does not have 和 这篇关于无法使用BroadcastReceiver检测到来电号码[棉花糖]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! READ_PHONE_STATE
权限,则将传递一个空字符串作为参数.
READ_PHONE_STATE
permission, an empty string will be passed as an argument. READ_PHONE_STATE
是危险的当您定位到API 23时,必须在运行时要求高于棉花糖行为更改