Android计时器/小部件

时间:2019-01-22 21:28:20

标签: java android

对不起,我英语不好 需要一个可以在所有窗口上运行的计时器。只需轻按一下,即可开始/暂停,双击-重置。 计时器本身没有问题。在所有窗口之上工作-我正在使用WindowManager。

但是我有一个问题。我开始活动,其中所有窗口上方都设有“重置”按钮和计时器。但是我不知道如何使它们工作。

另外,执行计时器功能的类可以正常工作,但是我不知道如何将其附加到所有窗口顶部的活动上。

这就是我在所有窗户上所做的事情 FloatButtonService.java

public class FloatButtonService extends Service {

@Override
public IBinder onBind(Intent intent) {
    return null;
}
private WindowManager windowManager;
private View rootView;

@Override
public void onCreate() {
    super.onCreate();

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    int layout_parms;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        layout_parms = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    }
    else {
        layout_parms = WindowManager.LayoutParams.TYPE_PHONE;
    }

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            layout_parms,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT );

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;

    rootView = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_timer, null);

    windowManager.addView(rootView, params);

    Button mButtonStartPause = rootView.findViewById(R.id.button_start_pause);
    mButtonStartPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(FloatButtonService.this, timer.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        }
    });

    rootView.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;
        private boolean shouldClick;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    shouldClick = true;
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_UP:
                    if(shouldClick)
                        return true;
                case MotionEvent.ACTION_MOVE:
                    shouldClick = false;
                    params.x = initialX
                            + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY
                            + (int) (event.getRawY() - initialTouchY);
                    windowManager.updateViewLayout(rootView, params);
                    return true;
            }
            return false;
        }
    });

}

@Override
public void onDestroy() {
    super.onDestroy();
    if (rootView != null)
        windowManager.removeView(rootView);
}


}

timer.java

public class timer extends MainActivity  {

private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis
private long lastPressTime;

private static final long START_TIME_IN_MILLIS = 600000;

private TextView mTextViewCountDoun;
private static Button mButtonStartPause;


private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMilles = START_TIME_IN_MILLIS;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);

    mTextViewCountDoun = findViewById(R.id.text_view_countdown);

    mButtonStartPause = findViewById(R.id.button_start_pause);

    mButtonStartPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            long pressTime = System.currentTimeMillis();

            if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
                resetTimer();
            }
            else if (mTimerRunning ) {
                pauseTimer();
            }
            else {
                startTimer();
            }
            lastPressTime = pressTime;
        }
    });

    updateCountDownText();

}
private void startTimer(){
    mCountDownTimer = new CountDownTimer(mTimeLeftInMilles, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeLeftInMilles = millisUntilFinished;
            updateCountDownText();
        }

        @Override
        public void onFinish() {
            mTimerRunning = false;
            mButtonStartPause.setText("RESET");
        }
    }.start();

    mTimerRunning = true;
       mButtonStartPause.setText("PAUSE");
}

private void pauseTimer(){
    mCountDownTimer.cancel();
    mTimerRunning = false;
      mButtonStartPause.setText("START");
}

private void resetTimer(){
    mTimeLeftInMilles = START_TIME_IN_MILLIS;
    pauseTimer();
    updateCountDownText();
       mButtonStartPause.setText("RESET");
}

private void updateCountDownText() {
    int minutes = (int) (mTimeLeftInMilles / 1000) / 60;
    int seconds = (int) (mTimeLeftInMilles / 1000) % 60;

    String timeLeftFormated = String.format(Locale.getDefault(),"%02d:%02d", minutes,seconds);
    mTextViewCountDoun.setText(timeLeftFormated);
}
}

acrivity_timer

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="180dp"
    android:layout_height="300dp"
    android:id="@+id/testTest"
    tools:context=".timer"
    tools:showIn="@layout/activity_timer"
    >

    <TextView
        android:id="@+id/text_view_countdown"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:text="00:00"
        android:textColor="@android:color/black"
        android:textSize="25dp"
        />

    <Button
        android:id="@+id/button_start_pause"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@id/text_view_countdown"
        android:layout_centerHorizontal="true"
        android:text="START"
        android:textSize="10dp"/>

    </RelativeLayout>

谢谢

0 个答案:

没有答案