java—在指定时间关闭蓝牙的应用程序

brc7rcf0  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(193)

主要活动

public class BluetoothActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener{

   private TextView mTextView;
   private TextView guraText;
   TextView time, time1, time2, time3;

   BluetoothAdapter mBluetoothAdapter;

   final static UUID BT_UUID = UUID.fromString("8CE255C0-200A-11E0-AC64-0800200C9A66");

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

       ImageButton buttonOff = (ImageButton) findViewById(R.id.buttonOff);
       mTextView = (TextView) findViewById(R.id.textView_time);
       guraText = (TextView) findViewById(R.id.dPdir);
       time = (TextView) findViewById(R.id.time);
       time1 = (TextView) findViewById(R.id.time1);
       time2 = (TextView) findViewById(R.id.time2);
       time3 = (TextView) findViewById(R.id.time3);

       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

       buttonOff.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               DialogFragment timePicker = new TimePickerFragment();
               timePicker.show(getSupportFragmentManager(), "time picker");
           }
       });

   }

   @Override
   public void onTimeSet(TimePicker view, final int hourOfDay, final int minute) {

       final Calendar c = Calendar.getInstance();
       c.set(Calendar.HOUR_OF_DAY, hourOfDay);
       c.set(Calendar.MINUTE, minute);

       final String cTime, cMinute;
       cTime = String.valueOf(hourOfDay);
       cMinute = String.valueOf(minute);

//        Calendar calNow = Calendar.getInstance();
//        calNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
//        calNow.set(Calendar.MINUTE, minute);

       class BtOff implements Runnable {
       @Override
       public void run() {
       try {
           // noinspection InfiniteLoopStatement
           while (true) {
               @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("H");
               @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf1 = new SimpleDateFormat("M");

               Calendar cal = Calendar.getInstance();

//                Date date = new Date();
//                String Hour1 = sdf.format(date);
//                String Minute1 = sdf1.format(date);
               String Hour1 = sdf.format(Calendar.getInstance().getTime()); //시
               String Minute1 = sdf1.format(Calendar.getInstance().getTime()); //분
               if(cTime.equals(Hour1) && cMinute.equals(Minute1)) {
                   bluetoothOff();
               }
               time.setText(Hour1);
               time1.setText(Minute1);
               time2.setText(cTime);
               time3.setText(cMinute);
           }
       } catch (Exception e){
           e.printStackTrace();
       }

       }
   }

       updateTimeText(c);
       BtOff btOff = new BtOff();
       Thread tr = new Thread(btOff);
       tr.start();
   }

   private void updateTimeText(Calendar c) {
       String timeText = "set  ";
       timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());
       mTextView.setVisibility(View.VISIBLE);
       mTextView.setText(timeText);
       guraText.setVisibility(View.VISIBLE);
   }

   void bluetoothOff() {
       if(mBluetoothAdapter.isEnabled()) {
           mBluetoothAdapter.disable();
           Toast.makeText(getApplicationContext(), "Bluetooth is disabled.", Toast.LENGTH_SHORT).show();
       }
       else {
           Toast.makeText(getApplicationContext(), "Bluetooth is already disabled.", Toast.LENGTH_SHORT).show();
       }
   }

}

计时器碎片

public class TimePickerFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        return new TimePickerDialog(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
                (TimePickerDialog.OnTimeSetListener) getActivity(), hour, minute,
                DateFormat.is24HourFormat(getActivity()));

    }
}

当它的时间,蓝牙是关闭的应用程序
对不起,我还是个初学者
我英语不成熟
这似乎是线程方面的问题。
在接收时间信息的过程中似乎出现了问题
导入过程被省略
我不知道要补充什么
小时
e

p

e

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题