android app is not sending sms automatically on re

2019-07-20 10:53发布

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.

标签: android sms
5条回答
太酷不给撩
2楼-- · 2019-07-20 11:34

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);        
}    

}

查看更多
Luminary・发光体
3楼-- · 2019-07-20 11:38

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.

查看更多
The star\"
4楼-- · 2019-07-20 11:41

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>
查看更多
可以哭但决不认输i
5楼-- · 2019-07-20 11:50

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);

查看更多
叛逆
6楼-- · 2019-07-20 11:54

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);
查看更多
登录 后发表回答