gpt4 book ai didi

java - Android 异常尝试调用虚方法 'int android.content.Context.checkPermission(java.lang.String, int, int)

转载 作者:行者123 更新时间:2023-12-05 04:11:27 25 4
gpt4 key购买 nike

我正在编写一个删除通话记录的应用程序。这是代码

public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);


if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Toast.makeText(context, "Call Idle State"+incomingNumber, Toast.LENGTH_SHORT).show();


DeleteCallLogByNumber("97********");
Toast.makeText(context, "Log deleted", Toast.LENGTH_SHORT).show();


}
} catch (Exception e) {
e.printStackTrace();
}

}

public void DeleteCallLogByNumber(String number) {
String queryString = "NUMBER=" + number;


if (ActivityCompat.checkSelfPermission(MainActivity.Context(), Manifest.permission.WRITE_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {

return;
}
MainActivity.ContentResolver().delete(CallLog.Calls.CONTENT_URI, queryString, null);
}
}

我遇到了异常

java.lang.NullPointerException:尝试在空对象引用上调用虚方法“int android.content.Context.checkPermission(java.lang.String, int, int)”

最佳答案

我自己想出来的

 public class PhoneStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

try {
System.out.println("Receiver start");
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);


if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
Toast.makeText(context, "Call Idle State"+incomingNumber, Toast.LENGTH_SHORT).show();


DeleteCallLogByNumber("97********", context);
Toast.makeText(context, "Log deleted", Toast.LENGTH_SHORT).show();


}
} catch (Exception e) {
e.printStackTrace();
}

}

public void DeleteCallLogByNumber(String number, Context context) {
String queryString = "NUMBER='" + number+"'";


if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {

return;
}
context.getContentResolver().delete(CallLog.Calls.CONTENT_URI, queryString, null);
}
}

关于java - Android 异常尝试调用虚方法 'int android.content.Context.checkPermission(java.lang.String, int, int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42594052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com