Android内置的手机计时器应用程序(时钟)在代码中的用法

时间:2018-08-22 13:02:43

标签: android timer

我希望我的应用使用每部手机上预安装的内置计时器应用,例如Google应用,当我说“在x分钟内提醒我”并激活时钟应用计时器时,是否可以使用此功能在我的应用程序中?在网络上找不到任何信息, 谢谢!

1 个答案:

答案 0 :(得分:2)

使用闹钟通过以下方式创建计时器:

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;

@Value("classpath:/file.txt")
public void setFile(Resource myRes) throws IOException {
    try (InputStream is = myRes.getInputStream()) {
        // Content of the file as a String
        String contents = StreamUtils.copyToString(is, StandardCharsets.UTF_8);
    }
}

确保将其添加到清单中:

Intent setTimer = new Intent(AlarmClock.ACTION_SET_TIMER);
setTimer.putExtra(AlarmClock.EXTRA_LENGTH, 60);
setTimer.putExtra(AlarmClock.EXTRA_MESSAGE, "My Amazing timer");
setTimer.putExtra(AlarmClock.EXTRA_SKIP_UI, false);
startActivity(setTimer);