4

我有一个SmsReceiver班级,我想在主要活动中注册,我应该怎么做?

我是安卓新手。

4

5 回答 5

5

你可以做两件事:

  1. BroadcastReceiver在中创建和定义Manifest
  2. 创建并注册BroadcastReceiverin 代码。

对于选项 2(您要问的):

创建一个BroadcastReceiverin 代码 (MyBroadcastReceiver)。在您的范围内声明 MyBroadcastReceiver Activity

MyBroadcastReceiver mMyBroadcastReceiver;

BroadcastReceiver通过以下方式注册Activity

IntentFilter filter = new IntentFilter(android.provider.Telephony.SMS_RECEIVED);
this.registerReceiver(mMyBroadcastReceiver, filter);
于 2013-01-18T11:01:48.577 回答
2

要让服务接收 SMS 消息,请执行以下步骤:

  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. 在您的服务类中,注册以接收 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);
        }
    }
    

就是这样 !

注意: 如果您想知道为什么我没有从单独的 BroadcastReceiver 类中绑定到我的服务 - 它不起作用,因为bindService()它不可用。

于 2013-01-18T10:54:48.230 回答
1

对于 API 级别 19 及以上

 IntentFilter smsFilter = new IntentFilter();
 smsFilter.addAction(Telephony.Sms.Intents.SMS_RECEIVED_ACTION);
registerReceiver(this.receiver, filter);
于 2017-01-20T15:16:26.650 回答
0

正如你所要求的那样在你的BroadcastReceiver里面注册MainActivityregisterReceiver-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();          
    }
  };
 }
于 2013-01-18T11:06:42.077 回答
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);

希望这可以帮助

于 2016-09-30T05:58:10.793 回答