我创建了一个具有定期后台服务(带有警报管理器)的应用程序,该服务每隔x分钟启动一次,因此到目前为止,一切都很好,我可以毫无问题地启动或停止服务。释放模式,此后无论我启动该服务,应用程序都会崩溃!
这是我的代码。
从共享项目启动/停止服务
DependencyService.Get<IAndroidNotifications>().StartNotificationsService();
DependencyService.Get<IAndroidNotifications>().StopNotificationsService();
通知类
public void StartNotificationsService()
{
//Start Notifications Service
Intent myIntent = new Intent(context, typeof(NotificationsService));
context.StartService(myIntent);
//Show Snackbar message
Activity activity = CrossCurrentActivity.Current.Activity;
Android.Views.View activityRootView =
activity.FindViewById(Android.Resource.Id.Content);
Snackbar.Make(activityRootView, "Notifications on",
Snackbar.LengthLong).Show();
}
public void StopNotificationsService()
{
//Stop Notifications Service
Intent myIntent = new Intent(context, typeof(NotificationsService));
context.StopService(myIntent);
//Show Snackbar message
Activity activity = CrossCurrentActivity.Current.Activity;
Android.Views.View activityRootView =
activity.FindViewById(Android.Resource.Id.Content);
Snackbar.Make(activityRootView, "Notifications off",
Snackbar.LengthLong).Show();
}
public bool CheckNotificationsService()
{
ActivityManager manager =
(ActivityManager)context.GetSystemService(Context.ActivityService);
foreach (var service in manager.GetRunningServices(int.MaxValue))
{
string x = service.Service.ClassName;
if (service.Service.ClassName == "com.TestApp.NotificationsService")
{
return true;
}
}
return false;
}
private void StartAlarm(bool isRepeating)
{
AlarmManager manager = (AlarmManager)GetSystemService(Context.AlarmService);
Intent myIntent;
PendingIntent pendingIntent;
myIntent = new Intent(this, typeof(NotificationsAlarm));
pendingIntent = PendingIntent.GetBroadcast(this, 0, myIntent, 0);
if (!isRepeating)
{
manager.Set(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() + 3000,
pendingIntent);
}
else
{
manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime()
+ 3000, 60 * 1000, pendingIntent);
}
}
}
通知服务
[Service
(Name = "com.TestApp.NotificationsService",
Label = "Service Notifications")]
class NotificationsService : Service
{
AlarmManager manager;
Intent myIntent;
PendingIntent pendingIntent;
public override IBinder OnBind(Intent intent)
{
return null;
}
//[return: GeneratedEnum]
public override StartCommandResult OnStartCommand(Intent intent,
StartCommandFlags flags, int startId)
{
//return base.OnStartCommand(intent, flags, startId);
manager = (AlarmManager)GetSystemService(Context.AlarmService);
myIntent = new Intent(this, typeof(NotificationsAlarm));
pendingIntent = PendingIntent.GetBroadcast(this, 0, myIntent, 0);
manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() +
1000, 60 * 1000, pendingIntent);
//Toast.MakeText(this, "Greating from our first service",
ToastLength.Long).Show();
return StartCommandResult.Sticky;
}
public override void OnDestroy()
{
base.OnDestroy();
pendingIntent.Cancel();
manager.Cancel(pendingIntent);
}
}
}
广播接收器
namespace TestApp.Droid
{
[BroadcastReceiver(Enabled = true)]
public class NotificationsAlarm : BroadcastReceiver
{
static Random random = new Random();
int randomNumber = random.Next(0, 1000);
InfosAPI _infosAPI;
public override void OnReceive(Context context, Intent intent)
{
Toast.MakeText(context, "Received intent!", ToastLength.Long).Show();
//something else here
}
}
}
有什么建议吗?谢谢