Activity. Intent. Service. BroadcastReceiver的使用

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显示文本


//布局示例

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.         android:layout_width="fill_parent"    
  4.         android:layout_height="fill_parent"    
  5.         android:orientation="vertical" >    
  6.         
  7.         <TextView    
  8.             android:layout_width="fill_parent"    
  9.             android:layout_height="wrap_content"    
  10.             android:text="@string/hello" />    
  11.     </LinearLayout>    


//类示例

[java] view plaincopy
  1. public class HelloActivity extends Activity {    
  2.         /** Called when the activity is first created. */    
  3.         @Override    
  4.         public void onCreate(Bundle savedInstanceState) {    
  5.             super.onCreate(savedInstanceState);    
  6.             setContentView(R.layout.hello);    
  7.         }    
  8.     }    


 

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"));


//完整布局示例

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>    
  2.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.         android:layout_width="fill_parent"    
  4.         android:layout_height="fill_parent"    
  5.         android:orientation="vertical" >    
  6.         
  7.         <Button    
  8.             android:id="@+id/btnGotoActivity02"    
  9.             android:layout_width="fill_parent"    
  10.             android:layout_height="wrap_content"    
  11.             android:text="@string/btnGotoActivity02" />    
  12.         
  13.         <Button    
  14.             android:id="@+id/btnSendSms"    
  15.             android:layout_width="fill_parent"    
  16.             android:layout_height="wrap_content"    
  17.             android:text="@string/btnSendSms" />    
  18.             
  19.     </LinearLayout>    


//完整类示例

[java] view plaincopy
  1. public class Activity01Activity extends Activity {    
  2.         /** Called when the activity is first created. */    
  3.         Button btnGotoActivity02 = null;    
  4.         Button btnSendSms = null;    
  5.         
  6.         @Override    
  7.         public void onCreate(Bundle savedInstanceState) {    
  8.             super.onCreate(savedInstanceState);    
  9.             setContentView(R.layout.activity01);    
  10.         
  11.             btnGotoActivity02 = (Button) findViewById(R.id.btnGotoActivity02);    
  12.             btnGotoActivity02.setOnClickListener(new btnGotoActivity02Listener());    
  13.                 
  14.             btnSendSms = (Button) findViewById(R.id.btnSendSms);    
  15.             btnSendSms.setOnClickListener(new btnSendSmsListener());    
  16.         }    
  17.         
  18.         // 内部类:btnGotoActivity02点击事件监听     
  19.         class btnGotoActivity02Listener implements OnClickListener {    
  20.         
  21.             public void onClick(View v) {    
  22.                 Intent intent = new Intent();    
  23.                 intent.setClass(Activity01Activity.this, Activity02Activity.class);    
  24.                 intent.putExtra("text_content""hello");    
  25.                 startActivity(intent);    
  26.             }    
  27.         
  28.         }    
  29.         
  30.         // 内部类:btnSendSms点击事件监听     
  31.         class btnSendSmsListener implements OnClickListener {    
  32.         
  33.             public void onClick(View v) {    
  34.                 Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:13800138000"));    
  35.                 intent.putExtra("sms_body""I miss you ……");    
  36.                 startActivity(intent);    
  37.             }    
  38.         
  39.         }    
  40.     }    


 

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

[java] view plaincopy
  1. public class MyService extends Service{    
  2.         @Override    
  3.         public IBinder onBind(Intent intent){    
  4.         }    
  5.             
  6.         @Override    
  7.         public void onCreate(){    
  8.             System.out.println("Service Start...");    
  9.         }    
  10.             
  11.         @Override    
  12.         public int onStartCommand(Intent intent, int flags, int startId){    
  13.             System.out.println("flags:" + flags + "; startId:" + startId);    
  14.             return START_NOTE_STICKY;    
  15.         }    
  16.             
  17.         @Override    
  18.         public void onDestory(){    
  19.             System.out.println("Service Stop...");    
  20.         }    
  21.     }    


2.在AndroidManifest.xml文件中注册服务
  <service android:name=".MyService"></service>
  
 3.编写代码启动、关闭服务

[java] view plaincopy
  1. public void startService(){    
  2.         Intent intent = new Intent();    
  3.         intent.setClass(ServiceActivity.this, MyService.class);    
  4.         startService(intent);    
  5.     }    
  6.     public void stopService(){    
  7.         Intent intent = new Intent();    
  8.         intent.setClass(ServiceActivity.this, MyService.class);    
  9.     }    


5.Broadcast


1.Android的广播机制介绍
 当Android系统接收到一个事件后(如电话响、收到短信),触发事件并能手广播
 
2.BroadcastReceiver的作用
 对有注册广播接收的程序进行广播,通知其它感兴趣的程序
 
3.BroadcastReceiver的编写方法
 1.在AndroidManifest.xml中注册事件:

[html] view plaincopy
  1. <receiver android:name=".TestReceiver">    
  2.         <intent-filter>    
  3.             <action android:name="android.intent.action.EDIT" />    
  4.         </intent-filter>    
  5.     </receiver>   


 2.创建一个类,继承于BroadcastReceiver,用于接收广播

[java] view plaincopy
  1. public class BCReceiver extends BroadcastReceiver{    
  2.         public void BCReceiver(){    
  3.         }    
  4.         @Override    
  5.         public void onReceive(Context context, Intent intent){    
  6.             System.out.println("receive a broadcast");    
  7.     }    


 

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”按钮
 接收广播后的相关处理:

[java] view plaincopy
  1. public void onReceive(Context context, Intent intent){    
  2.         Bundle bundle = intent.getExtras(); //接收Intent对象中的数据     
  3.         Object[] obj = (Object[])bundle.get("pdus"); //     
  4.         SmsMessage[] messages = new SmsMessage[obj.length]; //创建短消息类型数组     
  5.         for(int i=0; i<obj.length; i++){    
  6.             messages[i] = SmsMessage.createFromPdu((byte[]obj[i]); //获取一条短信     
  7.             System.out.println(messages[i].getDisplayMessageBody()); //打印消息体     
  8.         }    
  9.     }   


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 //屏幕被打开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值