2011-06-28 196 views
3

這裏是我的源代碼和它保持強制關閉,每次我運行它...如何註冊廣播接收器?

public class MainActivity extends Activity { 
    private static String content; 
    private static String phone; 
    private String number; 
    private String message; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     registerReceiver(new BroadcastReceiver(){ 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       //---get the SMS message passed in--- 
       Bundle bundle = intent.getExtras();   
       SmsMessage[] msgs = null; 

       if (bundle != null) 
       { 
        number = ""; 
        message = ""; 
        //---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]);     
         number = msgs[i].getOriginatingAddress();      
         message = msgs[i].getMessageBody(); 
        } 
        //---display the new SMS message--- 
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
        SendMe(); 
       } 
      } 
     }, null); 
    } 

    public void SendMe(){ 
     PendingIntent pi = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 0); 
     SmsManager sms = SmsManager.getDefault(); 
     sms.sendTextMessage(number, null, message, pi, null); 
    } 
} 

這裏是logcat的我得到...

06-28 14:39:00.331: ERROR/AndroidRuntime(396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ftt.autospond/com.ftt.autospond.MainActivity}: java.lang.NullPointerException 
06-28 14:39:00.331: ERROR/AndroidRuntime(396):  at android.app.ActivityManagerProxy.registerReceiver(ActivityManagerNative.java:1504) 
06-28 14:39:00.331: ERROR/AndroidRuntime(396):  at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:807) 
06-28 14:39:00.331: ERROR/AndroidRuntime(396):  at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318) 
06-28 14:39:00.331: ERROR/AndroidRuntime(396):  at com.ftt.autospond.MainActivity.onCreate(MainActivity.java:29) 
+0

當你問一個力關閉它確實有助於包括與異常堆棧跟蹤 – antlersoft

+0

@antlersoft我剛纔編輯上面的代碼,包括logcat中的logcat的。一探究竟。謝謝 – theITRanger22

回答

4

您需要將您的接收器外的onCreate。類似的東西 -

public class MainActivity extends Activity { 
private static String content; 
private static String phone; 
private String number; 
private String message; 

private BroadcastReceiver receiver = new BroadcastReceiver(){ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //---get the SMS message passed in--- 
     Bundle bundle = intent.getExtras();   
     SmsMessage[] msgs = null; 

     if (bundle != null) 
     { 
      number = ""; 
      message = ""; 
      //---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]);     
       number = msgs[i].getOriginatingAddress();      

       message = msgs[i].getMessageBody(); 


      } 
      //---display the new SMS message--- 
      Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
      SendMe(); 
    } 
    } 


    }; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    IntentFilter filter = new IntentFilter(); 
    filter.addAction(YOUR_SMS_ACTION); 
    this.registerReceiver(this.receiver, filter); 
    setContentView(R.layout.main); 
    } 


public void SendMe(){ 


    PendingIntent pi = PendingIntent.getActivity(this, 0, 
       new Intent(this, MainActivity.class), 0); 
    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(number, null, message, pi, null); 

} 
} 
+0

@Suchi好吧,我認爲這樣做。我需要在我的onCreate中調用任何東西嗎? – theITRanger22

+0

我已經在onCreate中添加了registerReceiver調用。這對我有用。讓我知道它是否成功! – Suchi

+1

@Suchi =(IM仍然得到一個強制關閉後,也許它的東西即時通訊做錯了 – theITRanger22

0

強制關閉可能發生,因爲您是從您的廣播接收器內管理UI。無線電通信局的onReceive在被強制關閉之前有10秒的限制。

要解決,請使用一個Activity組件來生成您的Toast。

0

我在這裏有點困惑。看起來你想爲「SMS_RECEIVED」IntentFilter註冊一個BroadcastReceiver,但是就我所見,代碼中的任何地方都沒有聲明該過濾器。

嘗試將registerReceiver {}末尾的空值替換爲new IntentFilter("SMS_RECEIVED"));以查看它是否有效。也許它是你得到空指針異常的原因。

} } }, null);

} } }, new IntentFilter("SMS_RECEIVED"));

相關問題