BroadcastReceiver实时短信提醒

添加权限

 

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

 

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsReceiver extends BroadcastReceiver {

	private NotificationManager myNotiManager;

	/* 声明静态字符串,并使用android.provider.Telephony.SMS_RECEIVED作为Action为短信的依据 */
	private static final String mACTION = "android.provider.Telephony.SMS_RECEIVED";

	public void onReceive(Context context, Intent intent) {

		myNotiManager = (NotificationManager)context.getSystemService("notification");

		/* 判断传来Intent是否为短信 */
		if (intent.getAction().equals(mACTION)) {
			String smsTitle = "" , smsContent = "";
			/* 接收由Intent传来的数据 */
			Bundle bundle = intent.getExtras();
			/* 判断Intent是有资料 */
			if (bundle != null) {
				/*
				 * pdus为 android内建短信参数 identifier 透过bundle.get("")并传一个包含pdus的对象
				 */
				Object[] myOBJpdus = (Object[]) bundle.get("pdus");
				/* 建构短信对象array,并依据收到的对象长度来建立array的大小 */
				SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
				for (int i = 0; i < myOBJpdus.length; i++) {
					messages[i] = SmsMessage
							.createFromPdu((byte[]) myOBJpdus[i]);
				}

				/* 将送来的短信合并自定义信息于StringBuilder当中 */
				for (SmsMessage currentMessage : messages) {
					/* 来讯者的电话号码 */
					smsTitle = smsTitle + currentMessage.getDisplayOriginatingAddress();
					/* 取得传来讯息的BODY */
					smsContent = smsContent + currentMessage.getDisplayMessageBody();
				}
			}
			
			setNotiType(context, R.drawable.doc, smsTitle , smsContent);

		}
	}

	public void setNotiType(Context context, int iconId, String title , String content) {
		/*
		 * 建立新的Intent,作为点选Notification留言条时, 会执行的Activity
		 */
		Intent notifyIntent = new Intent(context, Main.class);// 单击Notification转跳至MyNotification.class
		notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		/* 建立PendingIntent作为设定递延执行的Activity */
		PendingIntent appIntent = PendingIntent.getActivity(context, 0,
				notifyIntent, 0);

		/* 建立Notication,并设定相关参数 */
		Notification myNoti = new Notification();
		/* 设定statusbar显示的icon */
		myNoti.icon = iconId;
		/* 设定statusbar显示的文字讯息 */
		//myNoti.tickerText = content;
		/* 设定notification发生时同时发出预设声音 */
		myNoti.defaults = Notification.DEFAULT_SOUND;
		// 屏幕亮myNoti.defaults = Notification.DEFAULT_LIGHTS;
		// 震动myNoti.defaults = Notification.DEFAULT_VIBRATE;
		// 包括铃声、屏幕亮和震动myNoti.defaults = Notification.DEFAULT_ALL;
		/* 设定Notification留言条的参数 */
		myNoti.setLatestEventInfo(context, "来自 " + title + "的短信", "内容:" + content, appIntent);
		/* 送出Notification */
		myNotiManager.notify(0, myNoti);
	}

}

 manifest.xml

<receiver
			android:name=".SmsReceiver">
			<intent-filter>
				<action
					android:name="android.provider.Telephony.SMS_RECEIVED" />
			</intent-filter>
		</receiver>

 注意:这个receiver可以跟桌面工具的receiver共存,并且都可以拥有intent-filter


BroadcastReceiver实时短信提醒

你可能感兴趣的:(BroadcastReceiver实时短信提醒)