2012-08-28 105 views
0

在我的Android項目,我需要檢測和讀取文本短信,因爲他們進來,然後調用另一個類的功能。Android的 - 如何使用廣播接收器來讀取短信?

我如何可以把下面的代碼在一個類,並在清單正確地定義它。如果我把它放在另一個Java文件上,那麼我不知道如何從另一個Java文件中調用一個函數。 我試圖把這個代碼在我的主類,但在我的應用程序,它只是墜毀。

public class SMSReceiverActivity extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     // Parse the SMS. 
     Bundle bundle = intent.getExtras(); 
     SmsMessage[] msgs = null; 
     String str = ""; 
     if (bundle != null) 
     { 
      // Retrieve the SMS. 
      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]); 
       // In case of a particular App/Service. 
       //if(msgs[i].getOriginatingAddress().equals("+91XXX")) 
       //{ 
       //str += "SMS from " + msgs[i].getOriginatingAddress(); 
       //str += " :"; 
       str += msgs[i].getMessageBody().toString(); 
       str += "\n"; 
       //} 
      } 
      if (str != "") { // remove the last \n 
       str = str.substring(0, str.length()-1); 
      } 

      Reusable_CodeActivity.alert(my_ViewActivity.this, "AAAAAAAAAAA"); 
      try { 
       //my_ViewActivity.this.handle_incoming_message(str); 
      } catch(Exception e) { 

      } 
     } 
    } 
} 
+0

發佈你的清單文件。另外你的意思是「墜毀」? –

回答

0

您接收短信的代碼是正確的。但是當你在Activity類使用BroadCastReceiver你需要在的onStop()方法如下注銷它。我認爲這就是你的活動被粉碎的原因。

@Override 
protected void onStop() 
{ 
    unregisterReceiver(mReceiver); 
    super.onStop(); 
} 
0

如果我正確理解你的問題,你想接收廣播(當短信到達)您的活動展示。

如果是這樣,我發現了一個很好的模式來做這件事回來(不記得我從哪裏得到它,將是很好的歸功於原作者)。

首先,創建一個接口:

public interface Receiver { 
    public void onReceive(Context context, Intent intent); 
} 

現在創建了BroadcastReceiver的子類,我們可以在運行時動態註冊:

public class PortableReceiver extends BroadcastReceiver { 
    private Receiver mReceiver; 

    public void setReceiver(Receiver receiver) { 
     mReceiver = receiver; 
    } 

    public Receiver getReceiver() { 
     return mReceiver; 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (mReceiver != null) 
      mReceiver.onReceive(context, intent); 
     else 
      Log.w("PortableReceiver", "Dropping received Result"); 
    } 
} 

現在我們可以在活動課上使用PortableReceiver:

public class MyActivity implements Receiver { 
    private PortableReceiver mReceiver; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mReceiver = new PortableReceiver(); 
     mReceiver.setReceiver(this); 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     // Register the receiver 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction("_my_intent_action"); //TODO: add actions to filter just as you would in your AndroidManifest 
     registerReceiver(mReceiver, filter, null, null); 
    } 

    @Override 
    public void onPause() { 
     super.onPause(); 
     unregisterReceiver(mReceiver); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //TODO: Your code goes here! 
    } 
} 

現在,你有你的Activity類直接處理廣播,所以你可以訪問它提供的所有功能。

請注意,如果你想開始來自BroadcastReceiver的活動,這就有點不同了。如果是這樣的話,我推薦你退房http://www.vogella.com/articles/AndroidServices/article.html#pre_broadcastreceiver

+0

收件箱打開時是否可以接收廣播? @DanielGrech –

相關問題