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应用:
- package com.example.com.test.app;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.provider.ContactsContract.PhoneLookup;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.Intent;
- import android.database.Cursor;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private final static String TAG = "MainActivity";
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- /* 设置显示main.xml布局 */
- setContentView(R.layout.activity_main);
- /* findViewById(R.id.button1)取得布局main.xml中的button1 */
- Button button = (Button) findViewById(R.id.button1);
- /* 监听button的事件信息 */
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- /* 新建一个Intent对象 */
- Intent intent = new Intent();
- /* 指定intent要启动的类 */
- intent.setClass(MainActivity.this, SecondActivity.class);
- /* 启动一个新的Activity */
- startActivity(intent);
- /* 关闭当前的Activity */
- MainActivity.this.finish();
- }
- });
- //从main.xml布局中获得Button对象
- Button button_start = (Button)findViewById(R.id.PlayMusic);
- Button button_stop = (Button)findViewById(R.id.StopMusic);
- Button button_cp = (Button)findViewById(R.id.CProvider);
- //设置按钮(Button)监听
- button_start.setOnClickListener(startplay);
- button_stop.setOnClickListener(stopplay);
- button_cp.setOnClickListener(cplisten);
- }
- // 开始播放
- private OnClickListener startplay = new OnClickListener() {
- public void onClick(View v) {
- // 开启Service
- startService(new Intent("com.test.Android.MUSIC"));
- }
- };
- // 停止播放
- private OnClickListener stopplay = new OnClickListener() {
- public void onClick(View v) {
- // 停止Service
- stopService(new Intent("com.test.Android.MUSIC"));
- }
- };
- // 测试 Content Provider
- private OnClickListener cplisten = new OnClickListener() {
- public void onClick(View v) {
- String string = "";
- //得到ContentResolver对象
- ContentResolver cr = getContentResolver();
- //取得电话本中开始一项的光标
- Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
- // 向下移动一下光标
- while (cursor.moveToNext()) {
- // 取得联系人名字
- int nameFieldColumnIndex = cursor
- .getColumnIndex(PhoneLookup.DISPLAY_NAME);
- String contact = cursor.getString(nameFieldColumnIndex);
- // 取得电话号码
- int numberFieldColumnIndex = cursor
- .getColumnIndex(PhoneLookup.NUMBER);
- String number = null; //cursor.getString(numberFieldColumnIndex);
- string += (contact + ":" + number + "\n");
- Log.i(TAG,"telephone string: " + string );
- }
- cursor.close();
- //设置TextView显示的内容
- DisplayText(string);
- }
- };
- /* 显示Toast */
- private void DisplayText(String str) {
- Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
- }
- }
切换的Activity应用:
- package com.example.com.test.app;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class SecondActivity extends Activity{
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- /* 设置显示second.xml布局 */
- setContentView(R.layout.activity_second);
- /* findViewById(R.id.button1)取得布局main.xml中的button1 */
- Button button = (Button) findViewById(R.id.button1);
- /* 监听button的事件信息 */
- button.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v)
- {
- /* 新建一个Intent对象 */
- Intent intent = new Intent();
- /* 指定intent要启动的类 */
- intent.setClass(SecondActivity.this, MainActivity.class);
- /* 启动一个新的Activity */
- startActivity(intent);
- /* 关闭当前的Activity */
- SecondActivity.this.finish();
- }
- });
- }
- }
后台Service服务程序:
- package com.example.com.test.app;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- public class MusicService extends Service {
- // MediaPlayer对象
- private MediaPlayer player = null;
- public IBinder onBind(Intent arg0) {
- return null;
- }
- public void onStart(Intent intent, int startId) {
- super.onStart(intent, startId);
- // 这里可以理解为装载音乐文件
- player = MediaPlayer.create(this, R.raw.jiangnanstyle);
- // 循环播放
- player.setLooping(true);
- // 开始播放
- player.start();
- }
- public void onDestroy() {
- super.onDestroy();
- // 停止音乐-停止Service
- player.stop();
- }
- }
AndroidManifest.xml应用中全局描述文件:
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.com.test.app"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="10"
- android:targetSdkVersion="15" />
- ##### 可以访问的权限设定
- <uses-permission
- android:name="android.permission.READ_CONTACTS">
- </uses-permission>
- <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
- #### 主Activity
- <activity
- android:name=".MainActivity"
- android:label="@string/title_activity_main" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- #### 次Activity
- <activity android:name="SecondActivity"></activity>
- #### 服务Service
- <service android:name=".MusicService">
- <intent-filter>
- <action android:name="com.test.Android.MUSIC" />
- <category android:name="android.intent.category.default" />
- </intent-filter>
- </service>
- </application>
- </manifest>
资源相关文件:
layout 文件夹
activity_main.xml 内容:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="fill_parent">
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- tools:context=".MainActivity" />
- <Button
- android:id="@+id/PlayMusic"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_above="@+id/textView1"
- android:layout_alignParentLeft="true"
- android:layout_marginBottom="52dp"
- android:layout_marginLeft="22dp"
- android:text="播放MP3音乐" />
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_above="@+id/PlayMusic"
- android:layout_alignParentLeft="true"
- android:layout_marginBottom="67dp"
- android:layout_marginLeft="22dp"
- android:text="switch Second Activity" />
- <Button
- android:id="@+id/StopMusic"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/textView1"
- android:layout_alignBottom="@+id/textView1"
- android:layout_alignLeft="@+id/PlayMusic"
- android:text="停止播放MP3音乐" />
- <Button
- android:id="@+id/CProvider"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/StopMusic"
- android:layout_below="@+id/textView1"
- android:layout_marginTop="94dp"
- android:text="Content Provider Test" />
- </RelativeLayout>
activity_second.xml 内容:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal" >
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_marginTop="45dp"
- android:text="switch Main Activity" />
- </RelativeLayout>
新建了一个 res\raw 用于存放与应用一起打包的mp3文件:jiangnanstyle.mp3 江南Style,呵呵,最流行的音乐哟。
原文地址:
- android四大组件解析
- Android四大组件解析
- Android四大组件解析
- Android之四大组件解析
- android的四大组件解析
- Android四大组件深度解析
- 主题: Android之四大组件解析
- Android 四大组件之Service解析
- Android四大组件之Service解析
- Android四大组件之BroadcastReceiver详细解析
- Android四大组件:BroadcastReceiver史上最全面解析
- Android四大组件:Service史上最全面解析
- Android四大组件:BroadcastReceiver史上最全面解析
- Android四大组件:Service史上最全面解析
- android四大组件-service全面解析一
- Android四大组件之一---BroadcastReceiver全面解析
- Android四大组件之一 BroadcastReceiver解析
- Android四大基本组件——Service完全解析
- Sensor 代码流程分析
- Tomcat服务器的安装与配置
- HashMap和Hashtable的区别
- 输入输出加速外挂
- 【设计模式】5. 单例模式(以及多线程、无序写入、volatile对单例的影响)
- Android四大组件解析
- 本来不知道如何写博客 现在作为老鸟也写一篇给大家看看吧 使用委托将函数放在主线程调用
- 基本数据类型分析
- TortoiseSVN和Eclipse使用时需要注意的事项
- BMP位图格式详解<二>--转载
- poj1848 树状DP
- linux连接工具之Secure-CRT
- &和&&的区别
- 手把手教你ARC——ARC入门和使用