2013-10-10 142 views
0

我已經實現了一個名爲Reciever的擴展廣播接收器... 我想要的是,要麼顯示接收到的短信在吐司我想發送它在另一個Java類,這是從活動擴展。 。這裏是類的代碼..Android ListView項目ID

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

} 

現在我還有一個名爲類短信reciver包含2個edittexts中,我想顯示這些封郵件......

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..  

    } 

} 
+0

什麼是您的問題.. – Zyoo

回答

0

假設你廣播接收機工作正常你的變量msgnumber也是完美的。其他活動

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

第二個選項顯示:

只需將您的for後補充一點:

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"); 
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類裏面,你可以抓住所有的數據和設置爲編輯文本:

AlertMessage.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 

     } 
} 
}