如果应用关闭,则无法显示吐司

时间:2016-04-10 18:05:21

标签: android android-alarms

我正在尝试在android中创建一个警报。我有一个祝酒词,但只有当应用程序正在运行时它才有效。即使屏幕关闭或应用程序未运行,我也需要进行吐司/通知。我哪里错了?

MainActivity:

public void setTime() {
            Calendar calendar = Calendar.getInstance();
            timePickerDialog = new TimePickerDialog(
                    MainActivity.this,
                    TimePickerDialog.THEME_HOLO_DARK,
                    onTimeSetListener,
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    true);
            timePickerDialog.setTitle(""); 
            timePickerDialog.show();
    }

    TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener(){
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

            Calendar calNow = Calendar.getInstance();
            Calendar calSet = (Calendar) calNow.clone();

            calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
            calSet.set(Calendar.MINUTE, minute);
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);

            if(calSet.compareTo(calNow) <= 0){
                //Today Set time passed, count to tomorrow
                calSet.add(Calendar.DATE, 1);
            }

            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), pendingIntent);
        }
    };

AlarmReceiver:

public class AlarmReceiver extends WakefulBroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Вставай!", Toast.LENGTH_LONG).show();
        }
}

在Manifest中我有

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<receiver android:name=".AlarmReceiver" android:process=":remote" />

0 个答案:

没有答案