Android开发 四大组件是否运行在主线程中?

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31383345/article/details/53303823

Android的四大组件是不是运行在主线程中?打了log,做个笔记.


先上图:



图没有说服力,因为log是我写的.哈哈.


1.activity

在onCreate()里面打印当前的线程的id和name

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startService = (Button) findViewById(R.id.startService);
		startService.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.e("test", "开启服务.....");
				startService(new Intent(MainActivity.this, MyService.class));
			}
		});
		sendBroadcast = (Button) findViewById(R.id.sendBroadcast);
		sendBroadcast.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.e("test", "发送广播.....");
				Intent intent = new Intent();
				intent.setAction("com.dingzhipeng");
				intent.putExtra("name", "dingzhipeng");
				MainActivity.this.sendBroadcast(intent);
			}
		});

		String threaName = Thread.currentThread().getName();
		long threaId = Thread.currentThread().getId();
		Log.e("test", "activity...");
		Log.e("test", "activity_threadId: " + threaId);
		Log.e("test", "activity_threadName: " + threaName);

	}

2.Service
同上,在onCreate()里面打印当前的线程的id和name

public class MyService extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		String threaName = Thread.currentThread().getName();
		long threaId = Thread.currentThread().getId();
		Log.e("test", "service_threadId: " + threaId);
		Log.e("test", "service_threadName: " + threaName);
	}
}


3.ContentProvider
manifest.xml中记得配置.log也是简单暴力

public class MyProvider extends ContentProvider {

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean onCreate() {
		String threaName = Thread.currentThread().getName();
		long threaId = Thread.currentThread().getId();
		Log.e("test", "内容提供者...");
		Log.e("test", "ContentProvider_threadId: " + threaId);
		Log.e("test", "ContentProvider_threadName: " + threaName);
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}



}


4.BroadcastReceiver
在广播接收者中打log
public class MyReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context arg0, Intent arg1) {
		String name = arg1.getExtras().getString("name");
		Log.e("test", "接收到name: " + name);
		String threaName = Thread.currentThread().getName();
		long threaId = Thread.currentThread().getId();
		Log.e("test", "BroadcastReceiver_threadId: " + threaId);
		Log.e("test", "BroadcastReceiver_threadName: " + threaName);
	}
}





总结:在activity,service,contentprovider的oncreate()中打印log,获取的当前线程都是主线程,在broadcastreceiver的onReceiver()中当前线程也是主线程,四大组件都是运行在主线程中.

demo源码下载


我的博客:http://blog.csdn.net/qq_31383345
欢迎批评!



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页