当前位置:操作系统 > 安卓/Android >>

Android 笔记

  每一个android应用程序都在他自己的进程中运行,都有一个独立的Dalvik虚拟机实例,防止虚拟器崩溃时所有的chengx。Dalvik虚拟机是基于寄存器的,JVM是基于栈的。

应用解析:

1.Activity:用户所能看到的屏幕,用来处理应用的整体性工作。如监听系统事件、为用户显示指定的View、启动其他的activity等。

2.Intent:实现activity之间的切换。Intent描述应用想做什么,是应用程序的意图。Intent数据结构包括动作和动作所对应的数据。动作对应的数据用URI来进行表示。与之对应的是一个intentfilter,描述activity或intentreceiver能够操作那些intent,intentfilter要知道怎么去处理动作和URI,需要在AndroidManifest.xml中声明。调用startActivity(intent)实现activity的切换。重复利用intent,产生新的activity。

Intent showNextPage_intent = new Intent();
showNextPage_intent.setClass(this,NextPageActivity.class);
startActivity(showNextPage_intent);

3.IntentReceiver:对外部的事件进行响应,可以在AndroidManifest.xml和Context.registerReceiver()中注册。

4.Service:具有生命周期的一种服务。使用Context.startService()来启动一个service,并在后台运行该服务。Context.bindService()绑定到一个service上,通过service提供的接口与他进行通信,比如播放器的暂停,重播等操作。

生命周期:1>startService开始,stopService结束。

                2>与bindService进程同生共死,或者调用unbindService结束。

                3>混合使用需要stopservice和unbindService都结束,Service终止。

使用服务:Context.startService();Context.bindService()两种方式。


//音乐播放器的service实例//Manifest.xml中的Service定义
<service android:name=".Music">
 <intent-filter>
  <action android:name="com.liangshan.wuyong.START_AUDIO_SERVICE" />
  <category android:name="android.intent.category.default" />
 </intent-filter>
</service>
//Service子类中的Player
public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);
  player = MediaPlayer.create(this, R.raw.seven_days);
  player.start();
}
public void onDestroy() {
  super.onDestroy();
  player.stop();
}
//Activity 中定义的Intent开启相应的Service
startService(new Intent("com.gruden.sy.START_AUDIO_SERVICE"));
stopService(new Intent("com.gruden.sy.START_AUDIO_SERVICE"));

5.ContentProvider:数据共享,针对不同应用包或程序之间实现数据共享的解决方案。

6.Bundle:使用Bundle在activity之间传输数据,和activity及Intent联系较为密切。


//数据写入Intent
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("key_Name","NAME");
bundle.putString("Key_Salary",Salary);
intent.putExtras(bundle);
intent.setClass(this,NewActivity.class);
startActivity();

//从intnet中获取数据
Bundle bundle = this.getIntent().getExtras(); //get bundle
String name = bundle.getString("Key_Name");
String salary = bundle.getString("Key_Salaray");
setTitle(name+'-'+salary);

 另外一种在Activity间传递数据的方式intnet把服务请 求发送到目标Activity,在源请求Activity中冲在onActivity()方法等待Intent返回应答结果。

startActivity(intent,REQUEST_ASK);//开启intent时同时捎带上请求

//重载onActivityResult()
void onActivityResult(int requestCode,int resultCode,Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode == REQUEST_ASK){
       if(reulstCode == RESULT_CANCELed){
          setTitle("cancel...");
       }else if(ResultCode == RESULT_OK){
          showBundle = data.getExtras();
          Name = Bundle.getString("myName");
       }
    }
}

 目标Activity中发送结果代码,连同源请求的数据一起绑定到Bundle中通过Intent传回请求activity中去。


bundle.putString("Name",NAME);
intent.putExtras(bundle);
setResult(RESULT_OK,intent);//通过Intent返回结果
finish();

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,