本文介绍了如何获得当前连接的无线网络的名称在Android中使用的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我执行的BroadcastReceiver我要运行一些活动,当用户连接到WiFi网络与特定的名称。我该怎么办呢?我怎样才能得到的BroadcastReceiver的WiFi网络名称?我怎样才能发现这是WiFi连接?谢谢!

我已经有一个code:

 公共类ConnectionChangeReceiver扩展的BroadcastReceiver
{
  @覆盖
  公共无效的onReceive(上下文的背景下,意图意图)
  {
    ConnectivityManager connectivityManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    的NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    的NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    如果(activeNetInfo!= NULL)
    {

      Toast.makeText(背景下,活跃网络类型:+ activeNetInfo.getTypeName(),Toast.LENGTH_SHORT).show();
    }
    如果(mobNetInfo!= NULL)
    {
      Toast.makeText(背景下,手机网络类型:+ mobNetInfo.getTypeName(),Toast.LENGTH_SHORT).show();
    }
  }
}
 

解决方案

看看这的。该 SSID 字段是网络名称。您可以使用 getScanResults()以获得最新的扫描结果。

In my implementation of BroadcastReceiver I want to run some Activity when user is connected to WiFi network with specific name. How can I do that? How can I get the Wifi network name in BroadcastReceiver? And how can I find out that this is wifi connection? Thank you!

I already has a code:

public class ConnectionChangeReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive( Context context, Intent intent )
  {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE );
    if ( activeNetInfo != null )
    {

      Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
    if( mobNetInfo != null )
    {
      Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
    }
  }
}
解决方案

Take a look at this ScanResult. The SSID field is the network name. You can use getScanResults() to get the latest scan results.

这篇关于如何获得当前连接的无线网络的名称在Android中使用的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:58