我们四大组件都是运行在UI线程上的,之前据我自己所看到的是主线程上有耗时的操作可能会造成ANR,今天做了一个实验,建立一个工程,主Activity有一个可以触发显示一个Toast的按钮,另外还有一个SQLiteOpenHelper的子类,另外一个继承ContentProvider,提供往数据库插入数据的操作:
01 | package cth.android.verifycontentprovider; |
03 | import android.content.ContentProvider; |
04 | import android.content.ContentUris; |
05 | import android.content.ContentValues; |
06 | import android.content.UriMatcher; |
07 | import android.database.Cursor; |
08 | import android.database.sqlite.SQLiteDatabase; |
09 | import android.net.Uri; |
11 | public class MyContentProvider extends ContentProvider { |
13 | private MySQLiteOpenHelper mySQLiteOpenHelper; |
14 | private static String authority = "cth.android.verifycontentprovider.MyContentProvider" ; |
15 | private static String path = "student" ; |
16 | private static final int student = 1 ; |
17 | private static final int students = 2 ; |
18 | private UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
20 | uriMatcher.addURI(authority, path + "/#" , student); |
21 | uriMatcher.addURI(authority, path, students); |
25 | public boolean onCreate() { |
26 | mySQLiteOpenHelper = new MySQLiteOpenHelper(getContext()); |
31 | public Cursor query(Uri uri, String[] projection, String selection, |
32 | String[] selectionArgs, String sortOrder) { |
38 | public String getType(Uri uri) { |
44 | public Uri insert(Uri uri, ContentValues values) { |
46 | SQLiteDatabase myDb = null ; |
47 | int flag = uriMatcher.match(uri); |
50 | myDb = mySQLiteOpenHelper.getWritableDatabase(); |
51 | long id = myDb.insert( "student" , null , values); |
52 | resultUri = ContentUris.withAppendedId(uri, id); |
60 | public int delete(Uri uri, String selection, String[] selectionArgs) { |
66 | public int update(Uri uri, ContentValues values, String selection, |
67 | String[] selectionArgs) { |
然后我另外建一个工程,通过getContentResolver获得对象后利用ContentProvider往数据库插1000条数据:
01 | package cth.android.verifycontentprovider_insert; |
03 | import android.app.Activity; |
04 | import android.content.ContentResolver; |
05 | import android.content.ContentValues; |
06 | import android.net.Uri; |
07 | import android.os.Bundle; |
08 | import android.util.Log; |
09 | import android.view.View; |
10 | import android.view.View.OnClickListener; |
11 | import android.widget.Button; |
13 | public class MainActivity extends Activity { |
15 | private Button btn_insertData; |
17 | protected void onCreate(Bundle savedInstanceState) { |
18 | super .onCreate(savedInstanceState); |
19 | setContentView(R.layout.activity_main); |
21 | btn_insertData = (Button) findViewById(R.id.btn_insertData); |
22 | btn_insertData.setOnClickListener( new OnClickListener() { |
25 | public void onClick(View v) { |
26 | new Thread( new Runnable() { |
30 | for ( int i = 1 ;i <= 1000 ;i++) { |
32 | Uri url = Uri.parse(uriString); |
33 | ContentValues values = new ContentValues(); |
35 | values.put( "name" , "Mike" + i); |
36 | values.put( "age" , i + 10 ); |
37 | ContentResolver cr = getContentResolver(); |
40 | } catch (InterruptedException e) { |
43 | Uri rowNum = cr.insert(url, values); |
44 | Log.i( "cth" ,rowNum.toString()); |
在插入1000条数据的过程中我打开包含有ContentProvider的APP,点击主Activity上的Button,结果能显示一个Toast,
意思就是其他进程利用ContentProvider机制往本进程的数据库插入数据,在插入数据的过程中不影响到本进程的UI操作,这是为什么,是本进程的ContentProvider在其他进程访问数据库时没在运行呢还是说这个ContentProvider虽说是存在于主线程,但其实是有安卓系统进行管理而不是主线程?