您的位置:首页 > 移动开发 > Android开发

Android项目之广播(BroacastReceiver)与服务(Service)

2016-11-20 14:06 573 查看
前言:

在我写博客前再声明一下,我希望经过我同意装载我文章的某某某记得注明:(本文来自http://blog.csdn.net/qq_29269233),要尊重我的劳动成果,这样才能给我更多的支持和鼓励!今天我就接着上一篇博客:《Android项目之webview简单使用》,在上一篇博客的基础上添加自动启动并打开应用的功能!应用自启是利用了Broacastreceiver和service。

一、简单介绍:

BroadcastReceiver是一种消息型组件,用于在不同的组件乃至不同的应用之间传递消息,无法被用户直接感知,因为它工作在系统内部。BroadcastReceiver也叫广播,广播

的注册有两种方式:静态注册和动态注册。

Service是一种计算型组件,用于在后台执行一系列计算任务。由于Service组件工作在后台,因此也无法让用户直接感知到它的存在。Service分为两种工作状态,一种是启动

状态,主要用于执行后台计算;另一种是绑定状态,主要用于其他组件和service的交互。

(一)、BroadcastReceiver


清单文件中配置:

<receiver android:name="包名.广播接收者文件" >
<intent-filter android:priority="广播拦截的优先级(最大:2147483647)" >
<action android:name="广播监听的动作 可以是自定义的或者系统广播" />
</intent-filter>
</receiver>
1
2
3
4
5
1
2
3
4
5


动态注册广播接收者


1.动态创建一个广播接收者

class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

//收到广播后
//1.可以触发某些事件
//2.从initen参数中获取广播发送的数据
}
};
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
2.对广播接收者进行动态注册

Receiver = new MyReceiver();
//设置filter信息
IntentFilter filter = new IntentFilter();
filter.addAction("自定义的或者系统广播");   //设置广播监听的动作
//注册
registerReceiver(Receiver, filter);
1
2
3
4
5
6
1
2
3
4
5
6
3.取消注册

unregisterReceiver(mReceiver);
mReceiver = null;
1
2
1
2


自定义广播发送(通过intent携带数据)

Intent intent = new Intent();
intent.putExtra("键", 要携带到接收者的信息);
intent.setAction("自定义广播");            //设置广播监听的动作
sendBroadcast(intent);
1
2
3
4
1
2
3
4


两种广播的区别

静态注册广播接收者在程序的整个运行期间都会监听。

动态注册的广播接收者可以控制在需要的时候开启监听,不需要的时候关闭监听。通常可以将动态注册广播接收者放到一个服务中,服务开启时注册广播,服务关闭时取消注册。


.例子


常见系统广播

//监听系统启动广播

<receiver android:name=".receiver.BootCompleteReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
1
2
3
4
5
1
2
3
4
5
//监听短信

<receiver android:name=".receiver.SmsReceiver" >
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
1
2
3
4
5
1
2
3
4
5
//获取管理员权限  MyAdminReceiver需要实现但是不用写内容

<receiver
android:name=".receiver.MyAdminReceiver"
android:description="@string/sample_device_admin_description"
android:label="@string/sample_device_admin"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/device_admin_sample" />

<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
1
2
3
4
5
6
7
8
9
10
11
12
13

1
2
3
4
5
6
7
8
9
10
11
12
13
//监听创建快捷方式

<receiver android:name=".receiver.MyWidget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/process_widget_provider" />
</receiver>
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
//监听杀死所有进程

<receiver android:name=".receiver.KillProcess">
<intent-filter>
<action android:name="com.example.mobilesafe.KILLALLPROCESS" />
</intent-filter>
</receiver>
1
2
3
4
5
1
2
3
4
5


动态注册广播接收者(一般在服务中动态注册 重要)


适用情况: 广播接收者监听事件变化,有变化后就返回给服务,服务通过返回Bind或者写SharePrecefence等方法传出数据。


具体运用举例:


1.开启电话监听,监听来电去电 ,例如 黑名单,归属地查询等


2.程序加锁 ,监听当前栈顶程序是否被加锁


3.实时定位 ,不断读取当前定位信息


4.锁屏事件 ,监听到锁屏后可以做一些清理工作


5.和桌面上的Widget通信

(二)、Service:


注册一个服务

清单文件
<service android:name=".service.MyService" />

public class MyService extends Service {

@Override
public IBinder onBind(Intent intent) {

return null;
}

//创建服务的时候调用
@Override
public void onCreate() {
System.out.println("*****onCreate******");
super.onCreate();
}

//启动server    每次start都会调用一次
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("*****onStartCommand******");
return super.onStartCommand(intent, flags, startId);
}

//手动停止程序后会终止服务,会调用onDestroy()方法
@Override
public void onDestroy() {
System.out.println("*****onDestroy******");
super.onDestroy();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32


开启一个服务

1.启动
//使用Intent
Intent intent = new Intent(当前Activity.this, MyService.class);
startService(intent);
stopService(intent);

2.使用bind让其他组件获取服务提供的数据
//使用Intent
Intent intent = new Intent(当前Activity.this, MyService.class);
bindService(intent, conn, BIND_AUTO_CREATE);
unbindService(conn);
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11


注意事项

bindService绑定服务、unBindService解除绑定的服务;
服务是在被绑定的时候被创建,调用oncreate、onbind方法;
服务只能被绑定一次;
服务只能被解除一次,接触绑定的时候调用onUnbind、onDestrory方法,如果多次解除绑定会抛出异常;

推荐的方式(启用顺序):

1.startService:开启并创建一个服务,服务长期运行在后台;
2.bindService:绑定服务,可以调用服务里面的方法;
3.unBindService:解除服务,停止服务里面的方法;
4.stopService:停止服务,销毁服务对象;


二、说到这里应该都算了解BroacastReceiver和Service了,现在我就接着上一篇博客:《Android项目之webview简单使用》,在上一篇博客的基础上添加自动启动并打开应用的功能!

1、配置AndroidManifest

<!--服务-->
<service android:name=".BootService" >
</service>

<!--广播-->
<receiver android:name=".BootBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>


添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

2、BootBroadcastReceiver”广播监听类“BootBroadcastReceiver”

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
* Created by Administrator on 2016/11/20 0020.
*/

public class BootBroadcastReceiver extends BroadcastReceiver {

// 系统启动完成
static final String ACTION = "android.intent.action.BOOT_COMPLETED";

@Override
public void onReceive(Context context, Intent intent) {

// 当收听到的事件是“BOOT_COMPLETED”时,就创建并启动相应的Activity和Service
if (intent.getAction().equals(ACTION)) {
// 开机启动的Activity
Intent activityIntent = new Intent(context, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// 启动Activity
context.startActivity(activityIntent);

// 开机启动的Service
Intent serviceIntent = new Intent(context, BootService.class);
// 启动Service
context.startService(serviceIntent);

}

}

}
3、开启Service设置

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;

/**
* Created by Administrator on 2016/11/20 0020.
*/

public class BootService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

// Service被启动时,将会有弹出消息提示[c]
Toast.makeText(this, "[开启我的服务]", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent, flags, startId);

}
}


开机自启的效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐