我试图在一分钟后没有连接到任何网络时禁用Wifi。但是遇到了一些问题。
当我运行应用程序。它开始工作并尝试禁用无法连接到任何网络的wifi。这意味着我无法阻止它。我有CheckBoxPreference
的偏好活动。它是打开/关闭此功能的设置。我想只在用户选中复选框时才能工作。如果取消选中,请停止工作。但是现在,它随时都在失控。 CheckBox不适用于打开/关闭功能。
这是我的代码。 mycode有什么问题?请帮帮我。
MyActivity.java
public class MyActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.prefes);
Preference preference = (Preference) findPreference(Strings.SETTINGS_WIFI_TIMEOUT);
preference
.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object object) {
if (Boolean.TRUE.equals(object)) {
Intent intent = new Intent(getApplicationContext(),
MyService.class);
startService(intent);
} else {
stopService(new Intent(getApplicationContext(),
MyService.class));
Toast.makeText(getApplicationContext(),
"Stop service", Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
}
MyService.java
public class MyService extends Service {
int mStartMode; // indicates how to behave if the service is killed
IBinder mBinder; // interface for clients that bind
boolean mAllowRebind; // indicates whether onRebind should be used
@Override
public IBinder onBind(Intent arg0) {
// A client is binding to the service with bindService()
return mBinder;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// The service is starting, due to a call to startService()
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
ConnectivityManager cm = (ConnectivityManager) MyService.this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
if (isConnected) {
} else {
wifiManager.setWifiEnabled(false);
}
return Service.START_STICKY;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}
WifiWatcher.java
public class WiFiWatcher extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
if (isConnected) {
} else {
wifiManager.setWifiEnabled(false);
}
}
}
的Manifest.xml
<receiver android:name="com.example.example.service.WiFiWatcher" android:enabled="true">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
答案 0 :(得分:0)
我认为你也应该输入这两个权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>