0

我已经实现了一个名为 Reciever 的类,它扩展了广播接收器......我想要的是要么在 toast 中显示收到的短信,我想在另一个 java 类中发送它,它是从 Activity 扩展的......这是该类的代码..

package com.sms.sms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class Reciever extends BroadcastReceiver {
    final SmsManager sms = SmsManager.getDefault();
    String msg="";
    String number="";
    @Override
    public void onReceive(Context arg0, Intent inn) 
    {
        Bundle bndl = inn.getExtras();
        try
        {
            if(bndl != null)
            {
                Object[] sms_details= (Object[]) bndl.get("pdus");
                for(int i=0 ; i<sms_details.length ; i++)
                {
                    SmsMessage message = SmsMessage.createFromPdu((byte[]) sms_details[i]);
                     number = message.getDisplayOriginatingAddress();
                     msg = message.getDisplayMessageBody();
//                  Toast.makeText(arg0, "Sender Number = "+number+" Your Message = "+msg, Toast.LENGTH_LONG).show();
                }
                Intent in = new Intent();
                in.setAction("SMS_RECIEVED_ACTION");
                in.putExtra("msg", msg);
                in.putExtra("num", number);
                arg0.sendBroadcast(in);

            }
        }
        catch(Exception e)
        {
            Toast.makeText(arg0, "Error", Toast.LENGTH_SHORT).show();
        }
    }

}

现在我有另一个名为 sms reciver 的类,其中包含 2 个编辑文本,我想在其中显示这些消息...

package com.sms.sms;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class recieve_sms extends Activity {
    EditText edt_msg;
    Bundle bndl;
    Button btn_rply;
    EditText edt_num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recieve);
        edt_msg = (EditText) findViewById(R.id.editText2);
        edt_num = (EditText) findViewById(R.id.editText1);
        btn_rply = (Button) findViewById(R.id.button1);
    //here should be some code which recieve msg from Reciever class and show in edittexts..    

    }

}
4

2 回答 2

0

假设您的广播接收器工作正常并且您的变量msgnumber很完美。

第一个选项 Show using Toast

只需在您的后面添加for

 Toast.makeText(context, msg, Toast.LENGTH_SHORT);

第二个选项在其他活动中显示

 Intent intent = new Intent(context, recieve_sms.class);
 intent.putExtra("msg", msg);
 intent.putExtra("num", number);
 startActivity(intent);

在: onCreate_recieve_sms

 Intent intent = getIntent();
 String number = intent.getStringExtra("num");
 String msg = intent.getStringExtra("msg");
于 2013-10-10T17:28:42.273 回答
0

简单的代码是你可以这样做

package com.tcs.smsactivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

public class MySMSReceiver extends BroadcastReceiver {

String action,from,message;
public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";


@Override
public void onReceive(Context context, Intent intent) {

     action=intent.getAction();


    Bundle bundle = intent.getExtras();        
        SmsMessage[] msgs = null;


        if(null != bundle)
        {
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];

            if(action.equals(SMS_RECEIVED_ACTION)){

                 for (int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                        info += msgs[i].getOriginatingAddress();                    
                        info += "\n*****TEXT MESSAGE*****\n";
                        info += msgs[i].getMessageBody().toString();
                        from=msgs[i].getOriginatingAddress();
                        message=msgs[i].getMessageBody().toString();


                    }

            } 
           }

    Intent showMessage=new Intent(context, AlertMessage.class);
    showMessage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    showMessage.putExtra("from", from);
    showMessage.putExtra("message", message);
    context.startActivity(showMessage);

}

}

现在在您的 AlertMessage Activity 类中,您可以获取所有数据并设置到编辑文本中:

警报消息.java

public class AlertMessage extends Activity{

String from,message;

private Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen);


    bundle=getIntent().getExtras();

if(null!=bundle){
    from=bundle.getString("from");
    message=bundle.getString("message");

           // set the information in to edit text here  

      }
}
}
于 2013-10-10T17:38:00.807 回答