Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 手机在屏幕锁定时振动_Android_Android Studio_Countdowntimer_Vibration_Screen Lock - Fatal编程技术网

Android 手机在屏幕锁定时振动

Android 手机在屏幕锁定时振动,android,android-studio,countdowntimer,vibration,screen-lock,Android,Android Studio,Countdowntimer,Vibration,Screen Lock,我正在Studio中的应用程序中制作计时器,当计时器达到0时,它会振动。我想让手机在屏幕锁定时仍然振动。有什么建议吗 以下是我的主要活动: EditText timerWrite; ImageButton power; ImageButton check; TextView minsLeft; CountDownTimer countDownTimer = null; Vibrator vibe; @Override prot

我正在Studio中的应用程序中制作计时器,当计时器达到0时,它会振动。我想让手机在屏幕锁定时仍然振动。有什么建议吗

以下是我的主要活动:

    EditText timerWrite;
    ImageButton power;
    ImageButton check;
    TextView minsLeft;
    CountDownTimer countDownTimer = null;

    Vibrator vibe;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_screen);
        vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);



    timerWrite = (EditText) findViewById(R.id.timerWrite);
    power = (ImageButton) findViewById(R.id.power);
    check = (ImageButton) findViewById(R.id.check);

    minsLeft = (TextView) findViewById(R.id.minsLeft);


    power.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            vibe.cancel();
            if(countDownTimer != null)
                countDownTimer.cancel();
            minsLeft.setText("stopped");
        }
        });


    check.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            vibe.cancel();
            if (countDownTimer != null)
                countDownTimer.cancel();
            String text = timerWrite.getText().toString();

                if (!text.equalsIgnoreCase("")) {
                    int mins = Integer.valueOf(text);

                    countDownTimer = new CountDownTimer(mins * 60000, 1000) {

                        @Override
                        public void onTick(long milliseconds) {
                            long minutes = milliseconds / 60000;
                            long seconds = (milliseconds / 1000) % 60;
                            minsLeft.setText(getString(R.string.minutes) + (int) (minutes) + "  seconds:" + (int) (seconds));

                        }

                        @Override
                        public void onFinish() {
                            minsLeft.setText("Are You OK?");
                            long[] pattern = {0, 1000, 2000, 100};
                            vibe.vibrate(pattern, 1);
                        }
                    }.start();
                }
            }

    });
}

谢谢

这将超出活动生命周期,因为它将进入暂停和停止状态,因此您的代码/应用程序将无法工作,您正在寻找的是一种运行时不受这些限制的服务


干杯。

这是一个安卓的闹钟服务示例,您可以添加逻辑和震动代码哦,好的。那我就胡闹了。非常感谢!当我引入对象vibe时,我调用可控震源\ u服务:vibe=可控震源getSystemServiceContext.可控震源\ u服务;非常不专业,刚吃过午饭回来。不管怎么说,你不仅缺乏专业精神,还缺乏努力研究,我已经为你指出了正确的方向,你所做的是不对的。干杯。对不起,我不知道我怎么不专业。另外,我做了至少一个小时的研究,但我只是在学习如何编程,所以我很难理解Android API。这是我的第一个应用程序,我去年11月才开始学习Java。以下是我回顾过的页面:,,我为您指出了正确的方向,甚至添加了一个Android提供的示例,尽管您的问题不容易理解或不够清晰,但我花了时间和自由礼貌和坦率。你接受了我的答案,我得到了奖励分数,但我却撤销了它,我受到了惩罚,这真是一团糟:不管怎样,撇开感觉不谈,因为你盯着看,不要急于进入那些链接,还要掌握OOP 1的概念。免费PDF这是一个辉煌的开始2。您所需要的一切都直接来自于源代码