阅读并显示传入的消息文本android

阅读并显示传入的消息文本android,第1张

阅读并显示传入的消息文本android

试试这个对我有用的东西,您将看到祝酒词和收到的消息内容:

package com.example.a;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.Toast;    public class SMSBroadcastReceiver extends BroadcastReceiver { private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; private static final String TAG = "SMSBroadcastReceiver"; @Override public void onReceive(Context context, Intent intent) {      Log.i(TAG, "Intent recieved: " + intent.getAction());         if (intent.getAction().equals(SMS_RECEIVED)) {  Bundle bundle = intent.getExtras();  if (bundle != null) {      Object[] pdus = (Object[])bundle.get("pdus");      final SmsMessage[] messages = new SmsMessage[pdus.length];      for (int i = 0; i < pdus.length; i++) {          messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);      }      if (messages.length > -1) {          Toast.makeText(context, "Message recieved: " + messages[0].getMessageBody(), 7000).show();      }  }         }    }        }

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.a"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <uses-permission android:name="android.permission.RECEIVE_SMS"/>    <uses-permission android:name="android.permission.HARDWARE_TEST"/>    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <receiver android:name=".SMSBroadcastReceiver" > <intent-filter>     <action android:name="android.provider.Telephony.SMS_RECEIVED" >     </action> </intent-filter>        </receiver>    </application></manifest>

使用DDMS通过Telnet将短信发送到仿真器



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5499647.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存