Android 中自动发送短信
我想开发一个短信应用程序,当收到短信时,它会自动向“短信发送者”发送短信,并带有预定义的文本。 首先,我创建了广播接收器类
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在接收器中添加一个意图过滤器。 “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.