广播接收器从未被调用过

时间:2015-03-13 10:43:14

标签: android broadcastreceiver

我正在尝试通过收听" 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");
            }
        }
    }
};

2 个答案:

答案 0 :(得分:0)

以下是示例:

主要活动

   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>

main.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>

答案 1 :(得分:0)

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

我猜...你试过把日志放在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 + " ] ");

}

我的猜测是你可能试图以错误的方式检索信息,并且对于如此严格定义的日志,你看不到会发生什么,但接收器工作正常