Reputation: 805
I am trying to make an app to lunch my main activity when wifi turns on. I found I should use a service to perform it. I found this code but does not work.
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.widget.Toast;
public class MyServer extends IntentService {
public Boolean has_launched = false;
public MyServer() {
super("MyServer");
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(MyServer.this, "service started", Toast.LENGTH_SHORT).show();
}
@Override
protected void onHandleIntent(Intent intent) {
wificheck();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return android.app.Service.START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(MyServer.this, "service stoped!", Toast.LENGTH_SHORT).show();
}
public void wificheck() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled() && has_launched == false) {
has_launched = true;
//startActivity(new Intent(MyServer.this, MainActivity.class));
Toast.makeText(MyServer.this, "Wifi is on!", Toast.LENGTH_SHORT).show();
}
else if (wifi.isWifiEnabled() == false) {
has_launched = false;
}
}
}
I have permission to read wifi state and the app lunch normally(without an error) It does not lunch wificheck. what should I do?
Upvotes: 1
Views: 996
Reputation: 5893
As far as I remember, in Google I/O 2014, they introduced the new kind of Wifi State listener in OS level. So it's way more efficient in terms of battery life.
I don't remember which session it was. but either It is in "Key Note" or in "What's new in Android" video. You can check it out on YouTube.
I will update my answer when I come across this api.
EDIT :
I've found it. It's called JobScheduler. I'm not sure if it's all you need but worth to give it a try.
Upvotes: 1
Reputation: 926
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.TextView;
public class AndroidWifiStateChangedDetect extends Activity {
TextView WifiState;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WifiState = (TextView)findViewById(R.id.wifistate);
this.registerReceiver(this.WifiStateChangedReceiver,
new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
}
private BroadcastReceiver WifiStateChangedReceiver
= new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE ,
WifiManager.WIFI_STATE_UNKNOWN);
switch(extraWifiState){
case WifiManager.WIFI_STATE_DISABLED:
WifiState.setText("WIFI STATE DISABLED");
break;
case WifiManager.WIFI_STATE_DISABLING:
WifiState.setText("WIFI STATE DISABLING");
break;
case WifiManager.WIFI_STATE_ENABLED:
WifiState.setText("WIFI STATE ENABLED");
break;
case WifiManager.WIFI_STATE_ENABLING:
WifiState.setText("WIFI STATE ENABLING");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
WifiState.setText("WIFI STATE UNKNOWN");
break;
}
}};
}
To detect Android Wifi ON/OFF (Enable/Disable) state, we can implement a BroadcastReceiver to register with the intent WifiManager.WIFI_STATE_CHANGED_ACTION. IN the BroadcastReceiver, the Wifi state can be retrieved using the code intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)
Upvotes: 1