Working example of sendDataMessage() for android

て烟熏妆下的殇ゞ 提交于 2019-12-22 12:17:07

问题


I have tried to use the sendDataMessage() of android.telephony.SmsManager with the help of almost every sample that i could come accross.. Yet no success..

[In case u want to see the code then simply check the "Sending Sms android" link on mobiForge, i guess its the most popular one (and the one that i've used).]

This is one of the examples in reference to this question.

When i use the standard receiver shown in samples as follows, my Broadcast Receiver does indeed get activated and i am able to see the Toast which contains my message thus proving that my receiver is successfully running. [By the way, i am calling the SmsManager.sendTextMessage() which is working successfully so no issues there].

<receiver android:name=".SMSReceiver"> 
            <intent-filter> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
            </intent-filter>
</receiver>

However when i use the following receiver, i am unable to get the Toast which means that my receiver is clearly not getting activated, hence no Toast.

<receiver android:name=".SMSReceiver"> 
            <intent-filter> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
                <data android:port="8901"/>
                <data android:scheme="sms"/>
            </intent-filter> 
</receiver>

My question here is Why? In case some of you are thinking that the port number in my sendDataMessage() is not right then i assure you that its 8901 which is a short value.

Hence i would like to request anyone with a solution to this to either explain the solution or provide a working example.

Thanking anyone who can help in advance!

Best Regards, Siddhant


回答1:


Well i seem to have found out the problem in my code.

The fix was to change the android:name value in the above shown receiver from android.provider.Telephony.SMS_RECEIVED to android.intent.action.DATA_SMS_RECEIVED

So the new receiver will look like:

<receiver android:name=".SMSReceiver">  
            <intent-filter>  
                <action android:name="android.intent.action.DATA_SMS_RECEIVED" />  
                <data android:port="8901"/> 
                <data android:scheme="sms"/> 
            </intent-filter>  
</receiver>

Thanks to KRVarma SMSDemo which provided some really useful insight after understanding the code.




回答2:


Here are details of a possible bug with the Android emulator regarding this functionality.



来源:https://stackoverflow.com/questions/8972905/working-example-of-senddatamessage-for-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!