我使用游标查询获取所有手机号码。我已启用严格模式获取此异常。
以下是我提取手机号码的代码:
public String queryForPhoneNumber(String contactId) {
final String[] projection = new String[] {
Phone.NUMBER,
Phone.TYPE,
};
final Cursor phone = getActivity().getContentResolver().query(
Phone.CONTENT_URI,
projection,
Data.DISPLAY_NAME + "=?",
new String[]{contactId},
null);
if(phone.moveToFirst()) {
final int contactNumberColumnIndex = phone.getColumnIndex(Phone.NUMBER);
//final int contactTypeColumnIndex = phone.getColumnIndex(Phone.TYPE);
while(!phone.isAfterLast()) {
mFmFmUserMobile = phone.getString(contactNumberColumnIndex);
//final int type = phone.getInt(contactTypeColumnIndex);
phone.moveToNext();
}
}
phone.close();
if(mFmFmUserMobile.contains(" ")){
mFmFmUserMobile.replace(" ", "");
}
//mFmFmUserMobile.substring(mFmFmUserMobile.length()-10,mFmFmUserMobile.length()
return mFmFmUserMobile;
}
例外:
04-25 09:51:15.035: D/StrictMode(27661): StrictMode policy violation; ~duration=31 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=2335 violation=2
04-25 09:51:15.035: D/StrictMode(27661): at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1135)
04-25 09:51:15.035: D/StrictMode(27661): at android.database.sqlite.SQLiteConnection.applyBlockGuardPolicy(SQLiteConnection.java:1041)
04-25 09:51:15.035: D/StrictMode(27661): at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:842)
04-25 09:51:15.035: D/StrictMode(27661): at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
04-25 09:51:15.035: D/StrictMode(27661): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
04-25 09:51:15.035: D/StrictMode(27661): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
04-25 09:51:15.035: D/StrictMode(27661): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
04-25 09:51:15.035: D/StrictMode(27661): at android.database.CursorToBulkCursorAdaptor.getBulkCursorDescriptor(CursorToBulkCursorAdaptor.java:148)
04-25 09:51:15.035: D/StrictMode(27661): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:118)
04-25 09:51:15.035: D/StrictMode(27661): at android.os.Binder.execTransact(Binder.java:404)
04-25 09:51:15.035: D/StrictMode(27661): at dalvik.system.NativeStart.run(Native Method)
04-25 09:51:15.035: D/StrictMode(27661): # via Binder call with stack:
04-25 09:51:15.035: D/StrictMode(27661): android.os.StrictMode$LogStackTrace
04-25 09:51:15.035: D/StrictMode(27661): at android.os.StrictMode.readAndHandleBinderCallViolations(StrictMode.java:1705)
04-25 09:51:15.035: D/StrictMode(27661): at android.os.Parcel.readExceptionCode(Parcel.java:1447)
04-25 09:51:15.035: D/StrictMode(27661): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:134)
04-25 09:51:15.035: D/StrictMode(27661): at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
04-25 09:51:15.035: D/StrictMode(27661): at android.content.ContentResolver.query(ContentResolver.java:461)
04-25 09:51:15.035: D/StrictMode(27661): at android.content.ContentResolver.query(ContentResolver.java:404)
04-25 09:51:15.035: D/StrictMode(27661): at com.newtglobal.fmfm.InviteFriendFragement.queryForPhoneNumber(InviteFriendFragement.java:954)
04-25 09:51:15.035: D/StrictMode(27661): at com.newtglobal.fmfm.InviteFriendFragement$ContactsAdapter.bindView(InviteFriendFragement.java:747)
04-25 09:51:15.035: D/StrictMode(27661): at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
04-25 09:51:15.035: D/StrictMode(27661): at android.widget.AbsListView.obtainView(AbsListView.java:2255)
04-25 09:51:15.035: D/StrictMode(27661): at android.widget.ListView.makeAndAddView(ListView.java:1790)
04-25 09:51:15.035: D/StrictMode(27661): at android.widget.ListView.fillDown(ListView.java:691)
04-25 09:51:15.035: D/StrictMode(27661): at android.widget.ListView.fillGap(ListView.java:655)
04-25 09:51:15.035: D/StrictMode(27661): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5143)
04-25 09:51:15.035: D/StrictMode(27661): at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3243)
04-25 09:51:15.035: D/StrictMode(27661): at android.widget.AbsListView.onTouchMove(AbsListView.java:3587)
04-25 09:51:15.035: D/StrictMode(27661): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3431)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.View.dispatchTouchEvent(View.java:7736)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2212)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2218)
04-25 09:51:15.035: D/StrictMode(27661): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
04-25 09:51:15.035: D/StrictMode(27661): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
04-25 09:51:15.035: D/StrictMode(27661): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
04-25 09:51:15.035: D/StrictMode(27661): at android.app.Activity.dispatchTouchEvent(Activity.java:2466)
04-25 09:51:15.035: D/StrictMode(27661): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:268)
04-25 09:51:15.035: D/StrictMode(27661): at c
任何帮助对我都有帮助。提前谢谢。
答案 0 :(得分:2)
您不应该在UI线程上执行工作,而是创建工作线程并在那里完成大部分工作。最简单的解决方案是使用AsyncTask:
public class StrictModeActivity extends AppCompatActivity {
private static final String TAG = StrictModeActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads()
.detectDiskWrites()
.detectNetwork()
.penaltyLog()
.penaltyDeath()
.build());
new QueryForPhoneNumberTask().execute("your contact display name");
}
private class QueryForPhoneNumberTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
String mFmFmUserMobile = null;
String contactId = strings[0];
final String[] projection = new String[]{
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
};
final Cursor phone = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.Data.DISPLAY_NAME + "=?",
new String[]{contactId},
null);
if (phone.moveToFirst()) {
final int contactNumberColumnIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
//final int contactTypeColumnIndex = phone.getColumnIndex(Phone.TYPE);
while (!phone.isAfterLast()) {
mFmFmUserMobile = phone.getString(contactNumberColumnIndex);
//final int type = phone.getInt(contactTypeColumnIndex);
phone.moveToNext();
}
}
phone.close();
if (mFmFmUserMobile != null && mFmFmUserMobile.contains(" ")) {
mFmFmUserMobile.replace(" ", "");
}
//mFmFmUserMobile.substring(mFmFmUserMobile.length()-10,mFmFmUserMobile.length()
return mFmFmUserMobile;
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG, result);
}
}
}