2015-03-13 42 views
1

我试图通过侦听代码中下面显示的“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"); 
      } 
     } 
    } 
}; 
+1

在清单中设置的所有权限? – Opiatefuchs 2015-03-13 10:45:49

+0

是设置了所有权限,并且在运行时,我在logcat中没有收到任何错误 – rmaik 2015-03-13 10:52:01

+0

您是否还在清单中注册了接收器? – 2015-03-13 11:18:57

回答

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> 
0

您的接收机寻找正确登记(在运行时,不是基于清单,但不管,应该不错无论如何)。

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

} 

我的猜测是,你可能会试图检索以错误的方式的信息,并与日志,以便严格定义你不能看到足够的发生,但接收器工作正常

+0

谢谢,我试过你的答案,但仍然同样的事情,当我打开和关闭无线网络时,我收不到通知 – rmaik 2015-03-13 11:48:23

+0

我试着用给出的代码和我的增强日志创建一个新的Android项目。 在清单中没有设置权限。 结果是接收方工作,但最初使用的日志(SupplicantState.COMPLETED和DISCONNECTED)永远不会启动,因为supplicantState始终为空。相反,我的每个日志都正在正确地启动 – FrancescoC 2015-03-13 12:06:11

相关问题