gpt4 book ai didi

java - 使用android手机闲置时如何设置闹钟?

转载 作者:太空狗 更新时间:2023-10-29 22:57:27 26 4
gpt4 key购买 nike

您好,我想在手机未被触摸时设置闹钟。如果近2分钟没有触摸屏幕,就会发出警报声。我怎样才能做到这一点?有谁能够帮我?提前致谢。

最佳答案

传递 AlarmService通过下面的代码。这会发现您的设备闲置了多长时间。

idle.java

Handler hl_timeout = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

try{
hl_timeout.postDelayed(DoOnTimeOut, 15000);
}catch(Exception e)
{
e.printStackTrace();
}
}

// Toast
Thread DoOnTimeOut = new Thread() {
public void run() {
try{
Toast.makeText(getApplicationContext(), "System is idle", Toast.LENGTH_LONG).show();
}catch(Exception e)
{
e.printStackTrace();
}
}
};

@Override
public void onUserInteraction()
{
super.onUserInteraction();
//Remove any previous callback
try{
hl_timeout.removeCallbacks(DoOnTimeOut);
hl_timeout.postDelayed(DoOnTimeOut, 15000);
}catch(Exception e)
{
e.printStackTrace();
}
}

希望对你有帮助。

关于java - 使用android手机闲置时如何设置闹钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8985013/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com