我试图在Android N中获取呼叫块号,我想知道给定的是块号是否(例如:-5554仿真器号)
已提供联系,短信,电话状态许可以允许访问块号,我遵循" Android开发人员"网站https://developer.android.com/reference/reference/reference/android/android/provider/blockednumbercontract.htmltract.html
但是我无法获得块号,我正在使用最新的Android Studio 2.2.2,并检查了Android N模拟器中的功能,我没有设备。这是我的代码。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Button onclick method to show the logs
public void displayBlockCursorCount(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String number = "5552";
if (BlockedNumberContract.canCurrentUserBlockNumbers(MainActivity.this)) {
if (BlockedNumberContract.isBlocked(MainActivity.this, number)) {
Log.e(TAG, "given number is blocked >>>>>> " + number);
}
}
}
}
}
我得到了 java.lang.securityException:呼叫者必须是系统,默认拨号程序或默认SMS应用程序。如果发表评论,请发表评论,并感谢您的预付款。
要访问阻止的联系人,您的应用应为默认情况下调用应用程序或消息应用程序,否则会引发安全性异常。
添加其他检查
private boolean isAppAsDefaultDialer() {
TelecomManager telecom = mContext.getSystemService(TelecomManager.class);
if (getApplicationContext().getPackageName().equals(telecom.getDefaultDialerPackage())) {
return true;
}
return false;
}
或检查消息来源https://android.googlesource.com/platform/packages/providers/blockednumberprovider//android-7.0.0.r1/src/src/src/com/com/android/android/android/android/providers/blockednumber/blockednumber/blockednumberprovider.java
并将您应用于Defaul Dialer
<intent-filter>
<action android:name="android.intent.action.DIAL"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="tel"/>
</intent-filter>