android 学习随笔十八(广播与服务 )

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 学习随笔十八(广播与服务 )相关的知识,希望对你有一定的参考价值。

1、广播接收者注册

  • 清单文件注册(Android四大组件都要在清单文件中注册

 一旦应用部署,广播接收者就生效了,直到用户手动停止应用或者应用被删除

  • 广播接收者可以使用代码注册

 需要广播接收者运行时,使用代码注册,不需要时,可以使用代码解除注册
 特殊广播接收者,必须代码注册。电量改变、屏幕开关,必须使用代码注册

2、注册广播接收者

//创建广播接收者对象
receiver = new ScreenOnOffReceiver();
//通过IntentFilter对象指定广播接收者接收什么类型的广播
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_SCREEN_ON);

//注册广播接收者
registerReceiver(receiver, filter);
* 解除注册广播接收者

unregisterReceiver(receiver);
* 解除注册之后,广播接收者将失去作用

技术分享
 1 package com.itheima.register;
 2 
 3 import android.content.BroadcastReceiver;
 4 import android.content.Context;
 5 import android.content.Intent;
 6 
 7 public class ScreenOnOFFReceiver extends BroadcastReceiver {
 8 
 9     @Override
10     public void onReceive(Context context, Intent intent) {
11         
12         String action = intent.getAction();
13         if(Intent.ACTION_SCREEN_ON.equals(action)){
14             System.out.println("屏幕开啦啦");
15         }
16         else if(Intent.ACTION_SCREEN_OFF.equals(action)){
17             System.out.println("屏幕关哇哇");
18         }
19 
20     }
21 
22 }
View Code
技术分享
 1 package com.itheima.register;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.content.IntentFilter;
 6 import android.os.IBinder;
 7 
 8 public class RegisterService extends Service {
 9 
10     private ScreenOnOFFReceiver receiver;
11 
12     @Override
13     public IBinder onBind(Intent intent) {
14         // TODO Auto-generated method stub
15         return null;
16     }
17 
18     @Override
19     public void onCreate() {
20         super.onCreate();
21         //1.创建广播接收者对象
22         receiver = new ScreenOnOFFReceiver();
23         //2.创建意图过滤器对象
24         IntentFilter filter = new IntentFilter();
25         //指定接收什么广播
26         filter.addAction(Intent.ACTION_SCREEN_OFF);
27         filter.addAction(Intent.ACTION_SCREEN_ON);
28         //3.注册广播接收者
29         registerReceiver(receiver, filter);
30         
31     }
32     
33     @Override
34     public void onDestroy() {
35         super.onDestroy();
36         //反注册广播接收者
37         unregisterReceiver(receiver);
38     }
39     
40 }
View Code
技术分享
 1 package com.itheima.register;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.view.Menu;
 7 import android.view.View;
 8 
 9 public class MainActivity extends Activity {
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15     }
16 
17 
18     public void start(View v){
19         Intent intent = new Intent(this, RegisterService.class);
20         startService(intent);
21     }
22     public void stop(View v){
23         Intent intent = new Intent(this, RegisterService.class);
24         stopService(intent);
25     }
26     
27 }
View Code

 服务注册广播

3、服务的分类

  • 本地服务:服务和启动它的组件在同一个进程
  • 远程服务:服务和启动它的组件不在同一个进程
  • 远程服务只能隐式启动,类似隐式启动Activity,在清单文件中配置Service标签时,必须配置intent-filter子节点,并指定action子节点

远程服务

技术分享
 1 package com.itheima.remoteservice;
 2 
 3 import com.itheima.remoteservice.PublicBusiness.Stub;
 4 
 5 import android.app.Service;
 6 import android.content.Intent;
 7 import android.os.Binder;
 8 import android.os.IBinder;
 9 
10 public class RemoteService extends Service{
11 
12     @Override
13     public IBinder onBind(Intent intent) {
14         System.out.println("绑定");
15         return new FengMiShu();
16     }
17 
18     class FengMiShu extends Stub{
19 
20         @Override
21         public void qianXian() {
22             // TODO Auto-generated method stub
23             remoteBanZheng();
24         }
25         
26     }
27     
28     @Override
29     public boolean onUnbind(Intent intent) {
30         System.out.println("解绑");
31         return super.onUnbind(intent);
32     }
33     
34     @Override
35     public void onCreate() {
36         System.out.println("创建");
37         super.onCreate();
38     }
39     
40     @Override
41     public int onStartCommand(Intent intent, int flags, int startId) {
42         System.out.println("开始");
43         return super.onStartCommand(intent, flags, startId);
44     }
45     
46     @Override
47     public void onDestroy() {
48         System.out.println("摧毁");
49         super.onDestroy();
50     }
51     
52     public void remoteBanZheng(){
53         System.out.println("苗领导在国外远程办证");
54     }
55 
56 }
View Code
技术分享
 1 package com.itheima.remoteservice;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 
 7 public class MainActivity extends Activity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13     }
14 
15 
16     @Override
17     public boolean onCreateOptionsMenu(Menu menu) {
18         // Inflate the menu; this adds items to the action bar if it is present.
19         getMenuInflater().inflate(R.menu.main, menu);
20         return true;
21     }
22     
23 }
View Code
技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.itheima.remoteservice"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.itheima.remoteservice.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <service android:name="com.itheima.remoteservice.RemoteService">
26             <intent-filter >
27                 <action android:name="com.itheima.remote"/>
28             </intent-filter>
29         </service>
30     </application>
31 
32 </manifest>
View Code

调用远程服务操作

package com.itheima.runremoteservice;

public interface PublicBusiness {

    void qianXian();
}
技术分享
 1 package com.itheima.runremoteservice;
 2 
 3 import android.os.Bundle;
 4 import android.os.IBinder;
 5 import android.app.Activity;
 6 import android.content.ComponentName;
 7 import android.content.Intent;
 8 import android.content.ServiceConnection;
 9 import android.view.Menu;
10 import android.view.View;
11 
12 public class MainActivity extends Activity {
13 
14     public PublicBusiness pb;
15     private MyConn conn;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         
22         conn = new MyConn();
23     }
24 
25 
26     public void start(View v){
27         Intent intent = new Intent();
28         intent.setAction("com.itheima.remote");
29         startService(intent);
30     }
31     public void stop(View v){
32         Intent intent = new Intent();
33         intent.setAction("com.itheima.remote");
34         stopService(intent);
35     }
36     public void bind(View v){
37         Intent intent = new Intent();
38         intent.setAction("com.itheima.remote");
39         bindService(intent, conn, BIND_AUTO_CREATE);
40     }
41     
42     public void unbind(View v){
43         unbindService(conn);
44     }
45     class MyConn implements ServiceConnection{
46 
47         @Override
48         public void onServiceConnected(ComponentName name, IBinder service) {
49             pb = (PublicBusiness) service;
50             
51         }
52 
53         @Override
54         public void onServiceDisconnected(ComponentName name) {
55             // TODO Auto-generated method stub
56             
57         }
58         
59     }
60     
61     public void banZheng(View v){
62         pb.qianXian();
63     }
64 }
View Code

 

4、服务的分类

 

以上是关于android 学习随笔十八(广播与服务 )的主要内容,如果未能解决你的问题,请参考以下文章

Android强化:服务与通信

简单的学习心得:网易云课堂Android开发第四章服务广播与酷特性

Android广播接收器与服务[重复]

[android] 代码注册广播接收者&利用广播调用服务的方法

VS/Xamarin Android开发Follow Me

Android广播(BroacastReceiver)与服务(Service)