【问题标题】:Showing Send SMS in the default Message Viewer in android在android的默认消息查看器中显示发送短信
【发布时间】: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


【解决方案1】:

最后我成功了..虽然我在可能的地方读过这个方法不应该在很多地方使用..like this我在这里使用这个方法..所以如果有人使用这个代码那么请第一时间了解问题。

这是我的代码..

 private void SendSMS(Context c,String phonenumber,String message)
{
    SmsManager manager = SmsManager.getDefault();
    manager.sendTextMessage(phonenumber, null, message, null, null);

    //For displaying in Outbox
    ContentValues values = new ContentValues();
    values.put("address", phonenumber);
    values.put("body", SendMsgBody);
    c.getContentResolver().insert(Uri.parse("content://sms/sent"), values);
}

我从这部分调用此方法(完整代码显示在问题本身中)。在我上面的代码中添加了额外的上下文..

    if(MsgBody.equalsIgnoreCase("Test"))
    {
           SendSMS(context,PhNo,"Got you!!...Missile Launch initiated.Your location has been traced and the missile will hit your place in approximatly 2 Mins.. ESCAPE!!");

    }

就是这样..希望它可以帮助像我这样的其他人..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2016-07-03
    • 1970-01-01
    相关资源
    最近更新 更多