在指定时间启动WiFi

时间:2018-03-26 15:17:25

标签: android

我是Android编程新手。我想创建一个可以在用户指定的时间打开或关闭WiFi的应用程序。我不知道要使用什么概念。任何人都可以帮我解决如何开始和继续吗?

以下代码在我按下按钮后立即启动WiFi。有人可以帮帮我吗?

MainActivity.java: -

公共类MainActivity扩展了AppCompatActivity {

private Button wifiButton, dataButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wifiButton = (Button) findViewById(R.id.wifiEnableButton);
    dataButton = (Button) findViewById(R.id.dataEnableButton);

    wifiButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, WifiTimeActivity.class));
        }
    });
}
}

WifiTimeActivity.java: -

public class WifiTimeActivity extends AppCompatActivity {

private TimePicker timePickerForWifi;
private Button wifiSetTimeButton;
public static int buttonPressed = 0;
public static long milliseconds;
public static int firstRun = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wifi_time);

    timePickerForWifi = (TimePicker) findViewById(R.id.timePickerForWifi);
    wifiSetTimeButton = (Button) findViewById(R.id.wifiTimeSetButton);

    wifiSetTimeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            buttonPressed = 1;
            milliseconds = TimeUnit.SECONDS.toMillis(TimeUnit.HOURS.toSeconds(timePickerForWifi.getCurrentHour()) +
                    TimeUnit.MINUTES.toSeconds(timePickerForWifi.getCurrentMinute()));
            startService(new Intent(WifiTimeActivity.this, WifiService.class));
            Toast.makeText(getApplicationContext(), "Timer Set!", Toast.LENGTH_SHORT).show();
            finish();
        }
    });
}
}

WifiService.java: -

public class WifiService extends Service {
private AlarmManager alarmManager;
private PendingIntent alarmIntent;

public WifiService() {
}

@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onCreate() {
    super.onCreate();
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    String WIFI_ACTION = WifiReceiver.WIFI_START;
    Intent intent = new Intent(WIFI_ACTION);
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (WifiTimeActivity.buttonPressed == 1) {
        int alarmType = AlarmManager.RTC;
        alarmManager.setInexactRepeating(alarmType, WifiTimeActivity.milliseconds, AlarmManager.INTERVAL_DAY,
                alarmIntent);
    }
    else {
        alarmManager.cancel(alarmIntent);
    }

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                if (!wifiManager.isWifiEnabled()) {
                    if (wifiManager.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
                        wifiManager.setWifiEnabled(true);
                    }
                }
            }
        }
    });
    t.start();
    return Service.START_STICKY;
}
}

WifiReceiver: -

public class WifiReceiver extends BroadcastReceiver {

public static final String WIFI_START = "com.sohamchari.mytasker.WIFI_START";

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    Intent startIntent = new Intent(context, WifiService.class);
    context.startService(startIntent);
}
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以学习用于在应用程序生命周期之外触发操作的Alarm Manager类。