2

我开发了一个 android 应用程序,它会自动将 SMS 自动发送到它接收 SMS 的设备。

我的应用程序在模拟器上运行良好,但是当我在真实设备(android 手机)上运行它时,它只接收短信并且不会自动发送响应。

我的代码如下。

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

我不知道是什么问题。以及为什么它在真实设备上无法正常工作。

4

5 回答 5

2

试试这个代码发送短信。

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

}

于 2012-09-04T12:17:02.703 回答
1

您是否添加了将 SMS 发送到您的AndroidManifest.xml-file 的权限?

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

这些是相关的权限

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.

正如Dya 的回答所暗示的,使用PendingIntent可以调试您的sendTextMessage()-method上的操作。

错误代码如下:

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

根据本教程(和Mo Al Sh 的回答),您可以尝试另一种内置的发送 SMS 的方式:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content"); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
于 2012-09-04T11:56:56.097 回答
1

确保您已在manifest.xml中输入为;

<receiver android:name=".ListenerSms">
   <intent-filter ><action android:name="android.provider.Telephony.SMS_RECEIVED"/>
   </intent-filter>
</receiver>
于 2013-09-23T13:06:38.597 回答
0

你有没有检查你是否有短信权限

使用权限 android:name="android.permission.SEND_SMS"

在你的清单文件中?

您也可以尝试内置 SMS 应用程序:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);

sendIntent.putExtra("sms_body", "默认内容");

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

开始活动(发送意向);

于 2012-09-04T11:56:28.513 回答
0

您无法发送消息的原因是因为您没有包含:

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

您可以从平板电脑/手机消息部分获取此消息地址。

于 2013-01-24T03:03:49.507 回答