【问题标题】:broadCast Receiver is never called广播接收器永远不会被调用
【发布时间】:2015-03-13 10:43:13
【问题描述】:

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

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

代码

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");
            }
        }
    }
};

【问题讨论】:

  • 清单中的所有权限集?
  • 是的所有权限集,并且在运行时我没有在 logcat 中收到任何错误
  • 您也已经在清单中注册了您的接收器吗?
  • @umeshlohani 不,我没有在清单中注册接收器,应该吗?该怎么做?
  • 在下面的链接中关注dong221的回答stackoverflow.com/questions/9425187/…

标签: android broadcastreceiver


【解决方案1】:

这里是例子:

主要活动

   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>

【讨论】:

    【解决方案2】:

    您的接收器看起来注册正确(在运行时,不是基于 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 + " ] ");
    
    }
    

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

    【讨论】:

    • 谢谢,我试过你的答案,但还是一样,当我打开和关闭 wifi 时,我没有收到任何通知
    • 我尝试使用给定的代码和我的增强日志创建一个新的 Android 项目。清单中没有设置权限。结果是接收器工作,但您最初使用的日志(SupplicantState.COMPLETED 和 DISCONNECTED)永远不会触发,因为 supplicantState 始终为空。相反,我的每个日志都被正确触发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2013-10-12
    • 2012-03-27
    • 2013-08-29
    相关资源
    最近更新 更多