-1
我正在編程一個應用程序來刪除通話記錄。這裏是代碼Android異常嘗試調用虛擬方法'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********");
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 )」對空對象引用
不是* Downvoter *,但你應該在活動檢查的權限,而比broadcastreceiver – rafsanahmad007
@ rafsanahmad007我試過了,但它不會作爲後臺服務工作 –