从特定号码接收短信时启动应用程序

MAk*_*AkS 2 sms android

我想开始一个从特定号码接收短信的应用程序.我正在尝试使用onMessageWaitingIndicatorChanged(boolean mwi){}方法,但我正在努力.那么,有谁在那里帮我详细?谢谢

Chr*_*ris 5

您需要注册广播接收器android.provider.Telephony.SMS_RECEIVED.然后,接收方可以检查SMS的号码并根据需要开始您的活动.

所以,你需要:

  • 在您的清单中添加一个uses-permissionforandroid.permission.RECEIVE_SMS
  • <application/>在manflement中声明一个广播接收器:

    <receiver android:name=".YourReceiverName"> 
        <intent-filter> 
            <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
        </intent-filter> 
    </receiver>
    
    Run Code Online (Sandbox Code Playgroud)
  • 创建接收器类,扩展IntentReceiver.

  • onReceiveIntent,您可以通过调用Telephony.Sms.Intents.getMessagesFromIntent()并传入您提供的意图来获取相关消息.
  • 如果数字与您想要的数字匹配,则可以通过调用启动活动 startActivity