2

我正在尝试从服务中注册一个 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);
    }
}
4

0 回答 0