自动短信发送的android [英] automatic sms sending in android
本文介绍了自动短信发送的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要开发出自动发送短信的手机短信短信的应用程序时收到一个短信,以predefind文本。
首先,我创建了广播接收机类
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);
}
}
}
和清单文件是
<?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.
推荐答案
尝试添加一个意图过滤到接收机。 android.provider.Telephony.SMS_RECEIVED是一个有序广播意图,这意味着具有较高优先级可能会阻止接收机具有较低优先级的接收器。
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>
这篇关于自动短信发送的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文