我是 Android 开发的新手,只是想创建一个程序,如果我收到一条短信,它将显示一个 Toast ......这是到目前为止的代码
SMSReceiveActivity.java
package sms.receive.sample;
import android.content.*;
import android.os.Bundle;
import android.telephony.*;
import android.util.Log;
import android.widget.Toast;
public class SMSReceiveActivity extends BroadcastReceiver {
private static final String TAG = "Message recieved";
@Override
public void onReceive(Context context, Intent intent) {
Bundle pudsBundle = intent.getExtras();
Object[] pdus = (Object[]) pudsBundle.get("pdus");
SmsMessage messages =SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.i(TAG, messages.getMessageBody());
Toast.makeText(context, "SMS Received : "+messages.getMessageBody(),
Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sms.receive.sample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application icon="@drawable/ic_launcher" label="@string/app_name">
<receiver
android:name=".SMSReceiveActivity"
android:exported="true"
android:enabled="true">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
当我向 AVD 发送(telnet)短信时,我会在抽屉中收到通知,但不会收到 Toast。我也试过我的手机,我也得到了同样的结果......非常感谢任何帮助。