android app is not sending sms automatically on real device

孤者浪人 提交于 2019-12-24 03:51:45

问题


I have developed an android app that automatically sends an SMS automatically to a device from which it receives an SMS.

My App is working fine on the emulator but when I run it on a real device (android mobile) then it only receives SMSs and does not sends a reponse automatically.

My code is as follows.

public class SMSReciever extends BroadcastReceiver {

    String address;
    String smsMe = "I Recieved Your SMS";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        Object messages[] = (Object[]) bundle.get("pdus");
        SmsMessage smsMessage[] = new SmsMessage[messages.length];
        for (int n = 0; n < messages.length; n++) {
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            address = smsMessage[n].getOriginatingAddress();
        }

        Toast toast = Toast.makeText(context,"Received SMS: " +
                 smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
        toast.show();
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(address, null, smsMe, null, null);
    }
}

I don't know what is the problem. And why it is not working properly on a real device.


回答1:


Try this code to send sms.

//---sends an SMS---
private void sendSMS(String phoneNumber, String message)
{        
    PendingIntent pi = PendingIntent.getActivity(this, 0,
        new Intent(this, class_name.class), 0);                
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, pi, null);        
}    

}




回答2:


Have you added permissions to send SMS'es to your AndroidManifest.xml-file?

<manifest ...>
    <uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
</manifest>

These are related permissions:

SEND_SMS        Allows an application to send SMS messages.
BROADCAST_SMS   Allows an application to broadcast an SMS receipt notification
READ_SMS        Allows an application to read SMS messages.
RECEIVE_SMS     Allows an application to monitor incoming SMS messages, to record or perform processing on them.
WRITE_SMS       Allows an application to write SMS messages.

As Dya's answer suggests, using PendingIntents, will make it possible to debug the action on your sendTextMessage()-method.

The error codes are as follows:

Activity.RESULT_OK
Activity.RESULT_ERROR_GENERIC_FAILURE
Activity.RESULT_ERROR_RADIO_OFF
Activity.RESULT_ERROR_NULL_PDU

According to this tutorial (and Mo Al Sh's answer) there's another built in way of sending SMS'es you could try:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content"); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);



回答3:


Make sure that u have make entry in manifest.xml as;

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



回答4:


Have you checked if you have SMS permission

uses-permission android:name="android.permission.SEND_SMS"

in your manifest file ?

also you can try built in SMS application :

Intent sendIntent = new Intent(Intent.ACTION_VIEW);

sendIntent.putExtra("sms_body", "default content");

sendIntent.setType("vnd.android-dir/mms-sms");

startActivity(sendIntent);




回答5:


The reason you are not able to send the message is because you have not included:

message_sc_address sms.sendTextMessage(address, message_sc_address, smsMe, null, null);

You can get this message address from your tablet/phone message section.



来源:https://stackoverflow.com/questions/12262899/android-app-is-not-sending-sms-automatically-on-real-device

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