broadCast Receiver 永远不会被调用



我正在尝试通过收听代码中如下所示的"SUPPLICANT_CONNECTION_CHANGE_ACTION"来检测WiFi是否已连接。但问题是当我运行该应用程序时,我没有收到来自我注册的 broadCast 接收器的通知!!

为什么会发生这种情况以及如何解决?

代码

IntentFilter intentFilter2 = new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ConnectivityModule();
}
protected void ConnectivityModule() {
    // TODO Auto-generated method stub
     Log.d(TAG, "@interNetConnectivityModule: called");
    registerReceiver(SupplicantReceiver, intentFilter2);
}
BroadcastReceiver SupplicantReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        final String action = intent.getAction();
        if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
            SupplicantState supplicantState = (SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
            if (supplicantState == (SupplicantState.COMPLETED)) { 
                Log.d(TAG, "@SupplicantReceiver: connected");
            }
            if (supplicantState == (SupplicantState.DISCONNECTED)) {
                Log.d(TAG, "@SupplicantReceiver: not connected");
            }
        }
    }
};

下面是示例:

主要活动

   import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
public class MainActivity extends Activity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
   // broadcast a custom intent. 
   public void broadcastIntent(View view)
   {
      Intent intent = new Intent();
      intent.setAction("com.tutorialspoint.CUSTOM_INTENT");
      sendBroadcast(intent);
   }
}

我的广播接收器 :

 import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
      Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
   }
}

这是您应该如何在清单中声明广播接收器:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >
   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="15" />
   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >
       <activity
           android:name=".MainActivity"
           android:label="@string/title_activity_main" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
       <receiver android:name="MyReceiver">
          <intent-filter>
          <action android:name="com.tutorialspoint.CUSTOM_INTENT">
          </action>
          </intent-filter>
      </receiver>
   </application>
</manifest>

主.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   <Button android:id="@+id/btnStartService"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/broadcast_intent"
   android:onClick="broadcastIntent"/>
</LinearLayout>

您的接收器看起来已正确注册(在运行时,不是基于清单,但无论如何,无论如何都应该很好)。

我猜..你有没有尝试过onReceive方法放置日志,比如

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.d(TAG, "Reached this point, receiver is working");
    final String action = intent.getAction();
    if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
        SupplicantState supplicantState = (SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
        if (supplicantState == (SupplicantState.COMPLETED)) { 
            Log.d(TAG, "@SupplicantReceiver: connected");
        }
        if (supplicantState == (SupplicantState.DISCONNECTED)) {
            Log.d(TAG, "@SupplicantReceiver: not connected");
        }
        Log.d(TAG, "Checking for an error in retrieving data!");
        boolean myTest = intent.getBooleanExtra(EXTRA_SUPPLICANT_CONNECTED, false);
        if (myTest) Log.d(TAG, "This way it worked!"); 
          else Log.d(TAG, "This does not mean it didn't work at all! Might just be the correct value as a DISCONNECTED status");
    } else Log.d(TAG, "Bizzarre error, action received was different from the one receiver was registered to! [ " + action + " ] ");
}

我的猜测是,您可能试图以错误的方式检索信息,并且日志定义如此严格,您无法看到足够的情况,但是接收器工作正常

最新更新