【发布时间】:2011-12-24 07:09:16
【问题描述】:
我创建了一个应用程序来分析正在接收的 SMS 并将消息发送回 Sender。 但是那些由我的手机自动发送的短信不会显示在默认消息查看器中。如何完成??
这是我用来发送短信的代码
package com.lalsoft.janko;
import java.util.Calendar;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.view.View;
public class SMSReceiver extends BroadcastReceiver
{
public String SendMsgBody;
private static final String LOG_TAG = "SMSReactor";
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String PhNo="";
String MsgBody="";
if (bundle != null)
{
//---retrieve the SMS message received---
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
PhNo=msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
MsgBody=msgs[i].getMessageBody().toString();
str += "\n";
}
if(MsgBody.equalsIgnoreCase("Test"))
{
SendSMS(PhNo,"Got you!!...Missile Launch initiated.Your location has been traced and the missile will hit your place in approximatly 2 Mins.. ESCAPE!!");
//SendSMS2(PhNo,"Got you!!...Missile Launch initiated.Your location has been traced and the missile will hit your place in approximatly 2 Mins.. ESCAPE!!");
}
}
/* public void SendSMS2(String phonenumber,String message)
{
startActivity(new Intent(Intent.ACTION_VIEW,Uri.fromParts(message, phonenumber, null)));
}
*/
private void SendSMS(String phonenumber,String message)
{
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(phonenumber, null, message, null, null);
}
}
在收到一条写有“测试”的短信时,我的应用程序将短信发送回发件人..但在此 SendSMS 方法中,它发送短信但发送消息未显示在本机/默认短信中应用。我应该怎么做才能让我的手机将此短信发送回本机短信应用程序中显示的发件人。
【问题讨论】:
-
请输入代码。没有代码,我们不能假设任何事情。
-
这是我用来发送短信的代码.. private void SendSMS(String phonenumber,String message) { SmsManager manager = SmsManager.getDefault(); manager.sendTextMessage(phonenumber, null, message, null, null); }
-
感谢链接..我正在寻找第一个方法..但是 startactivity 显示错误..“方法 startActivity(Intent) 未定义 SMSReceiver 类型”
标签: android sms broadcastreceiver