Android四大组件解析

来源:互联网 发布:正道集团 知乎 编辑:程序博客网 时间:2024/05/19 22:48

Android 应用程序由四个模块构造而成:Activity、Intent 、Content Provider 、Service

下面简单介绍一下如下模块的含义:


1、Activity  "活动"
一个Activity就是单独的屏幕,每一个活动都被实现为一个独立的类,并且从活动基类中继承而来,活动类将会显示由视图控件组成的用户接口并对事件作出响应。代表一个用户能看到的屏幕,主要用于处理应用程序的整体性工作--如系统事件,用户显示等各种显示设计。

2、Intent  "请求"
用于不同的Activity之间切换使用,用于描述应用的功能。
其描述结构中,最重要的两个部分:动作(MAIN,VIEW,PICK,EDIT etc)及动作所对应的数据(URI..)

3、Content Provider 
用于将数据存储于文件及SQLITE数据库中或者其它有效设备中。提供应用数据与其它应用共享时使用。

4、Seriver "服务"
生命周期长但没有用户界面的程序,可一直在系统中执行,为多个客户端提供服务。

下面给出将这几者同时使用的例子,加入这些概念的理解与使用:


首先建立三个java文件,代码分别为:

主的Acitivy应用:

[java] view plaincopyprint?
  1. package com.example.com.test.app;  
  2.   
  3. import android.os.Bundle;  
  4. import android.provider.ContactsContract;  
  5. import android.provider.ContactsContract.PhoneLookup;  
  6. import android.app.Activity;  
  7. import android.content.ContentResolver;  
  8. import android.content.Intent;  
  9. import android.database.Cursor;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity {  
  17.     private final static String TAG = "MainActivity";  
  18.       
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);           
  22.           
  23.         /* 设置显示main.xml布局 */  
  24.         setContentView(R.layout.activity_main);  
  25.           
  26.         /* findViewById(R.id.button1)取得布局main.xml中的button1 */  
  27.         Button button = (Button) findViewById(R.id.button1);  
  28.         /* 监听button的事件信息 */  
  29.         button.setOnClickListener(new Button.OnClickListener() {  
  30.             public void onClick(View v) {  
  31.                 /* 新建一个Intent对象 */  
  32.                 Intent intent = new Intent();  
  33.                 /* 指定intent要启动的类 */  
  34.                 intent.setClass(MainActivity.this, SecondActivity.class);  
  35.                 /* 启动一个新的Activity */  
  36.                 startActivity(intent);  
  37.                 /* 关闭当前的Activity */  
  38.                 MainActivity.this.finish();  
  39.             }  
  40.         });  
  41.           
  42.         //从main.xml布局中获得Button对象  
  43.         Button button_start = (Button)findViewById(R.id.PlayMusic);  
  44.         Button button_stop  = (Button)findViewById(R.id.StopMusic);  
  45.         Button button_cp    = (Button)findViewById(R.id.CProvider);  
  46.           
  47.         //设置按钮(Button)监听  
  48.         button_start.setOnClickListener(startplay);  
  49.         button_stop.setOnClickListener(stopplay);  
  50.         button_cp.setOnClickListener(cplisten);  
  51.     }  
  52.       
  53.     // 开始播放  
  54.     private OnClickListener startplay = new OnClickListener() {  
  55.         public void onClick(View v) {  
  56.             // 开启Service  
  57.             startService(new Intent("com.test.Android.MUSIC"));  
  58.         }  
  59.     };  
  60.       
  61.     // 停止播放  
  62.     private OnClickListener stopplay = new OnClickListener() {  
  63.         public void onClick(View v) {  
  64.             // 停止Service  
  65.             stopService(new Intent("com.test.Android.MUSIC"));  
  66.         }  
  67.     };  
  68.       
  69.     // 测试 Content Provider  
  70.     private OnClickListener cplisten = new OnClickListener() {  
  71.         public void onClick(View v) {  
  72.             String string = "";   
  73.               
  74.             //得到ContentResolver对象  
  75.             ContentResolver cr = getContentResolver();    
  76.             //取得电话本中开始一项的光标  
  77.             Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, nullnullnullnull);  
  78.               
  79.             // 向下移动一下光标  
  80.             while (cursor.moveToNext()) {  
  81.                 // 取得联系人名字  
  82.                 int nameFieldColumnIndex = cursor  
  83.                         .getColumnIndex(PhoneLookup.DISPLAY_NAME);  
  84.                 String contact = cursor.getString(nameFieldColumnIndex);  
  85.                 // 取得电话号码  
  86.                 int numberFieldColumnIndex = cursor  
  87.                         .getColumnIndex(PhoneLookup.NUMBER);  
  88.                 String number = null//cursor.getString(numberFieldColumnIndex);  
  89.   
  90.                 string += (contact + ":" + number + "\n");  
  91.                 Log.i(TAG,"telephone string: " + string );  
  92.             }  
  93.               
  94.             cursor.close();  
  95.               
  96.             //设置TextView显示的内容  
  97.             DisplayText(string);  
  98.         }  
  99.     };  
  100.       
  101.     /* 显示Toast */  
  102.     private void DisplayText(String str) {  
  103.         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();  
  104.     }  
  105. }  

切换的Activity应用:

[java] view plaincopyprint?
  1. package com.example.com.test.app;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8.   
  9. public class SecondActivity extends Activity{  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState)  
  12.     {  
  13.         super.onCreate(savedInstanceState);  
  14.         /* 设置显示second.xml布局 */  
  15.         setContentView(R.layout.activity_second);  
  16.           
  17.         /* findViewById(R.id.button1)取得布局main.xml中的button1 */  
  18.         Button button = (Button) findViewById(R.id.button1);  
  19.         /* 监听button的事件信息 */  
  20.         button.setOnClickListener(new Button.OnClickListener() {  
  21.             public void onClick(View v)  
  22.             {  
  23.                 /* 新建一个Intent对象 */  
  24.                 Intent intent = new Intent();  
  25.                 /* 指定intent要启动的类 */  
  26.                 intent.setClass(SecondActivity.this, MainActivity.class);  
  27.                 /* 启动一个新的Activity */  
  28.                 startActivity(intent);  
  29.                 /* 关闭当前的Activity */  
  30.                 SecondActivity.this.finish();  
  31.             }  
  32.         });  
  33.     }     
  34. }  

后台Service服务程序:

[java] view plaincopyprint?
  1. package com.example.com.test.app;  
  2.   
  3. import android.app.Service;  
  4. import android.content.Intent;  
  5. import android.media.MediaPlayer;  
  6. import android.os.IBinder;  
  7.   
  8. public class MusicService extends Service {  
  9.     // MediaPlayer对象  
  10.     private MediaPlayer player = null;  
  11.   
  12.     public IBinder onBind(Intent arg0) {  
  13.         return null;  
  14.     }  
  15.   
  16.     public void onStart(Intent intent, int startId) {  
  17.         super.onStart(intent, startId);  
  18.         // 这里可以理解为装载音乐文件  
  19.         player = MediaPlayer.create(this, R.raw.jiangnanstyle);  
  20.         // 循环播放  
  21.         player.setLooping(true);  
  22.         // 开始播放  
  23.         player.start();  
  24.     }  
  25.   
  26.     public void onDestroy() {  
  27.         super.onDestroy();  
  28.         // 停止音乐-停止Service  
  29.         player.stop();  
  30.     }  
  31. }  

AndroidManifest.xml应用中全局描述文件:

[html] view plaincopyprint?
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     package="com.example.com.test.app"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="10"  
  8.         android:targetSdkVersion="15" />  
  9.       
  10.     ##### 可以访问的权限设定  
  11.     <uses-permission   
  12.     android:name="android.permission.READ_CONTACTS">  
  13.     </uses-permission>  
  14.       
  15.     <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">  
  16.         #### 主Activity  
  17.         <activity  
  18.             android:name=".MainActivity"  
  19.             android:label="@string/title_activity_main" >  
  20.             <intent-filter>  
  21.                 <action android:name="android.intent.action.MAIN" />  
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.           
  26.         #### 次Activity  
  27.         <activity android:name="SecondActivity"></activity>  
  28.           
  29.         #### 服务Service  
  30.         <service android:name=".MusicService">  
  31.           <intent-filter>  
  32.               <action android:name="com.test.Android.MUSIC" />  
  33.               <category android:name="android.intent.category.default" />  
  34.           </intent-filter>  
  35.         </service>  
  36.                   
  37.     </application>  
  38.   
  39. </manifest>  

资源相关文件:

layout 文件夹

activity_main.xml  内容:

[html] view plaincopyprint?
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.     <TextView  
  7.         android:id="@+id/textView1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_centerVertical="true"  
  12.         tools:context=".MainActivity" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/PlayMusic"  
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_above="@+id/textView1"  
  19.         android:layout_alignParentLeft="true"  
  20.         android:layout_marginBottom="52dp"  
  21.         android:layout_marginLeft="22dp"  
  22.         android:text="播放MP3音乐" />  
  23.   
  24.     <Button  
  25.         android:id="@+id/button1"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="wrap_content"  
  28.         android:layout_above="@+id/PlayMusic"  
  29.         android:layout_alignParentLeft="true"  
  30.         android:layout_marginBottom="67dp"  
  31.         android:layout_marginLeft="22dp"  
  32.         android:text="switch Second Activity" />  
  33.   
  34.     <Button  
  35.         android:id="@+id/StopMusic"  
  36.         android:layout_width="match_parent"  
  37.         android:layout_height="wrap_content"  
  38.         android:layout_alignBaseline="@+id/textView1"  
  39.         android:layout_alignBottom="@+id/textView1"  
  40.         android:layout_alignLeft="@+id/PlayMusic"  
  41.         android:text="停止播放MP3音乐" />  
  42.   
  43.     <Button  
  44.         android:id="@+id/CProvider"  
  45.         android:layout_width="match_parent"  
  46.         android:layout_height="wrap_content"  
  47.         android:layout_alignLeft="@+id/StopMusic"  
  48.         android:layout_below="@+id/textView1"  
  49.         android:layout_marginTop="94dp"  
  50.         android:text="Content Provider Test" />  
  51.   
  52. </RelativeLayout>  

activity_second.xml 内容:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.       
  7.   
  8.     <Button  
  9.         android:id="@+id/button1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginTop="45dp"  
  14.         android:text="switch Main Activity" />  
  15.   
  16. </RelativeLayout>  

新建了一个 res\raw 用于存放与应用一起打包的mp3文件:jiangnanstyle.mp3  江南Style,呵呵,最流行的音乐哟。

原文地址:

原创粉丝点击