我想知道是否可以激活BroadcastRecievers onRecieve方法,在按下按钮后仅在限定时间内接收短信?
我不希望它一直处于活动状态,并与我的应用程序冲突。
我是一个制作类“公共类SmsReciever扩展BroadcastReceiver”并在清单中定义了接收器:
<receiver android:name="SmsReciever" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
答案 0 :(得分:2)
如果要临时激活BroadcastReceiver,则不应在清单中定义接收器。您应该在代码中register receiver:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED"); // I don't know where in the Android SDK this string defined
BroadcastReceiver receiver = new SmsReciever();
registerReceiver(receiver, intentFilter);
和unregister receiver当不需要时:
unregisterReceiver(receiver);