安卓使用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使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户
- 安卓用ContentObserver监听发出的短信的信息日志
- 使用短信数据库监听特定号码短信并处理信息内容遇到的问题
- Android使用ContentObserver监听数据库变化 接受指定号码的短信,并且不让系统截取到通知用户
- 安卓短信功能全解:调用系统短信功能发送短信、彩信,使用SmsManager发送短信,并监听发送短信的投递情况,使用广播接收器监听接收的短信。
- Android 使用ContentObserver监听短信的变化,并发送信息给特定的手机号码
- 使用broadcastreceiver监听短信
- 使用MTM发送中文短信 需要更改的配置信息
- (转)[Android实例] 关于使用ContentObserver监听不到删除短信会话的解决方案
- android 使用广播接收者监听短信和拦截外拨电话
- 关于使用ContentObserver监听不到删除短信会话的解决方案
- Android手机使用广播监听手机收到的短信
- 11g R2 RAC使用srvctl工具添加监听并手动加入静态注册信息
- 安卓实训第十四天---使用ContentProvider共享数据,并且利用ContentResolver监听共享数据
- 使用广播接收者怎么只能监听收到的短信 ?为啥短信发送号码和时间为null
- 安卓应用程序在真机上调试时无打印信息,提示Unable to open log device ‘/dev/log/main’: No such file or directory在我们使用真机进行An
- 安卓四大组件复习---监听短信(二)
- 使用广播接收者监听短信
- Android监听发送短信和接受短信的事件
- 从安卓recovery 的 backup 备份数据中手动恢复 通讯录、短信,或者其他软件中的信息的方法