Android 四大组件你都知道吗
四大组件分别是:活动,服务,广播,内容提供器。
先来介绍下,对其有个大概的了解。
1.活动就是一个用户交互界面 2.服务,你可以理解为后台组件,用于服务活动的;
3.广播,是一个可以发送一个消息,然后再去收这个消息。这个消息可以是系统发给你的,也可以是你自己发,让其自己接收。
4.内容提供器,实现了共享数据的组件,用于不同应用之间共享数据。
接下是每个都介绍一遍吧。
-
活动:
简单的说就是一个与用户交互的一个界面,一般和布局文件绑定,这样才能显示页面内容。说到活动activity,那肯定要说说activity的生命周期。引用下我在网上找到的一张图吧,大概的流程就是这样的。
大概就是
①当你进入这个activity的时候,它会走onCreate() -> onStart() - > onResume().
②当你从当前页面跳转到另外一个页面的时候,它会走onPause()–>onStop()
③当你从另外一个页面返回当前页面,它会走onRestart()–>onStart()–>onResume()
④最后当你退出结束这个页面,它会走onPause()–>onStop()–>onDestroy()
2.服务
服务就是service,它是服务于活动的,为活动处理一些耗时的操作,比如下载。
网上有很多已经讲了关于service的介绍了,我这里就简单的说下,它怎么用吧。
service主要有2种启动方式。一种是startService,另外一种是bindService.
2种的区别就是startService需要在你的配置文件中去注册这个service,最后也需要你主动去结束活动,不然它会一直运行在后台;bindService,不需要去配置文件注册,你只需要去绑定活动就行,它可以主动也解除绑定,然后当你绑定的活动结束后,它也会自动解除绑定。
- 第一种方式(startService)
需要现在配置文件注册:
<service android:name=".ceshi.ceshiService"/>
然后开启服务和结束服务
//启动Service
Intent intentStart = new Intent(this, ceshiService.class);
startService(intentStart);
//停止Service
Intent intenStop = new Intent(this, ceshiService.class);
stopService(intenStop);
service模块
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
public class ceshiService extends Service {
@Override
public void onCreate() {
super.onCreate();
//进入初始化
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//如果多次开启活动,每次都会走这里
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
//服务结束
}
}
- 第二种方式(bindService)
private ceshi2Service.ceshi2Binder mBinder;
private void bindService() {
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//开始
mBinder = (ceshi2Service.ceshi2Binder) iBinder;
mBinder.toTask();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
//结束
}
};
//开始
Intent intenBindStart = new Intent(this, ceshi2Service.class);
bindService(intenBindStart, conn, BIND_AUTO_CREATE);
//结束
unbindService(conn);
}
启动活动需要一个ServiceConnection,用于和service之间通信。结束可以直接手动结束unbindService,也可以退出活动后自动结束。
service模块
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
public class ceshi2Service extends Service {
private ceshi2Binder mBinder;
@Override
public void onCreate() {
super.onCreate();
//进入初始化
mBinder = new ceshi2Binder();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//如果多次开启活动,每次都会走这里
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
if (mBinder != null) {
return mBinder;
}
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
//服务结束
}
class ceshi2Binder extends Binder {
public void toTask() {
//toTask
}
}
}
3.广播
这块就是接收信息用的,可以是系统发的信息,也可以是自己发送的信息,自己接收。对这块的详细解释就不写了,也只写下它的用法吧。
//因为篇幅问题,这里讲解下静态注册和动态注册,然后只讲自己发送和自己接收这块。
①静态注册
先创建接收的广播
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ceshiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//接收信息
String mes = intent.getStringExtra("mess");
}
}
先去配置文件注册
<receiver android:name=".ceshi.ceshiReceiver"/>
然后去发送广播
Intent intent = new Intent();
intent.putExtra("mess", "这是发送信息");
sendBroadcast(intent);
②动态广播,相对于静态广播的常驻后台,动态广播需要去主动销毁广播
先去动态创建广播接收器,再去注册它
private MyReceiver myReceiver;
private void initReceiver(){
myReceiver = new MyReceiver();
//实例化IntentFilter对象
IntentFilter filter = new IntentFilter();
//这里相当于一个标识
filter.addAction("myFilter");
registerReceiver(myReceiver,filter);
}
// 创建动态广播接收器
class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//接收信息
String mes = intent.getStringExtra("mess");
}
}
最后是发送广播
private void toSendReceiver() {
Intent intent = new Intent();
intent.setAction("myFilter");
intent.putExtra("mess", "这是发送信息");
sendBroadcast(intent);
}
最后你还需要在结束的时候销毁它
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(myReceiver);
}
- 最后来讲下内容提供器吧,这个是用于不同应用之间的共享数据。当然是有限制的,你只能读取到应用开放的内容。
规则就是content://<app 包名>/<表名>
这里讲解下关于读取内容吧,关于创建这里就不讲来,可以参考https://www.jianshu.com/p/a5de880973f3
这里讲的是关于读取手机系统的联系人信息。因为这个信息读取也是手机系统开发的,我们才能读取,所以读取下面的内容,你需要先去申请权限。
关于读取就是下面的代码。
//读取联系人
private void readPhoneList() {
Cursor cursor = null;
try {
//查询联系人
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
while (cursor.moveToNext()) {
//读取联系人姓名
String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//读取联系人手机号
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if (cursor != null) {
cursor.close();
}
}
看了上面的代码,你可以会很疑惑,这和上面将的content://<app 包名>/<表名>有什么联系。
//查询联系人
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
这就是读取手机信息。然后你按ctrl+左键,点击下面的代码,可以看到这个的详细信息
ContactsContract.CommonDataKinds.Phone.CONTENT_URI。
之后你会看到
/**
* The content:// style URI for all data records of the
* {@link #CONTENT_ITEM_TYPE} MIME type, combined with the
* associated raw contact and aggregate contact data.
*/
public static final Uri CONTENT_URI = Uri.withAppendedPath(Data.CONTENT_URI,
"phones");
还是有点不像之前的规则,然后再ctrl+左键CONTENT_URI。 你会看到
/**
* The content:// style URI for this table, which requests a directory
* of data rows matching the selection criteria.
*/
public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "data");
之后再次点击AUTHORITY_URI
/** The authority for the contacts provider */
public static final String AUTHORITY = "com.android.contacts";
/** A content:// style uri to the authority for the contacts provider */
public static final Uri AUTHORITY_URI = Uri.parse("content://" + AUTHORITY);
最后就是和之前的规则一样了。
Ok.今天的四大组件就讲到这里。如果有什么不懂的,或者一起探讨技术的,可以加我qq:2019793673。或者加q群:1033629708一起学习探讨技术。
欢迎关注我的公众号
期待的你关注