调试器未达到“BroadcastReceiver.onReceive()”

时间:2012-10-11 15:41:11

标签: java android broadcastreceiver

我正在尝试从服务中注册BroadcastReceiver以在后台检查WIFI_STATE_CHANGED_ACTION。到目前为止一切都那么好 - 没有编译错误..但是!

无论我做了什么或尝试过什么,我都没有达到我的onReceive()代码。 我在我的硬件设备上测试,打开和关闭wifi,但没有任何反应。我也在我的清单中注册了服务/接收器,做了我从演示,教程和书籍中找到的所有内容^^ (我正在使用该服务只是为了让BroadcastReceiver在后台运行并在必要时采取行动......)

所以这是我的代码:

“我的活动课程”

public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(this, IntelliRingService.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

“我的服务类”

public class IntelliRingService extends Service {


    public BroadcastReceiver myWifiBroadcastReceiver = new BroadcastReceiver() {


        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            final String action = intent.getAction();

            if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION))
            {
                // DO SOMETHING
            }
        }
    };

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        IntentFilter theFilter;
        theFilter = new IntentFilter();
        theFilter.addAction(WIFI_SERVICE);

        getApplicationContext().registerReceiver(myWifiBroadcastReceiver, theFilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

         return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        getApplicationContext().unregisterReceiver(myWifiBroadcastReceiver);
    }
}

0 个答案:

没有答案