我正在开发一个接收端口短信的应用程序。 虽然我还没能收到短信。
清单中指定的权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" >
</uses-permission>
清单中的广播接收器
<receiver android:name=".BinarySMSReceiver" >
<intent-filter android:priority="10" >
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data
android:host="*"
android:port="80"
android:scheme="sms" />
</intent-filter>
</receiver>
广播接收器类
public class BinarySMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
if (null != bundle) {
// String info = "SMS from ";
String info = "SMS from ";
String sender = "";
String sms = "";
// String info = "";
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
byte[] data = null;
for (int i = 0; i < msgs.length; i++) {
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sender += msgs[i].getOriginatingAddress();
// info += "\n*****BINARY MESSAGE*****\n";
info += msgs[i].getOriginatingAddress() + "\n";
data = msgs[i].getUserData();
for (int index = 0; index < data.length; ++index) {
info += Character.toString((char) data[index]);
sms += Character.toString((char) data[index]);
}
}
DatabaseHandler db = new DatabaseHandler(context);
db.addSMS(new SMS(sender, sms));
Intent i = new Intent(context, SMSListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
i, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("PORT SMS")
// Vibration
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
// LED
.setLights(Color.RED, 3000, 3000)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setWhen(System.currentTimeMillis())
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(info)).setContentText(info);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(2, mBuilder.build());
Log.e("msgg ", info);
// t.setText(info);
Toast.makeText(context, info, Toast.LENGTH_SHORT).show();
}
}
}
我在这里遗漏了清单中的东西吗?