我在VS 2017中使用Xamarin Android为我的侄女制作了一个小应用程序。我希望它在应用程序上打开一个页面,在她每次打开手机时都会问她一个简单的乘法问题。我希望它作为前台服务运行,这样当应用程序最小化时,它仍然可以运行。我有一个广播接收器,当应用程序有焦点时,它会检测到按钮事件,我已经将其移动到服务上运行。
目前,我按下切换按钮,然后5-10秒后,它抛出一个错误。看不到VS中的错误,因为它是外部的。我猜那是Genymotion模拟器捕捉到的吧?
有什么想法吗?
更新:我想我会来更新这篇文章,如果它对其他人有任何用处的话。在Oreo更新之后,NotificationCompat.Builder现在需要提供通道ID。我已经在代码中反映了这一点,它现在可以正确地启动服务。
我仍然需要让广播接收器从服务中工作,但我已经做到了!
更新2:任务完成!
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
Intent startServiceIntent;
Intent stopServiceIntent;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
// Difficulty Radio Buttons
RadioButton radioEasy = FindViewById<RadioButton>(Resource.Id.radioEasy);
radioEasy.Click += RadioButton_Click;
RadioButton radioMedium = FindViewById<RadioButton>(Resource.Id.radioMedium);
radioMedium.Click += RadioButton_Click;
RadioButton radioHard = FindViewById<RadioButton>(Resource.Id.radioHard);
radioHard.Click += RadioButton_Click;
// Set/Remove Lock
Button ToggleLock = FindViewById<Button>(Resource.Id.ToggleLock);
ToggleLock.Click += ToggleLock_Click;
startServiceIntent = new Intent(this, typeof(LockService));
stopServiceIntent = new Intent(this, typeof(LockService));
}
private void RadioButton_Click(object sender, EventArgs e)
{
var radiobtn = sender as RadioButton;
Toast.MakeText(this, radiobtn.Text, ToastLength.Long).Show();
}
private void ToggleLock_Click(object sender, EventArgs e)
{
var togglebtn = sender as ToggleButton;
if (togglebtn.Checked)
{
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
StartForegroundService(startServiceIntent);
else
StartService(startServiceIntent);
Toast.MakeText(this, "On", ToastLength.Long).Show();
}
else
{
Toast.MakeText(this, "Off", ToastLength.Long).Show();
}
}
}
/// <summary>
/// ////////////////////////////////////////////////////////////////////////////
/// </summary>
[Service]
public class LockService : Service
{
private PowerBroadcastReceiver receiver;
public override void OnCreate()
{
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
NotificationManager mngr = (NotificationManager)GetSystemService(NotificationService);
NotificationChannel chnl = new NotificationChannel("Channel", "Name", NotificationImportance.Default);
mngr.CreateNotificationChannel(chnl);
NotificationCompat.Builder foregroundNotification = new NotificationCompat.Builder(this);
foregroundNotification.SetOngoing(true);
foregroundNotification.SetContentTitle("My Foreground Notification")
.SetChannelId("Channel")
.SetContentText("This is the first foreground notification Peace");
StartForeground(101, foregroundNotification.Notification);
// Broadcast Receiver
receiver = new PowerBroadcastReceiver();
RegisterReceiver(receiver, new IntentFilter(Intent.ActionScreenOn));
return StartCommandResult.Sticky;
}
public override IBinder OnBind(Intent intent)
{
// Return null because this is a pure started service. A hybrid service would return a binder that would
// allow access to the GetFormattedStamp() method.
return null;
}
}
/// <summary>
/// ////////////////////////////////////////////////////////////////////////////////
/// </summary>
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionScreenOn, Intent.ActionScreenOff })]
public class PowerBroadcastReceiver : BroadcastReceiver
{
public PowerBroadcastReceiver()
{
}
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action.Equals(Intent.ActionScreenOn))
{
// TODO: Launch Question Page
Toast.MakeText(context, "On", ToastLength.Long).Show();
}
else if (intent.Action.Equals(Intent.ActionScreenOff))
Toast.MakeText(context, "Off", ToastLength.Long).Show();
}
}谢谢。
发布于 2019-03-08 14:40:39
你可以创建一个监听类来监听android的锁屏和解锁事件,使用广播来接收屏幕的状态。因为是系统广播,所以我不认为有必要使用Service。
https://stackoverflow.com/questions/55022171
复制相似问题