Android 中自动发送短信

发布于 2024-12-08 07:08:25 字数 3184 浏览 0 评论 0原文

我想开发一个短信应用程序,当收到短信时,它会自动向“短信发送者”发送短信,并带有预定义的文本。 首先,我创建了广播接收器类

public class MyBroadCastReceiver extends BroadcastReceiver {
private final String MSG_BODY="Thank you for contact we will contact u later";
final int MAX_SMS_MESSAGE_LENGTH=160;
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
    {

            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();

                    }
                        Toast.makeText(context, "SMS sent",
                              Toast.LENGTH_SHORT).show();
                        //String msgBody = msgs[i].getMessageBody();

                    msg_from = msgs[0].getOriginatingAddress();
                    sendSms(msg_from,MSG_BODY);
                    }

                    catch(Exception e){Log.d("Exception caught",e.getMessage());}
                }
            }   
}
 private void sendSms(String phonenumber,String message)
    {
        SmsManager manager = SmsManager.getDefault();


                int length = message.length();

                if(length > MAX_SMS_MESSAGE_LENGTH)
                {
                        ArrayList<String> messagelist = manager.divideMessage(message);

                        manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
                }
                else
                {
                        manager.sendTextMessage(phonenumber, null, message, null, null);
                }
        }
    }

,清单文件是

<?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.myapp"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:enabled="true"     

   android:name="com.myapp.MyBroadCastReceiver">   
   </receiver>
    <activity android:name=".MeraSms"
              android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

它什么也不做,只显示我的主要活动。 帮我看看我哪里出错了。

I want to develop a sms application which send automatic sms to the "sms sender" when receive a sms, with a predefind text.
First i created the broadcast receiver class

public class MyBroadCastReceiver extends BroadcastReceiver {
private final String MSG_BODY="Thank you for contact we will contact u later";
final int MAX_SMS_MESSAGE_LENGTH=160;
@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
    {

            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();

                    }
                        Toast.makeText(context, "SMS sent",
                              Toast.LENGTH_SHORT).show();
                        //String msgBody = msgs[i].getMessageBody();

                    msg_from = msgs[0].getOriginatingAddress();
                    sendSms(msg_from,MSG_BODY);
                    }

                    catch(Exception e){Log.d("Exception caught",e.getMessage());}
                }
            }   
}
 private void sendSms(String phonenumber,String message)
    {
        SmsManager manager = SmsManager.getDefault();


                int length = message.length();

                if(length > MAX_SMS_MESSAGE_LENGTH)
                {
                        ArrayList<String> messagelist = manager.divideMessage(message);

                        manager.sendMultipartTextMessage(phonenumber, null, messagelist, null, null);
                }
                else
                {
                        manager.sendTextMessage(phonenumber, null, message, null, null);
                }
        }
    }

And the manifest file is

<?xml version="1.0" encoding="utf-8"?>
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.myapp"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:enabled="true"     

   android:name="com.myapp.MyBroadCastReceiver">   
   </receiver>
    <activity android:name=".MeraSms"
              android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

It does nothing only display my main activity.
Help me where i am going wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还在原地等你 2024-12-15 07:08:25

尝试在接收器中添加一个意图过滤器。 “android.provider.Telephony.SMS_RECEIVED”是一个有序的广播意图,这意味着具有较高优先级的接收者可能会阻止具有较低优先级的接收者。

<receiver android:enabled="true" android:name="com.myapp.MyBroadCastReceiver">   
  <intent-filter android:priority="10000">
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-fileter>
</receiver>

Try adding an intent-filter into the receiver. "android.provider.Telephony.SMS_RECEIVED" is an ordered broadcast intent, which means a receiver with a higher priority may block receivers with lower priority.

<receiver android:enabled="true" android:name="com.myapp.MyBroadCastReceiver">   
  <intent-filter android:priority="10000">
    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  </intent-fileter>
</receiver>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文