android广播监听短信并显示内容
2018-07-20 来源:open-open
AndroidManifest.xml中添加
再写一个广播监听
<receiver android:name=".receive"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
再写一个广播监听
public class receive extends BroadcastReceiver { String receiveMsg = ""; public void onReceive(Context context, Intent intent) { SmsMessage[] msg= null; if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) { //StringBuilder buf = new StringBuilder(); Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdusObj = (Object[]) bundle.get("pdus"); msg= new SmsMessage[pdusObj.length]; for (int i = 0; i<pdusObj.length; i++) msg[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]); } for(int i = 0; i < msg.length; i++) { String msgTxt = msg[i].getMessageBody(); if (msgTxt.equals("Testing!")) { Toast.makeText(context, "success!", Toast.LENGTH_LONG).show(); return; } else { Toast.makeText(context, msgTxt, Toast.LENGTH_LONG).show(); return; } } return; } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇:SpringMVC上传下载
最新资讯
热门推荐