<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videoplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<!-- 短信接收权限 -->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- screenOrientation 屏幕方向-->
<!-- activity theme : 全屏-->
<activity
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 短信到来广播事件 priority 优先级 -->
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
<!-- 自定义广播事件 -->
<receiver android:name=".MyBroadcastReceiver">
<intent-filter >
<action android:name="MyBroadcast_1"/>
</intent-filter>
</receiver>
</application>
</manifest>
SmsReceiver.java
package com.example.videoplayer;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.widget.Toast;
/**
* 1.新建sms广播接收者 - 继承 系统广播接收者
* 2.清单文件添加短信到来广播事件 -接收短信系统权限
* 3.在用户全屏状态-短信到来会接收一个Toast
* @author Administrator
*
*/
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "您有新短消息", 0).show();
//通过 intent 操作接收的短信
//getExtras()返回 【Bundle类型 ==map集合】
//get("pdus"); pdus 电信短信格式pdu;byte[]数组
Object[] pdus = (Object[]) intent.getExtras().get("pdus");
for(Object pdu : pdus){
//每一条短信的数据
SmsMessage smsMsg = SmsMessage.createFromPdu((byte[]) pdu);
String body = smsMsg.getMessageBody();//短信内容
String sender = smsMsg.getOriginatingAddress();//短信发件人
Date date = new Date(smsMsg.getTimestampMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sendTime = sdf.format(date);
System.out.println("sendTime:"+sendTime);
System.out.println("body:"+body);
System.out.println("sender"+sender);
//如果·时10086短信 抛弃
if("10086".equals(sender)){
//优先级高的接受者终止广播-比他低的就接收不到广播了
abortBroadcast();
}
}
}
}
MyBroadcastReceiver.java
package com.example.videoplayer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
/**
* 自定义的广播接收者-主配置文件注册此广播
*/
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "A程序 MyBroadcastReceiver 接收到 Action:MyBroadcast_1", 1).show();
}
}
有序广播 - 无序广播
//发送无序-自定义广播事件-所有的广播接受者都会接收到此广播事件
sendBroadcast(intent);
//发送有序-自定义广播事件;广播接收按照优先级接收广播-并可以截断此广播
sendOrderedBroadcast(intent, receiverPermission);
车载仪
3G模块 - A应用 -注册广播接受者 -接收自定义的广播
故障检测- B应用 -自定义广播事件
事故报警-C应用
B应用主要代码:
// send but click event
public void sendBroadcastBut(View v){
Intent intent = new Intent();
intent.setAction("MyBroadcast_1");
//发送无序-自定义广播事件
sendBroadcast(intent);
//发送无序-自定义广播事件-参数2是指定接收者的接收权限
sendOrderedBroadcast(intent, null);
}