我是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)短信时,我在抽屉里得到了nofication,但没有Toast。我也试过我的手机而且我得到了相同的.... 任何帮助非常感谢。