我在onReceive方法中有一个方法调用。
public void onReceive(Context context, Intent intent) {
SaveCallLog();
}
public boolean SaveCallLog()
{
info = new TelephonyInfo();
imei1 = info.getImeiSIM1();
imei2 = "";
if (info.isDualSIM()) {
imei2 = info.getImeiSIM2();
}
Uri uri = Uri.parse("content://call_log/calls");
ContentResolver contentResolver = mContext.getContentResolver();
// here change where condition to get only those call logs those are not sent before
Cursor cursor = contentResolver.query(uri, null, null, null, "date DESC");
{
if (cursor != null && cursor.getCount() > 0)
{
while (cursor.moveToNext())
{
phNumber = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
callType = cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE));
callDate = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
callDuration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));
dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode)
{
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
default:
dir = "REJECTED";
}
// here save the call log in your database to send them later
//String QS = "call&ImeiNo=" + "imei1" + "&ph=" + phNumber + "&duration=" + callDuration + "&date=" + callDate + "&cType=" + dir + "";
}
cursor.close();
Log.e("cursor", "here it is");
}
return true;
}
---->在我到达时进行调试
ContentResolver contentResolver = mContext.getContentResolver();
它把我从这个页面转出来。
我已经尝试过多解决这些问题但是无法理解发生这些问题的问题
答案 0 :(得分:0)
mContext
未初始化。它为您提供NullPointerException
。
像这样更改您的代码..
public void onReceive(Context context, Intent intent) {
mContext=context;
SaveCallLog();
}
在清单
中添加此权限<uses-permission android:name="android.permission.READ_CALL_LOG"/>
基于OP要求..
像这样更改您的代码并尝试..
// here change where condition to get only those call logs those are not
// sent before
Cursor cursor = contentResolver.query(uri, null, null, null,
"date DESC");
try {
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
phNumber = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.NUMBER));
callType = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.TYPE));
callDate = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.DATE));
callDuration = cursor.getString(cursor
.getColumnIndex(CallLog.Calls.DURATION));
dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
default:
dir = "REJECTED";
}
} while (cursor.moveToNext());
}
}
} catch (Exception e) {
// TODO: handle exception
}