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

安卓使用BroadcastReceiver监听接受短信信息

2017-02-24 12:59 363 查看

效果



代码

1.SmsReceiverTest

package com.javen.devicemange.CrazyOne.broadcast;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.javen.devicemange.R;

/**
* Created by Administrator on 2017/2/24 0024.
* 使用BroadcastReceiver监听接受短信信息
*/

public class SmsReceiverTest extends AppCompatActivity implements View.OnClickListener {
private Button listener;
private static SmsReceiverTest smsReceiverTest;
private TextView messageAddress;
private TextView message_address;
private TextView messageBody;
private TextView message_body;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.smsreceivertest);
initView();
//在activity中创建一个静态类成员变量 instance,在onCreate方法的最后,将this赋值给instance
smsReceiverTest = this;

}
//获取本类对象
public static SmsReceiverTest getSingleInstance() {
return smsReceiverTest;
}

private void initView() {
listener = (Button) findViewById(R.id.listener);

listener.setOnClickListener(this);
messageAddress = (TextView) findViewById(R.id.messageAddress);
messageAddress.setOnClickListener(this);
message_address = (TextView) findViewById(R.id.message_address);
message_address.setOnClickListener(this);
messageBody = (TextView) findViewById(R.id.messageBody);
messageBody.setOnClickListener(this);
message_body = (TextView) findViewById(R.id.message_body);
message_body.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.listener:
break;
}
}

}


2.SmsReceiver

package com.javen.devicemange.CrazyOne.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.TextView;

import com.javen.devicemange.R;
/**
* Created by Administrator on 2017/2/24 0024.
* 使用BroadcastReceiver监听接受短信信息
*  <--授予程序接受短信的权限-->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
*/
public class SmsReceiver extends BroadcastReceiver { private TextView message_address; private TextView message_body; String originatingAddress; private StringBuilder stringBuilder; @Override public void onReceive(Context context, Intent intent) { //进行信息匹配,这个if判断语句可以不写 if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) { //中止广播,不让系统短信息接受到广播 //abortBroadcast(); Log.d("GsonUtils", "MessageBroadcastReceiver_onReceive"); stringBuilder = new StringBuilder(); //接受短信sms传递过来的数据 Bundle bundle = intent.getExtras(); //判断是否有数据 if (null != bundle) { //通过pdus可以获得接受到所有的短信消息 Object[] pdus = (Object[]) bundle.get("pdus"); //构建短信对象array,并依据收到的对象长度来创建数组的长度大小 SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < messages.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } //获取接收短信的电话号码 originatingAddress = messages[0].getOriginatingAddress(); Log.d("GsonUtils", "originatingAddress=" + originatingAddress); for (int i = 0; i < messages.length; i++) { SmsMessage message = messages[i]; //获取短信的内容 String messageBody = message.getMessageBody(); //将短信的内容拼接起来 stringBuilder.append(messageBody); } } Log.d("GsonUtils", "MessageBody=" + stringBuilder.toString()); //在广播接收者(BroadcastReceiver)中刷新界面UI效果 SmsReceiverTest.getSingleInstance().runOnUiThread( new Runnable() { @Override public void run() { message_address = (TextView) SmsReceiverTest.getSingleInstance().findViewById(R.id.message_address); message_address.setText(originatingAddress); message_body = (TextView) SmsReceiverTest.getSingleInstance().findViewById(R.id.message_body); message_body.setText(stringBuilder.toString()); } } ); } }}

AndroidManifest.xml

<!--授予程序接受短信的权限-->
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light">
<activity
android:name=".CrazyOne.broadcast.SmsReceiverTest"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!--通过清单文件注册广播-->
<!--指定短信接受者BroadcastReceiver的优先级为1000-->
<receiver android:name=".CrazyOne.broadcast.SmsReceiver">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED">
</action>
</intent-filter>
</receiver>


布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:id="@+id/listener"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开启监听短信到来的广播"/>
<TextView
android:id="@+id/messageAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="短信发过来的号码"
android:textColor="#0f0"/>
<TextView
android:id="@+id/message_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000"/>
<TextView
android:id="@+id/messageBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="短信发过来的内容信息"
android:textColor="#f00"/>
<TextView
android:id="@+id/message_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#000"/>

</LinearLayout>


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