在 Android 的默认消息查看器中显示发送短信

发布于 2024-12-23 00:12:55 字数 2431 浏览 0 评论 0原文

我创建了一个应用程序,可以分析正在接收的短信并将消息发送回发件人。 但是我的手机自动发送的那些短信不会显示在默认消息查看器中。怎样才能完成呢??

这是我用来发送短信的代码

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 方法中,它发送短信,但未显示发送消息在本机/默认 SMS 应用程序中。我应该做什么才能让我的手机将此短信发送回本机短信应用程序中显示的发件人。

I have created an app that analyses the SMS that are being received and send messages back to the the Sender.
But those SMS which are being send by my phone automatically are not shown in the default message viewer. How to get it done??

This is the code i use to send SMS

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

}

Here on receiving an SMS with "test" written on it, my application sends the SMS back t o the sender..but in this SendSMS method, its sending the SMS but the send Message is not showing in the native/default SMS Application. What should i do to make this SMS send by my phone back to the Sender being shown in the native SMS application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埖埖迣鎅 2024-12-30 00:12:55

最后我成功了..虽然我读过很多地方这个方法不应该在很多地方使用.. 像这样我在这里使用这个方法..所以如果有人使用这个代码,那么请先了解这些问题..

这是我的代码..

 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!!");

    }

就是这样..希望它能帮助像我这样的人..

Finally i got success..Though i have read in may places this method shouldn't be used in many places.. like this i am using this method here..so if any one is using this code then please be informed about the issues first..

here is my code..

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

I am calling this method from this portion( full code shown in the question itself). Additional context is added in my above code..

    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!!");

    }

That's it.. Hope it helps someone else like me..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文