2014-09-26 56 views
1

我有抓短信SmsListener類和我有TextVew在我的主要活動,之後接收傳入的短信,我需要在主要活動中更新的TextView的TextView從SmsListener類活動的

public class SmsListener extends BroadcastReceiver{ 


     @Override 
     public void onReceive(Context context, Intent intent) { 
      // TODO Auto-generated method stub 

      if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){ 
       Bundle bundle = intent.getExtras();   
       SmsMessage[] msgs = null; 
       String msg_from; 
       if (bundle != null){ 
        try{ 
         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]); 
          msg_from = msgs[i].getOriginatingAddress(); 
          String msgBody = msgs[i].getMessageBody(); 

there i want update TextView on my Main activity 
         } 
        }catch(Exception e){ 
    //       Log.d("Exception caught",e.getMessage()); 
        } 
       } 
      } 
     } 
    } 

幫我

+0

我需要通過msgbody在另一個活動 – Serjaru 2014-09-26 23:14:40

+0

到TextView的而且有多種方式來做到這一點,它們中的哪一個你試過嗎?在發佈問題之前,您需要做出努力。 – cYrixmorten 2014-09-26 23:16:35

回答

0

在你的MainActivity中初始化MainActivity類的一個變量,如下所示。

public class MainActivity extends Activity { 
private static MainActivity ins; 
protected void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
ins = this;  
} 

public static MainActivity getInstace() 
{ 
    return ins; 
} 
public void updateTheTextView(final String t) { 
MainActivity.this.runOnUiThread(new Runnable() { 
    public void run() { 
     TextView textV1 = (TextView) findViewById(R.id.textV1); 
     textV1.setText(t); 
    } 
}); 
} 
} 


public class NotifAlarm extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
      try { 
      MainActivity .getInstace().updateTheTextView("String"); 
     } catch (Exception e) { 

     } 

    } 
}