安卓入门(第二篇 )——四大组件

来源:互联网 发布:掌握系统优化的方法 编辑:程序博客网 时间:2024/05/16 00:37

     

     前几篇讲解了一下 我项目中用的框架  为了大多数初学者   我决定从头开始给大家讲解一下安卓  希望我的博客能带给你最大的帮助

好,安卓的历史这个话题咱们就不讨论了 大家网上百度稍微了解一下就可以了

  一.安卓中的四大组件

      1. Activity

      2.Service

      3.Content provider

      4.Broadcast receiver


下面我们详细解释一下这四大组件

 1) Activity

  

  a.一个Activity通常就是一个单独的屏幕也可以称之为一个窗口 

  b.它上面可以显示一些控件可以监听处理用户的事件作出相应的动作

  c.Activity之间通过Intent进行数据传递

  d.每一个Activity必须在配置文件中进行注册 不然程序会崩掉


注册的代码 

android:name="包名+.MainActivity"


2)Service

一个Service 是一段长生命周期的,没有用户界面的程序,很简单的例子就是音乐播放器

我们知道手机按home键 但是我们的音乐还在播放 没错 这个就是一个Service最简单的例子

  Service分为两种 

   启动的(start)和绑定(bind)的 这两种的区别是 

started service(启动的)是由其他组件调用startService()方法启动的,当服务是started状态时,可以在后台无限期运行,并且生命周期不受启动他的组件的影响,即使启动服务的组件已经被销毁。因此,服务需要在完成任务后调用stopSelf()方法停止,或者由其他组件调用stopService()方法停止

 bindService() 调用Service的组件和Service绑定在了一起,当调用组件销毁或者退出时,我们的Service也随之停止


实现Service步骤

1.继承Service 实现里面的方法

2.在配置文件中注册服务

     <service name=".MyService"/>

3.通过Context.startService(),或者Context.bindService(),启动服务


public class CalculateService extends Service {    private static final String TAG = "SERVICE";    public CalculateService() {    }    @Override    public void onCreate() {        super.onCreate();        Log.e(TAG, "Oncreat");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.e(TAG, "onStartCommand");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        super.onDestroy();        Log.e(TAG, "onDestroy");    }    @Override    public boolean onUnbind(Intent intent) {        Log.e(TAG, "onUnbind");        return super.onUnbind(intent);    }


我只是选择的实现了里面的方法 其中onStartCommand是必须实现的




三.Content provider(内容提供者)

  

我们可以理解为Content provider实现了数据的共享

开发人员不会直接使用ContentProvider类的对象,大多数是通过ContentResolver对象实现对ContentProvider的操作。

例如:通讯录数据被多个应用程序使用,且必须存储在一个内容提供者中。它的好处是统一数据访问方式 我们看一小段代码  


ContentResolver mContentResolver = MainActivity.this
.getContentResolver();


// 只查询jpeg和png的图片
Cursor mCursor = mContentResolver.query(mImageUri, null,
MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Images.Media.MIME_TYPE + "=?",
new String[] { "image/jpeg", "image/png" },
MediaStore.Images.Media.DATE_MODIFIED);


利用ContentProvider查询手机中的图片 这只是一个简单的例子  帮助大家理解一下 



四.Broadcast receiver(广播接收器)


我们可以简单的理解为一个过滤器 比如说从医院药房中拿出适合自己病症的药品,广播

接收器没有用户界面 广播接收器可以启动一个Servicer或者一个Activity响应他们的接收到

的消息

注册方式:

动态注册:

  IntentFilter filter = new IntentFilter();       filter.addAction(Intent.ACTION_MEDIA_REMOVED);       filter.addAction(Intent.ACTION_MEDIA_EJECT);       filter.addAction(Intent.ACTION_MEDIA_MOUNTED);       filter.addDataScheme("file");       registerReceiver(sdcardStateReceiver, filter);
这种类型的广播是非常驻类型的  当应用程序关闭或者没有的时候 这种类型的广播就没有了

  


静态注册

<receiver android:name=".receiver.BootCompleteReceiver" >  <intent-filter android:priority="1000" >      <action android:name="android.intent.action.BOOT_COMPLETED" />  </intent-filter></receiver>
这种类型的广播是常驻行的  在配置文件中注册 你的应用程序关闭了,如果有广播信息来,你写的广播接收器同样的能接收到

 


下面我们看一下整个完整的代码 一个很简单的例子  

  1.      
  2.         //注册广播  
  3.         registerBoradcastReceiver();  
  4.           
  5.         LinearLayout mLinearLayout = new LinearLayout(this);  
  6.         mBtnMsgEvent = new Button(this);  
  7.         mBtnMsgEvent.setText("发送广播");  
  8.         mLinearLayout.addView(mBtnMsgEvent);  
  9.         setContentView(mLinearLayout);  
  10.           
  11.         mBtnMsgEvent.setOnClickListener(new OnClickListener() {  
  12.             @Override  
  13.             public void onClick(View v) {  
  14.                 Intent mIntent = new Intent(ACTION_NAME);  
  15.                 mIntent.putExtra("yaner""发送广播,相当于在这里传送数据");  
  16.                   
  17.                 //发送广播  
  18.                 sendBroadcast(mIntent);  
  19.             }  
  20.         });  
  21.     }  
  22.       
  23.     private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){  
  24.         @Override  
  25.         public void onReceive(Context context, Intent intent) {  
  26.             String action = intent.getAction();  
  27.             if(action.equals(ACTION_NAME)){  
  28.                 Toast.makeText(Test.this"处理action名字相对应的广播"200);  
  29.             }  
  30.         }  
  31.           
  32.     };  
  33.       
  34.     public void registerBoradcastReceiver(){  
  35.         IntentFilter myIntentFilter = new IntentFilter();  
  36.         myIntentFilter.addAction(ACTION_NAME);  
  37.         //注册广播        
  38.         registerReceiver(mBroadcastReceiver, myIntentFilter);  
  39.     }  
  40. }  

我们看25行就是 我们接收广播的方法  我们这里注册没有在配置文件中注册 我们选择的是非常驻行的广播   18行是我们发送广播  我们还可以在不同的Activity中实现广播  我们自己去研究 基本的用法告诉大家了  大家要学会举一反三

  总结:这就是我们讲的四大组件  对于初学者来说经常用到的一个Activity和Service其他两个用的频率相对于这两个有点。。。但是这四个都必须掌握





0 0
原创粉丝点击