2013-01-18 64 views
2

我有一個SmsReceiver類,我想在主要活動中註冊,我該做什麼?如何在主要活動中註冊接收者?

我是Android新手。

+0

你可以看看這個帖子的http://計算器。com/questions/4660823/android-alarm-not-working/4661122#4661122 – ccheneson

+0

關於此主題有很多答案。檢查[this](http://stackoverflow.com/a/4805733/1521064),[this](http://stackoverflow.com/questions/4134203/how-to-use-registerreceiver-method)和[this] (http://stackoverflow.com/questions/6508300/how-to-register-broadcast-receiver)。 – vault

+0

[以編程方式註冊廣播接收器]的可能的重複(http://stackoverflow.com/questions/4805269/programmatically-register-a-broadcast-receiver) – glomad

回答

3

要麼你可以做兩件事情:

  1. 的創建和Manifest
  2. 定義BroadcastReceiver創建並在代碼註冊BroadcastReceiver

選項2(你問):

創建代碼BroadcastReceiver(MyBroadcastReceiver)。 申報MyBroadcastReceiver在Activity範圍:

MyBroadcastReceiver mMyBroadcastReceiver; 

由註冊BroadcastReceiverActivity

IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED); 
this.registerReceiver(mMyBroadcastReceiver, filter); 
+0

這說「無法解析SMS_RECEIVED」 –

2

爲了有一個服務接收短信遵循下面的步驟:

  1. 更新清單給你的應用程序的權限,以接收短信 (WRITE_SMS,READ_SMS,RECEIVE_SMS)
  2. 沒有更新清單與接收機意向過濾器! (這 每個樣本代碼在網上似乎做)
  3. 在你的服務,你的服務類

    private class SMSreceiver extends BroadcastReceiver 
    { 
        private final String TAG = this.getClass().getSimpleName(); 
    
        @Override 
        public void onReceive(Context context, Intent intent) 
        { 
         Bundle extras = intent.getExtras(); 
    
         String strMessage = ""; 
    
         if (extras != null) 
         { 
          Object[] smsextras = (Object[]) extras.get("pdus"); 
    
          for (int i = 0; i < smsextras.length; i++) 
          { 
           SmsMessage smsmsg = SmsMessage.createFromPdu((byte[])smsextras[i]); 
    
           String strMsgBody = smsmsg.getMessageBody().toString(); 
           String strMsgSrc = smsmsg.getOriginatingAddress(); 
    
           strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;      
    
           Log.i(TAG, strMessage); 
          } 
    
         } 
    
        } 
    
    } 
    
  4. 內創建一個嵌套的BroadcastReceiver類服務類,登記接收 android.provider.Telephony.SMS_RECEIVED意圖過濾器:

    public class ServiceCommunicator extends Service 
    { 
        private SMSreceiver mSMSreceiver; 
        private IntentFilter mIntentFilter; 
    
        @Override 
        public void onCreate() 
        { 
         super.onCreate(); 
    
         //SMS event receiver 
         mSMSreceiver = new SMSreceiver(); 
         mIntentFilter = new IntentFilter(); 
         mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); 
         registerReceiver(mSMSreceiver, mIntentFilter); 
        } 
    
        @Override 
        public void onDestroy() 
        { 
         super.onDestroy(); 
    
         // Unregister the SMS receiver 
         unregisterReceiver(mSMSreceiver); 
        } 
    } 
    

這就是它!

注: 包住你想知道爲什麼我沒有從單獨的廣播接收器類中綁定到我的服務 - 這是行不通的,因爲bindService()不可用。

0

按照您的要求進行註冊BroadcastReceiverMainActivity適合於registerReceiver-codewise-inside-MainActivity

代碼如下==>

public class MainActivity extends Activity 
{ 
BroadcastReceiver smsReceiver; 
private IntentFilter myFilter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED); 

    protected void onCreate(Bundle savedInstanceState) 
    { 
    super.onCreate(savedInstanceState); 
    registerReceiver(smsReceiver, myFilter); // register BroadCastReceiver at time of initialization of Activity with proper Intent-Filter 
    } 

@Override 
    protected void onPause() 
    { 
    unregisterReceiver(smsReceiver); // UnRegister BroadCastReceiver as you no longer have your activity at Foreground -- Saving CPU & Battery Drainage 
    super.onPause(); 
    } 

@Override 
    protected void onResume() 
    { 
    registerReceiver(smsReceiver, myFilter); // Register BroadCastReceiver Once again as your activity comes from pause to forground state again. 
    super.onResume(); 
    } 

    smsReceiver = new BroadcastReceiver() //Implementation of your BroadCastReceiver 
    { 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     // Do whatever you like as sms is received and caught by these BroadCastReceiver 
     Toast.makeText(context, "SMS Received", Toast.LENGTH_LONG).show();   
    } 
    }; 
} 
0
private BroadcastReceiver receiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //some action 
    } 
}; 

IntentFilter filter = new IntentFilter("YOUR_ACTION"); 
registerReceiver(receiver, filter); 

希望這有助於

1

對於API級別爲19及以上

IntentFilter smsFilter = new IntentFilter(); 
smsFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION); 
registerReceiver(this.receiver, filter);