gpt4 book ai didi

android - 在android中自动发送短信

转载 作者:行者123 更新时间:2023-11-29 14:04:52 25 4
gpt4 key购买 nike

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

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

list 文件是

<?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>

它什么都不做,只显示我的主要 Activity 。帮助我哪里出错了。

最佳答案

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

<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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com