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

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>


它什么也没做,只会显示我的主要活动。
帮帮我,我要去哪里错了。

最佳答案

尝试在接收器中添加一个意图过滤器。 “ 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>

关于android - 在Android中自动发送短信,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7634393/

10-13 06:15