【发布时间】:2017-08-23 15:22:25
【问题描述】:
我有一个带有闹钟的旧 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();
};
}
}
我的接收器:
[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);
}
}
请不要浪费时间告诉我应该避免这种行为。这是一个闹钟,如果是他自己设置的,它是用来叫醒他的。另外,默认的 android 闹钟不会做我的用户想要做的事情。警报之前是根据应用程序中的一些数据作为建议设置的。用户必须运行它们,并且它可以根据他的需要进行高度定制。
【问题讨论】:
-
顺便说一句,我更新了我的答案,还包括了锁屏部分。
标签: android xamarin xamarin.android