问题
I'm trying to disable Wifi when not connect to any network after a min. But getting some problems.
When i run the app. It start working and try to disable wifi that's not connect to any network. It's mean I can't stop it. I have a Preference Activity with CheckBoxPreference
. It's the setting to turn on/off this feature. I want to only work when user checked the checkbox. If uncheck, stop working. But right now, It's working anytime out of control. CheckBox is doesn't work for turn on/off feature.
Here is my code. What is wrong with mycode? Please help me.
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" />
回答1:
I think you should be entering both these permissions also
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
来源:https://stackoverflow.com/questions/29677601/android-checkboxpreference-doesnt-work-with-broadcast-reciever