Activity. Intent. Service. BroadcastReceiver的使用
来源:互联网 发布:网络巫师中文字幕6 编辑:程序博客网 时间:2024/05/13 14:11
1.Activity篇
1.Activity的主要作用
Activity是界面、用户接口、控件窗口,负责程序与用户间进行交互
2.创建一个Activity需要的步骤
1.一个Activity就是一个类,并且这个类要继承Activity
2.需要复写(@override)onCreate方法,第一次运行就会运行此方法
3.每一个Activity都需要在AndroidMainfest.xml文件中配置
4.为Activity添加必要的控件(layout.xml)
5.设置Activity使用的Layout文件(setContentView(R.layout.layoutID))
3.获取Layout文件中配置的控件
setContentView(R.layout.layoutID) //设置内容对应的而已文件ID
TextView myText = (TextView)findViewById(R.id.myTextView); //获取一个TextView
myText.setText("First TextView"); //设置TextView显示文本
//布局示例
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- </LinearLayout>
//类示例
- public class HelloActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.hello);
- }
- }
2.Intent篇
1.Intent的主要作用
负责从一个Actvity传递数据到另一个Activity或其它对象
传递的对象不一定要是程序自身的Activity,可以是系统的,或其它程序的,或服务等
2.一个Intent对象包含的一组信息
Component name:传递到哪个对象
Action:传递的动作(Intent.ACTION_??)
Data:传递的URI
Extras:传递参数(多种数据类型的键值对)
3.调用跳转另一个Activity
使用startActivity(Intent)方法
4.添加监听事件的内部类
class btnGotoActivity02Listener implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(Activity01Activity.this, Activity02Activity.class); //从哪个对象传递到哪个对象
intent.putExtra("text_content", "hello "); //传键值对
startActivity(intent); //跳转
}
}
5.为按钮添加监听事件
btnGotoActivity02 = (Button) findViewById(R.id.btnGotoActivity02);
btnGotoActivity02.setOnClickListener(new btnGotoActivity02Listener());
6.使用Intent打开短信发送界面
class btnSendSmsListener implements OnClickListener {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13800138000")); //SENDTO调用短信界面
intent.putExtra("sms_body", "I miss you ……"); //短信内容
startActivity(intent);
}
}
7.另一个Activity中接收Intent传递过来的键值对
Intent intent = getIntent();
txtActivity02 = (TextView) findViewById(R.id.txtActivity02);
txtActivity02.setText(intent.getStringExtra("text_content"));
//完整布局示例
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/btnGotoActivity02"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/btnGotoActivity02" />
- <Button
- android:id="@+id/btnSendSms"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/btnSendSms" />
- </LinearLayout>
//完整类示例
- public class Activity01Activity extends Activity {
- /** Called when the activity is first created. */
- Button btnGotoActivity02 = null;
- Button btnSendSms = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity01);
- btnGotoActivity02 = (Button) findViewById(R.id.btnGotoActivity02);
- btnGotoActivity02.setOnClickListener(new btnGotoActivity02Listener());
- btnSendSms = (Button) findViewById(R.id.btnSendSms);
- btnSendSms.setOnClickListener(new btnSendSmsListener());
- }
- // 内部类:btnGotoActivity02点击事件监听
- class btnGotoActivity02Listener implements OnClickListener {
- public void onClick(View v) {
- Intent intent = new Intent();
- intent.setClass(Activity01Activity.this, Activity02Activity.class);
- intent.putExtra("text_content", "hello");
- startActivity(intent);
- }
- }
- // 内部类:btnSendSms点击事件监听
- class btnSendSmsListener implements OnClickListener {
- public void onClick(View v) {
- Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13800138000"));
- intent.putExtra("sms_body", "I miss you ……");
- startActivity(intent);
- }
- }
- }
3.Activity生命周期
1.Activity生命周期七大函数
//当Activity第一次被创建时调用,用于设置布局文件,绑定监听器
protected void OnCreate(){}
//当Activity看到后调用
protected void OnStart(){}
//当Activity能够获取用户焦点时调用
protected void OnResume(){}
//当应用程序启动了另外一个Activity时调用,用于保存当前页面的数据
protected void OnPause(){}
//当Activity处于不可见的情况下调用,如果是对话框则不会调用
protected void OnStop(){}
//当回退键回到原来的Activity时调用,此时不需要OnCreate
protected void OnRestart(){}
//当Activity被销毁时调用(如:调用Finish方法、资源回收、后退键)
protected void OnDestory(){}
2.调用顺序(两个Activity,分别是1.2.)
启动程序:1.OnCreate -> 1.OnStart -> 1.OnResume
事件切换:1.OnPause -> 2.OnCreate -> 2.OnStart -> 2.OnResume -> 1.OnStop
点击后退:2.OnPause -> 1.OnRestart -> 1.OnStart -> 1.OnResume -> 2.OnStop -> 2.OnDestory
3.Task
用于存放Activity的一个栈(Stack),先入后出,回退也是这原理
一个应用程序有一个Task,手机端显示的永远是栈里最顶部的Activity
回退后,被弹出的Activity将被销毁
4.窗口方式的Activity(对话框)
在AndroidManifest.xml中,在Activity节点中增加属性 android:theme="@android:style/Theme.Dialog"
5.资源回收
系统会在资源不足时,对Activity资源进行回收
被回收的Activity是处于以下状态:OnPause OnStop OnDestory
4.Service篇
1.Service是什么
Service是一个应用程序组件
Service没有图形化界面
Service用于处理一些耗时较长的动作
可以使用Service更新ContentProvider,发送Intent以及启动系统的通知等等
2.Service不是什么
Service不是一个单独的进程
Service不是一个线程
3.Service生命周期
Service首次运行时,会执行onCreate方法
再次运行时,由于Service在运行,所以不会执行onCreate,而是直接执行onStartCommand
4.启动和停止Service的方法
1.新建一个类,并继承于Service
- public class MyService extends Service{
- @Override
- public IBinder onBind(Intent intent){
- }
- @Override
- public void onCreate(){
- System.out.println("Service Start...");
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId){
- System.out.println("flags:" + flags + "; startId:" + startId);
- return START_NOTE_STICKY;
- }
- @Override
- public void onDestory(){
- System.out.println("Service Stop...");
- }
- }
2.在AndroidManifest.xml文件中注册服务
<service android:name=".MyService"></service>
3.编写代码启动、关闭服务
- public void startService(){
- Intent intent = new Intent();
- intent.setClass(ServiceActivity.this, MyService.class);
- startService(intent);
- }
- public void stopService(){
- Intent intent = new Intent();
- intent.setClass(ServiceActivity.this, MyService.class);
- }
5.Broadcast
1.Android的广播机制介绍
当Android系统接收到一个事件后(如电话响、收到短信),触发事件并能手广播
2.BroadcastReceiver的作用
对有注册广播接收的程序进行广播,通知其它感兴趣的程序
3.BroadcastReceiver的编写方法
1.在AndroidManifest.xml中注册事件:
- <receiver android:name=".TestReceiver">
- <intent-filter>
- <action android:name="android.intent.action.EDIT" />
- </intent-filter>
- </receiver>
2.创建一个类,继承于BroadcastReceiver,用于接收广播
- public class BCReceiver extends BroadcastReceiver{
- public void BCReceiver(){
- }
- @Override
- public void onReceive(Context context, Intent intent){
- System.out.println("receive a broadcast");
- }
3.编写一个类,用于发送广播(测试用)
Intent intent = new Intent();
intent.setAction(Intent.ACTION_EDIT);
CPActivity.this.sendBoardcase(intent);
4.BroadcastReceiver的生命周期
在Android系统接收到一个广播时,会查找符合的广播程序
如果符合intent-filter的话,则会创建一个广播接收类
广播接收类在触发onReceive方法后,就会被销毁,下次重新创建
5.注册BroadcastReceiver的方法
BroadcastReceiver用于监听被广播的事件,为了达到这个目的,BroadcastReceiver必须进行注册,注册的方法:
1.在应用程序的代码当中进行注册
2.在AndroidManifest.xml当中进行注册(上节已介绍)
两者区别:注册在xml文件中的话,即使程序被关闭了,也会接收到广播
代码绑定广、解除绑定播接收方法:
//代码绑定广播接收方法:
SmsReceiver smsReceiver = new SmsReceiver(); //创建监听类
IntentFilter filter = new IntentFilter(); //创建过滤器
filter.addAction("android.provider.Telephony.SMS_RECEIVED"); //向过滤器添加动作
BCActivity.this.registerReceiver(smsReceiver,filter); //注册广播
//代码解除绑定广播接收方法:
BCActivity.this.unregisiterReceiver(smsReceiver);
在模拟器中发送短消息:
DDMS - Emulator Control - SMS中,填写号码和内容,点击“Send”按钮
接收广播后的相关处理:
- public void onReceive(Context context, Intent intent){
- Bundle bundle = intent.getExtras(); //接收Intent对象中的数据
- Object[] obj = (Object[])bundle.get("pdus"); //
- SmsMessage[] messages = new SmsMessage[obj.length]; //创建短消息类型数组
- for(int i=0; i<obj.length; i++){
- messages[i] = SmsMessage.createFromPdu((byte[]obj[i]); //获取一条短信
- System.out.println(messages[i].getDisplayMessageBody()); //打印消息体
- }
- }
6.Android内置的BroadcastReceiver
在帮助文档中的Intent类页面中,找到常量中有“Broadcast Action”的相关ACTION
ACTION_CAMERA_BUTTON //照相按钮被点击
ACTION_BATTERY_LOW //电池低电
ACTION_DATE_CHANGED //
ACTION_POWER_CONNECTIED //连接USB充电
ACTION_REBO0OT //重启系统
ACTION_SCREEN_ON //屏幕被打开
- Activity. Intent. Service. BroadcastReceiver的使用
- 安卓的intent,activity,service,broadcast,broadcastreceiver
- 基于Service的播放器,Activity,Service,BroadcastReceiver综合使用
- Activity. Intent. Service. Broadcast的使用
- Android中context、activity、service、broadcastReceiver的使用
- Android 编程五要诀:Activity、Service、BroadcastReceiver、ContentProvider、Intent
- Android编程五要诀:Activity、Service、BroadcastReceiver、ContentProvider、Intent
- BroadcastReceiver实现Intent的Activity跳转
- 【代码】Android -- Activity 、Service、BroadcastReceiver 综合使用
- Activity和Service通信 使用BroadcastReceiver
- Android Service、Activity、BroadcastReceiver简单使用
- Activity、Service、BroadCastReceiver之间的通信
- Service&BroadcastReceiver&Activity
- Activity,Service,BroadcastReceiver生命周期
- 使用BroadcastReceiver实现开机启动Service或Activity
- 使用BroadcastReceiver实现开机启动Service或Activity
- 使用BroadcastReceiver实现开机启动Service或Activity
- 使用BroadcastReceiver实现开机启动Service或Activity
- appfuse - hibernate search 2 - 常用注解
- Win8快捷键
- 工业水处理:威立雅化工污水处理工艺流程与技术特点
- Windows C下利用wininet发送http请求
- 拷贝构造函数,构造函数
- Activity. Intent. Service. BroadcastReceiver的使用
- linux串口收发
- CPU-上下文切换,运行队列和使用率
- You must install 'makeinfo' on your build machine
- python中read() readline()以及readlines()区别 .
- MyEclipse中导入jdk源码
- ARM Trusted Firmware User Guide
- 工业水处理:威立雅化工废水处理技术的特点介绍
- *(leetcode_string) Add Binary