Android 3.1 的 wifi api 问题

发布于 2024-12-29 17:44:58 字数 1596 浏览 2 评论 0原文

我想在我的 wifi 打开时开始我的活动,以便在 WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION 时调用我的活动。它可以在 2.3(Gingerbread) samsung tap 上正常工作,但相同的程序在 3.1(Honeycomb)samsung tap 中无法工作。指南为什么会发生此类问题,这是我的漏洞代码:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
 * A BroadcastReceiver that listens for updates for the
 * ExampleAppWidgetProvider. This BroadcastReceiver starts off disabled, and we
 * only enable it when there is a widget instance created, in order to only
 * receive notifications when we need them.
 */
public class WIFIBroadcastReceiver extends BroadcastReceiver {

    public static String packageName = "com.example.wifi";

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
            if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED,
                    false)) {
                Log.d(packageName, "WIFI Connected");
                if (context != null) {

                    Intent ssIntent = new Intent(context,
                            com.example.wifi.Activity.class);
                    ssIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(ssIntent);
                }
            } else {
                Log.d(packageName, "WIFI Connection was Lost");
            }
        }
    }
}

I want to start my activity when my wifi will be switch on so that I call my activity when WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION. It will be working fine with 2.3(Gingerbread) samsung tap but same program won't work in 3.1(Honeycomb)samsung tap.guide why such type of issue will be happen here is my hole code:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
 * A BroadcastReceiver that listens for updates for the
 * ExampleAppWidgetProvider. This BroadcastReceiver starts off disabled, and we
 * only enable it when there is a widget instance created, in order to only
 * receive notifications when we need them.
 */
public class WIFIBroadcastReceiver extends BroadcastReceiver {

    public static String packageName = "com.example.wifi";

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
            if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED,
                    false)) {
                Log.d(packageName, "WIFI Connected");
                if (context != null) {

                    Intent ssIntent = new Intent(context,
                            com.example.wifi.Activity.class);
                    ssIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(ssIntent);
                }
            } else {
                Log.d(packageName, "WIFI Connection was Lost");
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2025-01-05 17:44:58

我不确定你的问题是什么,但听起来它与你的 ROM 有关,是否已 root 等。

另一种可能性是你正在使用的 API。 HC 是否需要与 GB 不同的版本?

我不认为这是你的代码,所以如果你无法弄清楚,请尝试提供有关标签和电话之间差异的信息(除了明显的差异之外)。

只是一个预感,所以如果偏离基地,我很抱歉。

祝你好运!

I'mot sure exactly what you issue is but it sounds like it has something to do with your ROM, if it's rooted or not, etc.

Another possibility is the API you're using. Does HC require a different version than GB?

I don't think it's your code, so if you can't figure it out, try providing info about the difference between the tab and phone (besides the obvious).

Just a hunch, so I'm sorry if it's way off base.

Good luck!

债姬 2025-01-05 17:44:58

不确定您面临的问题。谦虚地建议尝试替代方案:

您可以注册 WIFI_STATE_CHANGED_ACTION 意图,然后在 onReceive() 中调用 getConnectionInfo()。 NetworkInfo.DetailedState 类应该提供(更详细的)有关 IP 地址是否可用的信息,然后您的活动就可以开始了。

Not sure of the issue you are facing. Would humbly suggest trying an alternative:

You could register for WIFI_STATE_CHANGED_ACTION intent and then in onReceive(), call getConnectionInfo(). The NetworkInfo.DetailedState class should provide (a more detailed) information on the whether IP address is available following which your activity could be started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文