Android:在有限的时间内激活短信接收器

时间:2012-05-17 12:41:08

标签: android broadcastreceiver

我想知道是否可以激活BroadcastRecievers onRecieve方法,在按下按钮后仅在限定时间内接收短信?

我不希望它一直处于活动状态,并与我的应用程序冲突。

我是一个制作类“公共类SmsReciever扩展BroadcastReceiver”并在清单中定义了接收器:

    <receiver android:name="SmsReciever" >
        <intent-filter  >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

1 个答案:

答案 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);