在monodroid中重启设备时通知已停止

时间:2015-09-16 19:33:28

标签: android notifications broadcastreceiver xamarin.android alarmmanager

我正在尝试在我的monodroid应用程序中使用通知 我编写的代码运行良好,但是当我重新启动设备时,通知停止并弹出一个警告并说“遗憾的是app-name已经停止” 我真的不知道如何解决它。请帮忙。感谢

主要活动:

Calendar calendar = Calendar.GetInstance(Java.Util.TimeZone.Default);

            calendar.Set(Calendar.Year, DateTime.Now.Year);
            calendar.Set(Calendar.Month, DateTime.Now.Month);
            calendar.Set(Calendar.DayOfYear, DateTime.Now.DayOfYear);
            calendar.Set(Calendar.HourOfDay,12);
            calendar.Set(Calendar.Minute, 00);
            calendar.Set(Calendar.Second, 00);
            calendar.Set(Calendar.Millisecond, 00);

            NotificationReceiver Notifications = new NotificationReceiver();
            Notifications.StartNotify(this, calendar.TimeInMillis);

manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" />
    <!-- Permission to start Alarm on device reboot -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application android:allowBackup="true" android:icon="@drawable/LogoLifeTime" android:label="@string/ApplicationName" android:theme="@style/AppTheme">


    <activity android:name=".Main_Activity" android:label="@string/ApplicationName"></activity>

    <receiver android:name=".NotificationReceiver" android:enabled="true" android:exported="true">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>


    <!-- Will not be called unless the application explicitly enables it -->
    <receiver android:name=".DeviceBootReceiver" android:enabled="false">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </receiver>
    </application>
</manifest>

notificationreceiver:

[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionBootCompleted })]
      class NotificationReceiver : BroadcastReceiver
     {
         PowerManager.WakeLock w1;

          public override void OnReceive(Context context, Intent intent)
        {
                PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
                w1 = pm.NewWakeLock(WakeLockFlags.Partial, "NotificationReceiver");
                w1.Acquire();

                CreateNotification(context);

                w1.Release();

        }

        void CreateNotification(Context context)
        {
            var nMgr = (NotificationManager)context.GetSystemService(Context.NotificationService);
            var notification = new Notification(Resource.Drawable.LogoLifeTime, "LTP");

            var pendingIntent = PendingIntent.GetActivity(context, 0, new Intent(context, typeof(check_daily_tasks_activity)), PendingIntentFlags.OneShot);
            notification.SetLatestEventInfo(context,"hello dear", pendingIntent);
            notification.Defaults |= NotificationDefaults.Sound | NotificationDefaults.Vibrate | NotificationDefaults.Lights;
            notification.Flags = NotificationFlags.ShowLights | NotificationFlags.AutoCancel;
            notification.LedARGB = Color.Blue;
            notification.LedOnMS = 1;
            notification.LedOffMS = 1;
            nMgr.Notify(0, notification);

        }

        public void StartNotify(Context context, long alertTime)
        {
            AlarmManager am = (AlarmManager)context.GetSystemService(Context.AlarmService);
            Intent intent = new Intent(context, this.Class);
            PendingIntent pi = PendingIntent.GetBroadcast(context, 0, intent, 0);
            am.SetRepeating(AlarmType.RtcWakeup, alertTime, AlarmManager.IntervalDay, pi);

        }

    }

DeviceBootReceiver:

[BroadcastReceiver]
    public class DeviceBootReceiver : BroadcastReceiver
    {

        public override void OnReceive(Context context, Intent intent)
        {

            if (intent.Action.Equals("android.intent.action.BOOT_COMPLETED"))
            {
                Calendar calendar = Calendar.GetInstance(Java.Util.TimeZone.Default);
                calendar.Set(Calendar.Year, DateTime.Now.Year);
                calendar.Set(Calendar.Month, DateTime.Now.Month);
                calendar.Set(Calendar.DayOfYear, DateTime.Now.DayOfYear);
                calendar.Set(Calendar.HourOfDay, 12);
                calendar.Set(Calendar.Minute, 00);
                calendar.Set(Calendar.Second, 00);
                calendar.Set(Calendar.Millisecond, 00);

                 NotificationReceiver Notifications = new NotificationReceiver();
                 Notifications.StartNotify(context, calendar.TimeInMillis);

                 Intent alarmIntent = new Intent(context, typeof(NotificationReceiver));
                 PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, alarmIntent, 0);
                 AlarmManager manager = (AlarmManager)context.GetSystemService(Context.AlarmService);
                 manager.SetInexactRepeating(AlarmType.RtcWakeup, calendar.TimeInMillis, AlarmManager.IntervalDay, pendingIntent);
                 Toast.MakeText(context, "Alarm Setsds", ToastLength.Short).Show();

            }
        }

    }

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题:如果我通过任务管理器关闭应用程序,则BroadcastReceivers停止工作。您尝试在发布模式签名应用中构建应用并在Ad-Hoc Distribution

中发布

祝你好运。