如何唤醒设备并在锁定屏幕顶部显示活动以进行警报?

Dpe*_*nha 5 android xamarin.android xamarin

我有一个带有闹钟的旧 android 应用程序,用户可以在其中设置时间和闹钟行为,例如铃声和铃声持续时间、音量,如果它只是振动或静音。它正在工作,但由于新的 android 规则,它很久以前就停止工作了,现在我有时间更新它,但我找不到这些问题的最新答案。

一旦闹钟到期,我需要打开一个活动,在任何东西之上,包括另一个应用程序或锁定屏幕,就像默认的 android 闹钟或来电一样。此活动将有一条消息和一个关闭按钮。一旦被解雇,我需要将电话状态恢复到以前的状态。

我可以设置警报并且它可以工作,如果应用程序在前台或后台,则 BroadcastReceiver 会打开活动,但如果应用程序被迫关闭,则不会。它弹出我的应用程序停止的默认崩溃消息。另外,我不知道如何让它在任何锁定屏幕上打开。

我猜这是因为缺少权限或标志。

我正在使用 Xamarin,所以如果您不知道它,您只需要知道活动元数据是在类上设置的,然后编译到清单中。

这是我想在闹钟结束时显示的活动(不是主要活动):

[Activity(Label = "@string/app_name", Theme = "@style/MainTheme.StopAlarm", LaunchMode = Android.Content.PM.LaunchMode.SingleTask)]
public class StopAlarmActivity : Activity
{

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.StopAlarmLayout);

        Bundle bundle = Intent.Extras;
        string msg = bundle.GetString("message");

        TextView nameTV = FindViewById<TextView>(Resource.Id.alarmTextView);
        nameTV.Text = msg;

        Button okButton = FindViewById<Button>(Resource.Id.okButton);
        okButton.Text = AppResources.OK;
        okButton.Click += (object sender, EventArgs args) =>
        {
            Finish();
        };
    }
}
Run Code Online (Sandbox Code Playgroud)

我的接收器:

[BroadcastReceiver]
    public class AlarmReceiver : BroadcastReceiver
    {

        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Alarm worked.", ToastLength.Long).Show();

            string msg = intent.Extras.GetString("message");

            var myIntent = new Intent(context, typeof(StopAlarmActivity));
            Bundle bundle = new Bundle();
            bundle.PutString("message", msg);
            myIntent.PutExtras(bundle);
            myIntent.SetFlags(ActivityFlags.FromBackground);
            myIntent.SetFlags(ActivityFlags.NewTask);
            myIntent.AddCategory(Intent.CategoryLauncher);
            Forms.Context.StartActivity(myIntent);
        }
    }
Run Code Online (Sandbox Code Playgroud)

请不要浪费时间告诉我应该避免这种行为。这是一个闹钟,如果他自己设置,就是为了叫醒他。另外,默认的 android 闹钟不会做我的用户想要做的事情。警报是根据应用程序中的一些数据作为建议预先设置的。用户必须运行它们,并且可以根据他的需要高度定制。

Sus*_*ver 5

只需设置正确的窗口标志即可。这适用于 API 14(?) 到 Oreo Beta:

活动:

[Activity(Label = "AlarmActivity")]
public class AlarmActivity : Activity, View.IOnClickListener
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Alarm);
        FindViewById<Button>(Resource.Id.myButton).SetOnClickListener(this);
    }

    public override void OnAttachedToWindow()
    {
        Window.AddFlags(WindowManagerFlags.ShowWhenLocked | 
                        WindowManagerFlags.KeepScreenOn | 
                        WindowManagerFlags.DismissKeyguard | 
                        WindowManagerFlags.TurnScreenOn);
    }

    public void OnClick(View v)
    {
        Finish();
    }
}
Run Code Online (Sandbox Code Playgroud)

警报管理器测试:

调用此例程,警报管理器将在一分钟内启动AlarmActivity,因此在按钮单击/侦听器中应用此例程并锁定屏幕:

using (var manager = (Android.App.AlarmManager)GetSystemService(AlarmService))
using (var calendar = Calendar.Instance)
{
    calendar.Add(CalendarField.Minute, 1);
    Log.Debug("SO", $"Current date is   : {Calendar.Instance.Time.ToString()}");
    Log.Debug("SO", $"Alarm will fire at {calendar.Time.ToString()}");
    var alarmIntent = new Intent(ApplicationContext, typeof(AlarmActivity));
    var pendingIntent = PendingIntent.GetActivity(this, 0, alarmIntent, PendingIntentFlags.OneShot);
    manager.SetExact(AlarmType.RtcWakeup, calendar.TimeInMillis, pendingIntent);
}
Run Code Online (Sandbox Code Playgroud)