警报上升时不显示活动

时间:2016-04-16 12:10:16

标签: android broadcastreceiver alarmmanager

我已经安排了一个闹钟,以便在节目时间到来时启动广播接收器,并在广播检查中查看一些警报对象的参数,以确定要启动的活动..应用程序当屏幕打开但屏幕被锁定时,工作找不到

这就是我所做的:

- 用于安排闹钟

/*  Prepare Alarm Intent*/
            Intent alarmIntent = new Intent(ctx, LockRoute.class);
            alarmIntent.putExtra(CONSTANTS.ALARM_NAME, alarm.alarmName);
            alarmIntent.putExtra(CONSTANTS.ALARM_TONE, alarm.tone);
            alarmIntent.putExtra(CONSTANTS.ALARM_LOCK_TYPE, alarm.unLockType);


            //prepare the Pending intent

            PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, day.getId().intValue(), alarmIntent, 0);

这是广播

 /* Pass Alarm Info to unLock Screen*/
        alarmRaised.putExtra(CONSTANTS.ALARM_NAME,bundle.getString(CONSTANTS.ALARM_NAME));
        alarmRaised.putExtra(CONSTANTS.ALARM_TONE,bundle.getString(CONSTANTS.ALARM_TONE));
        alarmRaised.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(alarmRaised);

我添加了活动的标志,以便在设备锁定时显示

 pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        Logger.init();
         /* Check Device SDK to call the correct Function to TURN ON SCREEN */
        if (Build.VERSION.SDK_INT < 20) {
            if (!pm.isScreenOn()) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                        +WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        +WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
                Logger.d("less than 20");
            }else {
                Logger.d("SCREEN ON < 20");
            }

        } else {
            if (!pm.isInteractive()) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                        + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
                        +WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        +WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
                Logger.d("bigger = than 20");
            }else {
                Logger.d("SCREEN ON else 20");
            }
        }

1 个答案:

答案 0 :(得分:0)

我已经制作了一个应用程序来安排一个响铃,无论屏幕是否被锁定,它都会响铃。这可以帮助您使用AlarmReciever.class文件。

public class AlarmReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {


    //this will sound the alarm tone
    //this will sound the alarm once, if you wish to
    //raise alarm in loop continuously then use MediaPlayer and setLooping(true)
    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null) {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
    ringtone.play();

    //this will send a notification message
    ComponentName comp = new ComponentName(context.getPackageName(),
            AlarmService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

并且要调用它,您将需要在AlarmActivity.class文件中进行编辑

final Intent my_intent = new Intent(AlarmActivity.this, AlarmReceiver.class);
    Button Alarm_ON = (Button) findViewById(R.id.bu1);
    Alarm_ON.setOnClickListener(new View.OnClickListener() {

        @TargetApi(Build.VERSION_CODES.M)
        @Override
        public void onClick(View v) {

            int hour = 0;
            int minute = 0;
            int currentApiVersion = android.os.Build.VERSION.SDK_INT;
            if (currentApiVersion > android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {

                calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getHour());
                calendar.set(Calendar.MINUTE, alarmTimePicker.getMinute());

                hour = alarmTimePicker.getHour();
                minute = alarmTimePicker.getMinute();
            } else {
                calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
                calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());

                hour = alarmTimePicker.getCurrentHour();
                minute = alarmTimePicker.getCurrentMinute();
            }


            String hour_string = String.valueOf(hour);
            String minute_string = String.valueOf(minute);


            if (hour > 12) {
                hour_string = String.valueOf(hour - 12);
            }

            if (minute < 10) {

                minute_string = "0" + String.valueOf(minute);
            }
            //setAlarmText("Alarm set to: " + hour_string + ":" + minute_string);
            Toast.makeText(getApplicationContext(), "Alarm set to: " + hour_string + ":" + minute_string, Toast.LENGTH_LONG).show();
            my_intent.putExtra("extra", "alarm on");

            pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this, 0,
                    my_intent, PendingIntent.FLAG_UPDATE_CURRENT);

            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    pendingIntent);

        }
    });

我们在这里做的是 - 1)从主活动.class文件中,将调用此AlarmReciever.class文件,并且根据预定的时间,无论电话是否被锁定,警报都会响起。

有任何问题,请回复此处。 :)并且不要忘记按下刻度标记接受答案。