android四大组件(Activity,Service,BroadcastReceiver,Content Provider)

来源:互联网 发布:信息管理平台软件 编辑:程序博客网 时间:2024/06/08 06:32

安卓的四大组建,五大布局

Android四大组件:Activity、Service、Broadcast Receiver、Content Provider。

1、 Activity
可以去本人博客activity了解学习

是Android程序与用户交互的窗口,从视觉效果来看,一个Activity占据当前的窗口,响应所有窗口事件,具备有控件,菜单等界面元素。从内部逻辑来看,Activity需要为了保持各个界面状态,需要做很多持久化的事情,还需要妥善管理生命周期,和一些转跳逻辑。

2、Service

 就是剥离了界面的Activity,它们在很多Android的概念方面比较接近,都是封装一个完整的功能逻辑,通常都是后台长时间运行,接受上层指令,完成相关事务的模块。定义好需要接受的Intent,提供同步或异步的接口,在上层绑定了它后,通过这些接口(很多时候都是RPC的...)进行通信。

3、Broadcast Receiver

接收一种或者多种Intent跳转做触发事件,接受相关消息,做一些简单的处理,转换成一条Notification,统一了Android的事件广播模式。

4、 Content Provider

提供第三方应用数据的访问方案。可以派生ContentProvider类,对外提供数据,像数据库一样进行选择排序,屏蔽内部数据的存储细节,向外提供统一的接口模型,大大简化了上层应用,对数据的整合提供了更方便的途径。

Android 五大布局: FrameLayout(框架布局),LinearLayout (线性布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局)。

1、FrameLayout

    所有东西依次都放在左上角,会重叠,这个布局比较简单,也只能放一点比较简单的东西。

2、LinearLayout

    每一个LinearLayout里面又可分为垂直布局(android:orientation="vertical")和水平布局(android:orientation="horizontal" )。当垂直布局时,每一行就只有一个元素,多个元素依次垂直往下;水平布局时,只有一行,每一个元素依次向右排列。

3、 AbsoluteLayout

   绝对布局用X,Y坐标来指定元素的位置,这种布局方式也比较简单,但是在屏幕旋转时,往往会出问题,而且多个元素的时候,计算比较麻烦。

4、RelativeLayout

   相对布局可以理解为某一个元素为参照物,来定位的布局方式。主要属性有:相对于某一个元素android:layout_below、      android:layout_toLeftOf相对于父元素的地方android:layout_alignParentLeft、android:layout_alignParentRigh

5、TableLayout

   每一个TableLayout里面有表格行TableRow,TableRow里面可以具体定义每一个元素。   每一个布局都有自己适合的方式,这五个布局元素可以相互嵌套应用,做出美观的界面。

BroadcastReceiver(广播接收器)

1、新建项目
在布局文件中加几个按钮

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" tools:context=".MainActivity">   <Button       android:id="@+id/button_send"       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="发送广播"/>    <Button        android:id="@+id/button_alarm"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="闹钟"/>    <Button        android:id="@+id/button_cancel"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="取消闹钟"/></LinearLayout>

2、写一个MyReceiver类,继承BroadcastReceiver,重写onReceive方法

public class MyReceiver extends BroadcastReceiver {    @Override    public void onReceive(Context context, Intent intent) {        Toast.makeText(context, "我收到了广播", Toast.LENGTH_SHORT).show();    }}

3、MainActivity

public class MainActivity extends Activity {    private Button mButtonSend;    private MyReceiver myReceiver;    private  Button mButtonAlarm;    private Button mButtonCancel;    private AlarmManager mAlarmManager;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //在代码中注册广播接收器,不需要在manifest中声明        myReceiver = new MyReceiver();        IntentFilter filter = new IntentFilter();        filter.addAction("com.myreceiver");        registerReceiver(myReceiver, filter);        mButtonSend = (Button) findViewById(R.id.button_send);        mButtonSend.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setAction("com.myreceiver");                sendBroadcast(intent);            }        });        //下面是闹钟按钮点击事件        mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);        mButtonAlarm = (Button) findViewById(R.id.button_alarm);        mButtonAlarm.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setAction("com.myreceiver");                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0x23,intent,PendingIntent.FLAG_UPDATE_CURRENT);                mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+5000,3000,pendingIntent);            }        });        //取消闹钟        mButtonCancel = (Button) findViewById(R.id.button_cancel);        mButtonCancel.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent();                intent.setAction("com.myreceiver");                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0x23,intent,PendingIntent.FLAG_UPDATE_CURRENT);                mAlarmManager.cancel(pendingIntent);            }        });    }    @Override    protected void onDestroy() {        super.onDestroy();        unregisterReceiver(myReceiver);    }}

这里写图片描述

Service

1、在layout布局文件中布局两个按钮
2、写一个MyReceiver类,继承Receiver,重写方法

public class MyService extends Service {    @Override    public void onCreate() {        super.onCreate();        Log.d("","onCreate");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d("","onStartCommand");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();        Log.d("", "onDestroy");    }    @Nullable    @Override    public IBinder onBind(Intent intent) {        return null;    }}

3、需要在manifest文件中注册声明

 </activity>        <service android:name=".MyService"></service>    </application>

4、activity

public void onClick(View v) {        switch (v.getId()){            case R.id.button_start:                Intent intent = new Intent(getApplicationContext(),MyService.class);                startService(intent);//启动服务,onCreate>onStartCommand                break;            case R.id.button_stop://按停止按钮,停止服务,onDestroy                Intent intent1 = new Intent(getApplicationContext(),MyService.class);                stopService(intent1);                break;            default:                break;        }    }
0 0