-1

I am designing an app which needs to turn off and then on with in one/two seconds (at first installation (only once) ). Is there anyway to do this automatically by programming.

Is there any problem to other apps in same mobile if we turn off wifi for 1 sec like is it affects any downloads from other app?

Niranjan
  • 1,879
  • 2
  • 22
  • 28

1 Answers1

1
 try {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   start = (Button) findViewById(R.id.start_wifi);
   stop = (Button) findViewById(R.id.stop_wifi);

   start.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
     WifiManager wifi = (WifiManager) MainActivity.this
       .getSystemService(Context.WIFI_SERVICE);
     if (!wifi.isWifiEnabled()) {
      wifi.setWifiEnabled(true);
      Toast.makeText(MainActivity.this, "Turn ON WIFI",
        Toast.LENGTH_LONG).show();
     }
    }
   });

   stop.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
     WifiManager wifi = (WifiManager) MainActivity.this
       .getSystemService(Context.WIFI_SERVICE);
     if (wifi.isWifiEnabled()) {
      wifi.setWifiEnabled(false);
      Toast.makeText(MainActivity.this, "Turn OFF WIFI",
        Toast.LENGTH_LONG).show();
     }
    }
   });

  } catch (Exception e) {
   Log.v("MainActivity Exception", Log.getStackTraceString(e));
  }
 }

In Manifest

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Jeetendra
  • 134
  • 5