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?