短信接收,无通知

问题描述

| 我想在我的应用程序中收到短信,但我不希望我的Android显示有关该事件的通知。算法: 接收短信(可以) 如果这是具有特殊内容格式的短信(针对我的应用程序),请使用我的应用程序对其进行处理,并且不显示通知。 如果这是一条简单消息-我不想处理它,那么必须显示一条通知。 我尝试使用有序广播,但无济于事。我在某个地方看到SMS_RECEIVE的广播没有命令,但是我看到一些应用程序可以在不通知的情况下接收SMS。 有谁能帮助我或向我展示解决此问题的正确方法? 在广播中呼叫“ 0”无济于事     

解决方法

在您的意图过滤器中设置优先级 //在清单中
<intent-filter android:priority=\"100\">  /*your receiver get high priority*/
//在广播接收器中
 if (keyword_match)
      {
        // Stop it being passed to the main Messaging inbox
        abortBroadcast();
      }
    ,通过注册您的应用程序以接收SMS消息,然后在检测到消息到达时使用abortBroadcast(),应该可以实现此目的。您说abortBroadcast()不起作用-SMS肯定会被您的SMS接收器处理吗? 对于其他想要这样做的人,请继续阅读... 首先,在您的AndroidManifest.xml中声明SMS接收器,并确保该应用有权接收SMS消息。
 <receiver android:name=\"mypackage.SMSReceiver\">
   <intent-filter>
     <action android:name=\"android.provider.Telephony.SMS_RECEIVED\"/>
   </intent-filter>
 </receiver>

<uses-permission android:name=\"android.permission.RECEIVE_SMS\" />
这是一些处理SMS消息的示例代码:
public class SMSReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive(Context context,Intent intent)
  {
    Bundle extras = intent.getExtras();

    Object[] pdus = (Object[])extras.get(\"pdus\");
    for (Object pdu: pdus)
    {
      SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);

      String origin = msg.getOriginatingAddress();
      String body = msg.getMessageBody();

      // Parse the SMS body
      if (isMySpecialSMS)
      {
        // Stop it being passed to the main Messaging inbox
        abortBroadcast();
      }
    }
  }
}
    ,您不应该这样做。其他应用可能想要或需要接收SMS_RECEIVED广播。中止它会破坏第三方应用程序的正常运行。这是一种不好的编程方式。您只应中止您创建的广播,而不中止系统广播。我不知道为什么Android OS允许您执行此操作。     ,不知道我是否确切知道您要做什么,但据我了解,您只是想知道如何不发送通知? 你为什么不能做:
If(instance 2){
//do your processing
}else{
//send notification
}
如果您是想阻止OS进行广播,那么您可能不走运,因为我不相信您可以做到这一点     ,您需要android:priority属性才能正常工作
<intent-filter android:priority=\"1\"> 
                <action android:name=\"android.provider.Telephony.SMS_RECEIVED\" /> 
</intent-filter> 
    ,你犯了一个错误 因为您解雇了abortBroadcast();如果您的特殊消息使消息广播中止,因此SMS通知未显示,并且短信未保存在收件箱中, 如果收到您的特殊消息,则必须在“ onRecive”中发出自己的通知。 例:
      public class SMSReceiver extends BroadcastReceiver
    {
      @Override
      public void onReceive(Context context,Intent intent)
      {
        Bundle extras = intent.getExtras();

        Object[] pdus = (Object[])extras.get(\"pdus\");
        for (Object pdu: pdus)
        {
          SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);

          String origin = msg.getOriginatingAddress();
          String body = msg.getMessageBody();

          // Parse the SMS body
          if (isMySpecialSMS)
          { // Stop it being passed to the main Messaging inbox
            abortBroadcast();
NotificationManager notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,\"New Message (My Messaging app)\",System.currentTimeMillis());
        // Uses the default lighting scheme
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        // Will show lights and make the notification disappear when the presses it
        notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
        Intent notificationIntent = new Intent(context,SplashActivity.class);
        PendingIntent pendingIntent =PendingIntent.getActivity(context,new Intent(context,SplashActivity.class),0);

        Log.i(\"Wakeup\",\"Display Wakeup\");
        PowerManager pm = (PowerManager)context.getApplicationContext().getSystemService(Context.POWER_SERVICE);
        WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP),\"Phone WakeUp\");
        wakeLock.acquire();

        notification.setLatestEventInfo(context,\"My Messaging App(New Message)\",msg,pendingIntent);
        //notification.sound.
        notification.defaults |= Notification.DEFAULT_SOUND;
        notificationManager.notify(9999,notification);
          }
         else{
        //Continue Broadcasting to main android app
         }
        }
      }
    }
我希望这能解决您的问题     

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...