如何显示当前连接的WIFI SSID?

时间:2013-03-16 01:40:20

标签: android eclipse android-wifi connectivity wifimanager

我正在尝试显示当前的SSID(并最终捕获它,以便我可以在以后使用它)

到目前为止,我已经能够编译以下代码 - 但是我无法显示它并且每次运行时应用程序强制关闭。我知道我忽视了一些简单的事情。任何帮助是极大的赞赏!

  • Amani Swann

JAVA

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.preference.PreferenceActivity;

public static String getCurrentSsid(Context context) {
  String ssid = null;
  ConnectivityManager connManager = (ConnectivityManager)      context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo =     connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  if (networkInfo.isConnected()) {
    final WifiManager wifiManager = (WifiManager)     context.getSystemService(Context.WIFI_SERVICE);
    final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) {
      ssid = connectionInfo.getSSID();
    }
  }
  return ssid;
}
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="@drawable/linkingmanagerbackground2"
          android:layout_alignParentTop="true">


<View
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"  
    android:onClick="imageClick"  >
</View>

</RelativeLayout>

logcat的

03-16 01:34:52.938: E/Trace(2236): error opening trace file: No such file or directory (2)
03-16 01:34:53.487: D/dalvikvm(2236): GC_FOR_ALLOC freed 42K, 7% free 2466K/2652K, paused 45ms, total 49ms
03-16 01:34:53.567: I/dalvikvm-heap(2236): Grow heap (frag case) to 6.061MB for 3686416-byte allocation
03-16 01:34:53.627: D/dalvikvm(2236): GC_FOR_ALLOC freed 2K, 4% free 6064K/6256K, paused 55ms, total 55ms
03-16 01:34:53.717: D/dalvikvm(2236): GC_CONCURRENT freed <1K, 4% free 6064K/6256K, paused 9ms+16ms, total 95ms
03-16 01:34:55.048: D/libEGL(2236): loaded /system/lib/egl/libEGL_emulation.so
03-16 01:34:55.128: D/(2236): HostConnection::get() New Host Connection established 0x2a155228, tid 2236
03-16 01:34:55.208: D/libEGL(2236): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-16 01:34:55.218: D/libEGL(2236): loaded /system/lib/egl/libGLESv2_emulation.so
03-16 01:34:55.419: W/EGL_emulation(2236): eglSurfaceAttrib not implemented
03-16 01:34:55.448: D/OpenGLRenderer(2236): Enabling debug mode 0
03-16 01:34:56.568: D/AndroidRuntime(2236): Shutting down VM
03-16 01:34:56.568: W/dalvikvm(2236): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-16 01:34:56.638: E/AndroidRuntime(2236): FATAL EXCEPTION: main
03-16 01:34:56.638: E/AndroidRuntime(2236): java.lang.Error: Unresolved compilation problem: 
03-16 01:34:56.638: E/AndroidRuntime(2236):     Connect cannot be resolved to a type
03-16 01:34:56.638: E/AndroidRuntime(2236):     at     com.nfc.linked.SplashScreen$1.run(SplashScreen.java:34)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at android.os.Handler.handleCallback(Handler.java:725)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at android.os.Looper.loop(Looper.java:137)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at java.lang.reflect.Method.invokeNative(Native Method)    
03-16 01:34:56.638: E/AndroidRuntime(2236):     at java.lang.reflect.Method.invoke(Method.java:511)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 01:34:56.638: E/AndroidRuntime(2236):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

在您的应用程序清单中使用以下权限

< uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
< uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

答案 1 :(得分:2)

您的问题似乎与Wi-Fi SSID无关。在此之前,您可能遇到以下两个问题之一: