【发布时间】:2011-11-18 08:05:17
【问题描述】:
我的应用程序使用广播接收器以这种方式获取短信:
SmsBR.java
public class SmsBR extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
if (messages.length > 0)
//doSomething();
}
}
}
}
Manifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsBR">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
这样,SmsBR 始终处于开启状态。我想在服务启动时注册它(onCreate())并在服务被销毁时取消注册(onDestroy())。我该怎么做?
【问题讨论】: