用service的onCreate/onDestroy来控制广播的监听(电话,sms)。
先写一个这样的框架,具体功能不实现:
/**
* 用TelSmsBlackService来控制广播监听(电话,sms)。
*
* @author lmk
*
*/
public class TelSmsBlackService extends Service {
private SmsBroadcastReceiver smsBroadcastReceiver;
private BlackDao blackDao;
private TelephonyManager tm;
private PhoneStateListener listener;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// 短信广播
private class SmsBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 短信广播监听要干的事情
//__①__
}
}
@Override
public void onCreate() {
blackDao = new BlackDao(getApplicationContext());
// 创建短信的广播监听对象
smsBroadcastReceiver = new SmsBroadcastReceiver();
// 短信广播意图
IntentFilter filter = new IntentFilter(
"android.provider.Telephony.SMS_RECEIVED");
// 设置最高级别
filter.setPriority(Integer.MAX_VALUE);
registerReceiver(smsBroadcastReceiver, filter);
// 开启电话的监听
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
listener = new PhoneStateListener() {