0

当我决定时间时如何激活wifi?这是代码,但现在我创建了时间选择器,打开它的 wifi 选项在选择器内部,所以不会去。我希望如果我决定在时间选择器中选择例如凌晨 3 点在凌晨 3 点打开 wifi。

package com.pkg.androidmemoryinfo;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

    public class MainActivity extends Activity implements OnClickListener {

        private Button mTimeButton;

        private Calendar mCalen;
        private int hourOfDay;
        private int minute;
        private int ampm;

        private static final int Time_PICKER_ID = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTimeButton = (Button) findViewById(R.id.time_button);
            mCalen = Calendar.getInstance();
            hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
            minute = mCalen.get(Calendar.MINUTE);
            ampm = mCalen.get(Calendar.AM_PM);
            mTimeButton.setOnClickListener(this);
         // Creating a memory_info  Object
            MemoryInfo memory_info = new MemoryInfo();      
            // Using Activity Manager System Service
            ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
            activityManager.getMemoryInfo(memory_info);     
            // Assign Memory Value to free_memory Variable
            long free_memory = memory_info.availMem / 1048576L;     
             // Display free Ram Memory
             TextView memoryInfoView = (TextView) findViewById(R.id.device_memory_status_txt);
             memoryInfoView.setText("Free RAM Memory :-"+ free_memory + " MB");    


        }
        public void run() {
            WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            String ssid = info.getSSID();

            TextView ssidTextView = (TextView) findViewById(R.id.wifiSSID);
            ssidTextView.setText(ssid);
        }
        @Override
        @Deprecated
        protected Dialog onCreateDialog(int id) {

            switch (id) {
                case Time_PICKER_ID:
                    return new TimePickerDialog(this, TimePickerListener,
                            hourOfDay, minute, false);
            }
            return null;
        }

        private TimePickerDialog.OnTimeSetListener TimePickerListener =
                new TimePickerDialog.OnTimeSetListener() {

                    // while dialog box is closed, below method is called.
                    public void onTimeSet(TimePicker view, int hour, int minute) {

                        mCalen.set(Calendar.HOUR_OF_DAY, hour);
                        mCalen.set(Calendar.MINUTE, minute);

                        int hour12format = mCalen.get(Calendar.HOUR);
                        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                        minute = mCalen.get(Calendar.MINUTE);
                        ampm = mCalen.get(Calendar.AM_PM);
                        String ampmStr = (ampm == 0) ? "AM" : "PM";
                        // Set the Time String in Button

                        TextView dateInfoView = (TextView) findViewById(R.id.dateinfo);
                        dateInfoView.setText("L'attivazione del Wi-Fi è impostata per le ore: " + hour12format + " : " + minute + " / " + ampmStr);    

                        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
                        wifiManager.setWifiEnabled(true);


                    }
                };

        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View v) {
            showDialog(Time_PICKER_ID);
        }
    }

谢谢你。

4

0 回答 0