【问题标题】:How to use PendingIntent in order to launch an application when SMS is received?收到短信时如何使用 PendingIntent 启动应用程序?
【发布时间】:2011-10-04 16:04:20
【问题描述】:

当仅从某个号码和某个端口收到 SMS 时,我需要启动应用程序。如果应用程序已经运行,我需要解析此消息。如何使用 PendingIntent 做到这一点?

【问题讨论】:

    标签: android sms android-pendingintent


    【解决方案1】:

    首先使广播接收器类接收这样的消息。 并在收到短信时触发您班级的意图。

       class SMSReceiver extends BroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        if (bundle != null) {
          Object[] pdus = (Object[]) bundle.get("pdus");
          msgs = new SmsMessage[pdus.length];
          for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            str += "SMS from " + msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";
          }
          Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
          Intent mainActivityIntent = new Intent(context, SMS.class);
          mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(mainActivityIntent);
          Intent broadcastIntent = new Intent();
          broadcastIntent.setAction("SMS_RECEIVED_ACTION");
          broadcastIntent.putExtra("sms", str);
          context.sendBroadcast(broadcastIntent);
        }
      }
    }
    

    将 sms_sender 存储在字符串中

      String msg_sender=msg[0].getOriginatingAddress();
    

    并在收到消息后给出条件

       if(msg_sender=(String)"your_unique_number")
       and fire the intent of your class if this condition is true.
    

    【讨论】:

      猜你喜欢
      • 2015-09-19
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多