如果我将我的应用设置为默认消息传递应用,则所有传入消息都不会显示在其他消息应用上。这是我的SmsReceiver和清单。
@Override
public void onReceive(Context context, Intent intent) {
Object[] smsExtra = (Object[]) intent.getExtras().get("pdus");
String body = "";
for (int i = 0; i < smsExtra.length; ++i) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
body += sms.getMessageBody();
}
Notification notification = new Notification.Builder(context)
.setContentText(body)
.setContentTitle("New Message")
.setSmallIcon(R.drawable.ic_alert)
.setStyle(new Notification.BigTextStyle().bigText(body))
.build();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(1, notification);
}
<receiver
android:name=".SmsReceiver"
android:permission="android.permission.BROADCAST_SMS"
android:enabled="false"
android:exported="true" >
<intent-filter android:priority="0" >
<action android:name="android.provider.Telephony.SMS_DELIVER" />
</intent-filter>
</receiver>
消息仅显示为通知,然后未在其他消息传递应用程序中列出。实际上,我只想将我的应用程序设置为默认仅发送彩信,因此我不想通过我的应用处理传入的消息。
谢谢。
答案 0 :(得分:0)
更新了接收器,如下所示。它的工作原理
公共类SmsReceiver扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Object[] smsExtra = (Object[]) intent.getExtras().get("pdus");
String body = "";
String msg_from = "";
SmsMessage[] msgs = null;
msgs = new SmsMessage[smsExtra.length];
for (int i = 0; i < smsExtra.length; ++i) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
msgs[i] = SmsMessage.createFromPdu((byte[])smsExtra[i]);
msg_from = msgs[i].getOriginatingAddress();
body += sms.getMessageBody();
}
ContentValues values = new ContentValues();
values.put("address", msg_from);//sender name
values.put("body", body);
context.getContentResolver().insert(Uri.parse("content://sms/inbox"), values);
Notification notification = new Notification.Builder(context)
.setContentText(body)
.setContentTitle(msg_from)
.setSmallIcon(R.drawable.ic_alert)
.setStyle(new Notification.BigTextStyle().bigText(body))
.build();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
notificationManagerCompat.notify(1, notification);
}
}