我有下面的類,用於連接監聽廣播接收機安卓:不工作
public class connectionListener{
public ConnectivityManager conMgr;
public htmlParserListener vystrahyListener;
public htmlParserListener podmienkyListener;
private ConnectivityBroadcastReceiver mReceiver;
public connectionListener(ConnectivityManager conMgr, htmlParserListener vystrahyListener, htmlParserListener podmienkyListener){
this.conMgr = conMgr;
this.vystrahyListener = vystrahyListener;
this.podmienkyListener = podmienkyListener;
}
private class ConnectivityBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("connected");
}
}
public synchronized void startListening(Context context) {
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(mReceiver, filter);
System.out.println("start");
}
}
我用這個代碼(由MainActivity)實例吧:
connectionListener conLst = new connectionListener(conMgr, vystrahyListener, podmienkyListener);
conLst.startListening(this);
但是我得到的「開始」,在輸出,所以startListening方法起作用,但當我禁用或啓用移動數據時,我不會「連接」。所以它看起來像我的BroadcastReceiver不工作。我究竟做錯了什麼?
感謝在前進
你有沒有在manifest中使用 權限。 –
Chitrang
2014-11-02 11:31:43
是的,我已經把它放在那裏 – horin 2014-11-02 11:45:09
張貼你的清單或檢查你的清單接收者的意圖 – 2014-11-02 12:16:51