WifiManager.setWifiEnabled 在蜂窝上无法正常工作
我正忙着编写一个小程序来自动化我的 3.1 蜂窝选项卡和 2.3.5 姜饼手机上的 wifi。
手机上一切正常,但是当我的应用程序尝试禁用蜂窝上的 wifi 时,它就崩溃了。
我的应用程序基本上设置并发出警报来触发自定义广播,然后由我的广播接收器捕获。我可以确认广播已被触发并且很咳嗽,但是 honeycomb 在运行 .setWiFiEnabled(false) 时似乎会抛出运行时异常。我已经检查了权限,它们看起来是正确的。 (它以 GB 工作)。
这是我的一些代码,以尝试更好地解释。
清单
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<receiver android:name="WiFiOffDoerReceiver" android:enabled="true">
<intent-filter>
<action android:name="za.co.cjoliver.WIFIOFFDOER"/>
</intent-filter>
</receiver>
代码
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
public class WiFiOffDoerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("WiFi Beater3", "in WiFiOffDoerReceiver.onReceive");
WifiManager myWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
myWifiManager.setWifiEnabled(false);
}
}
让我对您的帮助表示感谢。
I'm busy writing a little program to automate my wifi on my 3.1 honeycomb tab and my 2.3.5 gingerbread phone.
Everything works great on the phone, but when my app tries to disable my wifi on honeycomb it fc's.
My app basically sets and alarm to fire a custom broadcast which is then catched by my broadcast receiver. I can confirm that the broadcast is fired and is cough, but it would seem that honeycomb throws an runtime exception when it runs .setWiFiEnabled(false). I have check the permissions and they look correct. (It is working in gb).
Here is some of my code to try and explain better.
Manifest
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<receiver android:name="WiFiOffDoerReceiver" android:enabled="true">
<intent-filter>
<action android:name="za.co.cjoliver.WIFIOFFDOER"/>
</intent-filter>
</receiver>
Code
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.util.Log;
public class WiFiOffDoerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("WiFi Beater3", "in WiFiOffDoerReceiver.onReceive");
WifiManager myWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
myWifiManager.setWifiEnabled(false);
}
}
Let me say thank you already for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你的问题是什么,但听起来它与你的 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!