问题描述
- Android获取未接来电失败
-
通过点击按钮获取为借来电的信息,并以对话框形式显示未接来电的号码。虚拟机中测试通过,但在真机上运行时会直接强制退出。
代码:@Override public void onClick(View v) { final String[] projection = null; final String selection = null; final String[] selectionArgs = null; final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC"; Cursor cursor = null; try { cursor = getContentResolver().query( Uri.parse("content://call_log/calls"), projection, selection, selectionArgs, sortOrder); while (cursor.moveToNext()) { String callLogID = cursor.getString(cursor .getColumnIndex(android.provider.CallLog.Calls._ID)); String callNumber = cursor.getString(cursor .getColumnIndex(android.provider.CallLog.Calls.NUMBER)); String callDate = cursor.getString(cursor .getColumnIndex(android.provider.CallLog.Calls.DATE)); String callType = cursor.getString(cursor .getColumnIndex(android.provider.CallLog.Calls.TYPE)); String isCallNew = cursor.getString(cursor .getColumnIndex(android.provider.CallLog.Calls.NEW)); if (Integer.parseInt(callType) == (CallLog.Calls.MISSED_TYPE) && Integer.parseInt(isCallNew) > 0) { Toast.makeText(getApplication(), "Missed Call Found: " + callNumber, Toast.LENGTH_SHORT).show(); } } } catch (Exception e) { e.printStackTrace(); } finally { cursor.close(); } }
权限列表中添加的权限为:
时间: 2024-11-02 14:18:57