【问题标题】:Wifi Broadcast android inside android intentWifi Broadcast android inside android intent
【发布时间】:2014-08-05 01:16:27
【问题描述】:

我正在学习android,我被困在这里:

我正在编写的应用程序会定期在后台扫描 Wifi 信号。我正在使用 android 意图服务。问题是,应用程序永远不会执行 BroadCastReceiver 的 onReceive() 方法

意图代码:

public class BackgroundIntent extends IntentService {

    // Default Constructor
    public BackgroundIntent() {
        super("BackgroundIntent");
        // TODO Auto-generated constructor stub
    }

    WifiManager mainWifi;
    BroadcastReceiver receiverWifi;

    private final Handler handler = new Handler();

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub

        // Get mainWifi
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        if (mainWifi.isWifiEnabled() == false) {
            mainWifi.setWifiEnabled(true);
        }

        receiverWifi = new WifiReceiver();
        doInback();

    }

    // Basically a thread which calls itself after 5000milli sec
    public void doInback() {
        handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                mainWifi.startScan();
                Log.i("Inside ", "doInBack");

                // Call itself CODE GOES HERE :D
                doInback();
            }
        }, 5000);

    }

    public class WifiReceiver extends BroadcastReceiver {

        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        public void onReceive(Context c, Intent intent) {

            // CODE NEVER GOES HERE :(

            List<ScanResult> wifiList;
            wifiList = mainWifi.getScanResults();

            Log.i("Inside receiver", "yes");

        }
    }
}

调用android意图服务的MainActivity代码

public class MainActivity extends Activity {
    TextView texty;

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

        // Intent is called here
        Intent myIntent = new Intent(MainActivity.this, BackgroundIntent.class);
        startService(myIntent);

        texty = (TextView) findViewById(R.id.textView1);

    }

    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        return super.onMenuItemSelected(featureId, item);
    }
}

知道可能是什么原因吗?这是实现后台wifi信号扫描的错误方法吗? 当我在 Main Activity 中实现时运行良好,所以我猜 AndroidManifest.xml 是正确编写的..

【问题讨论】:

    标签: java android android-intent android-wifi


    【解决方案1】:

    编辑:我可能在这里误解了你的目标,如果你只是想知道为什么你的 onReceive() 没有触发,你的活动必须注册接收器。类似于以下内容:

    receiverWifi = new WifiReceiver();
    registerReceiver(receiverWifi, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    doInback();
    

    Check out this link

    【讨论】:

    • 对不起,我不明白,我已经在使用广播接收器了 Wifireceiver
    • 试过了,还是没有结果:(进入doInback()但调用失败onReceive()
    • 这是你能做的最好的事情,onReceive() 不会立即执行,因为它可能仍在扫描。扫描可能需要 1 秒到 1 小时,你能做的最好的就是设置一个接收器并交叉手指:)
    • 感谢您的及时回复!这是因为在 Android Intent 中使用它而导致的扫描延迟吗?它在 Activity 中工作得非常好。如果是这种情况,我会将其标记为已解决
    • 也试过了,没有区别。我还在日志中检查过,doInback() 函数按预期在 5 秒后调用。这意味着 wifi 扫描没有延迟。在解决它之前,我将把这个问题再开放一天。谢谢。
    【解决方案2】:

    所以问题出在我使用的函数上。

    我使用了 onHandleIntent(intent) 这是错误的功能。右边是onStartCommand(intent, flags, startId) for service。

    对不起..

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多