2017-10-19 328 views
0

如果我将我的应用程序设置为默认消息应用程序,则所有传入的消息都不会显示在其他消息应用程序上。这是我的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

'这是我的SmsReceiver和明显file.'哪里清单你说的吗?编辑你的问题。 – Xenolion

+0

我刚发布的woops。 –

+0

默认应用程序负责将所有传入短信写入提供者。它也负责所有传入的彩信。如果您的应用程序是默认应用程序,它必须是一个完整的消息应用程序。 –

回答

0

更新后的接收器如下。它的工作原理

公共类SmsReceiver扩展广播接收器{

@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); 
} 

}

相关问题