我正在使用Pomodoro Technique创建计时器。第一个计时器将是20分钟,在这段时间我希望屏幕关闭。我希望屏幕在计时器结束时重新打开。
在我的清单中,我要求以下权限:
import React, {Component} from 'react'
export default class Expenses extends Component {
constructor(props) {
super(props)
this.state = {
expenseItems: []
}
}
showItemDetails(idx) {
const {expenseItems} = this.props.expenses.expenseList[idx]
this.setState({expenseItems})
}
render() {
const createdDates = this.props.expenses.expenseList.map(({createdAt}) => {
return createdAt
})
const expenseListProps = {
showItemDetails: (idx) => this.showItemDetails(idx),
createdDates
}
const itemDetailsProps = {expenseItems: this.state.expenseItems}
return (
<div>
<ExpenseList {...expenseListProps}/>
<ItemDetails {...itemDetailsProps}/>
</div>
)
}
}
class ExpenseList extends Component{
render() {
const expenseList = this.props.createdDates.map((date, idx) => (
<div key={idx}>
<button onClick={() => this.props.showItemDetails(idx)}>
{date}
</button>
</div>
))
return (
<div>
{expenseList}
</div>
)
}
}
class ItemDetails extends Component {
render() {
debugger;
const items = this.props.expenseItems.map(({uid, date, desc, amount}) => (
<div>
{uid}
{date}
{desc}
{amount}
</div>
))
return (
<div>
{items}
</div>
)
}
}
在我的计时器类中,我在onCreat()中获取窗口:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
在我的计时器的onFinish()中,我调用了wakeUp():
Window win;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
createTimer(time_work);
win = this.getWindow();
}
在wakeUp()中,我尝试了以下各种组合:
public void onFinish() {
text_time.setText(R.string.done);
if (isWork) {
pomodoro_count++;
text_pomodoro_count.setText(String.valueOf(pomodoro_count));
}
isWork = !isWork;
onResume();
//I have more here but removed for brevity
wakeUp();
}
答案 0 :(得分:0)
这需要以与我的想法不同的方式完成。
需要做的是需要通过警报创建新活动。
您仍需要manifest
中的相同权限:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
您需要receiver
:
public class ReceiverAlarm extends BroadcastReceiver {
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
//This is what will be called when your alarm goes off
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Logged", "ReceiverAlarm");
Intent i = new Intent(context, ScreenWake.class);
context.startActivity(i);
}
//This is a method I made that will set an alarm
public void setAlarm(Context context, int timeToPop) {
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ReceiverAlarm.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
long currentTime = SystemClock.elapsedRealtime();
long timeToAdd = (long) timeToPop;
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
currentTime + timeToAdd, alarmIntent);
}
public void cancelAlarm(Context context) {
Intent intent = new Intent(context, ReceiverAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
然后,当通过警报调用警报时,您将需要一个完全不同的活动:
ScreenWake.java
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_wakes_screen);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
ms = new ManagerSound(getApplicationContext());
ms.playAlarm();
}