实施内部服务广播接收器 [英] Implement Broadcast receiver inside Service

查看:155
本文介绍了实施内部服务广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要检查,通过了我的Andr​​oid应用程序的运行时间的互联网连接。我尝试使用服务,但似乎这是不是最好的选择。有没有为我服务来实现与广播接收任何可能的方式?还是我不得不放弃该服务,并使用单独广播接收器?建议,欢迎

I want to check internet connection through out the run time of my android application. I tried using services but seems like it is not the best option. is there any possible way for me to implement a broadcast receiver with in a service? or do i have to give up the service and use broadcast receivers alone? suggestions are welcome

感谢

推荐答案

我现在演示如何在服务中创建的短信接收器:

I now show how to create SMS receiver in a service:

public class MyService extends Service {

@Override
public void onCreate() {
    BwlLog.begin(TAG);
    super.onCreate();

    SMSreceiver mSmsReceiver = new SMSreceiver();
    IntentFilter filter = new IntentFilter();
    filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
    filter.addAction(SMS_RECEIVE_ACTION); // SMS
    filter.addAction(WAP_PUSH_RECEIVED_ACTION); // MMS
    this.registerReceiver(mSmsReceiver, filter);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

   /**
 * This class used to monitor SMS
 */
class SMSreceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (TextUtils.equals(intent.getAction(), SMS_RECEIVE_ACTION)) {
             //handle sms receive
        }
    }
}

这篇关于实施内部服务广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆