如何在xamarin

时间:2016-11-23 22:16:00

标签: android xamarin permissions cross-platform startup

我想写一个程序,当手机开机时会自动运行。我想在某些时候每天显示来自该程序的通知。用户将设置这些通知的时间。我正在使用sqlite在数据库中注册它。我已经为我编写的程序中定义了一个广播接收器和服务。我正在使用待定意图创建通知。因此,当手机关闭时,用户正在生成的通知正在消失。我的课程是这样的:

Class BootCompleted:BroadcastReceiver

类AlarmReceiver:BroadcastReceiver

Class AutoStartUp:Service

在我的AndroidManifest中,我做了以下定义:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application android:label="myprogram">
    <receiver
      android:name=".ReminderService.BootCompleted"
      android:enabled="true"
      android:exported="false">
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
      </intent-filter>
    </receiver>
    <service android:name=".ReminderService.AutoStartUp">
    </service>
  </application>

因此,当我在手机上安装此程序并尝试使用时,问题就出现了。 因此,当我以用户身份创建通知,然后关闭和打开手机时,我收到一个错误,例如“该程序停止工作”#39; 为什么会这样?

我的BootCompleted类是扩展BroadcastReceiver

    [BroadcastReceiver]
    [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
    public class BootCompleted : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Log.Debug("BroadcastReceiverBoot", "BootCompleted");
            if (intent.Action.Equals("android.intent.action.BOOT_COMPLETED"))
            {
                Intent serviceIntent = new Intent(context, typeof(AutoStartUp));
                context.StartService(serviceIntent);
            }
        }
    }

我的AlarmReceiver类是扩展BroadcastReceiver

[BroadcastReceiver]
    [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
    public class AlarmReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {

                var message = intent.GetStringExtra("message");
                var title = intent.GetStringExtra("title");

                var notIntent = new Intent(context, typeof(MainActivity));
                notIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
                var contentIntent = PendingIntent.GetActivity(context, 0, notIntent,
                    PendingIntentFlags.CancelCurrent);
                var manager = NotificationManagerCompat.From(context);

                var style = new NotificationCompat.BigTextStyle();
                style.BigText(message);

                var builder = new NotificationCompat.Builder(context)
                                .SetContentIntent(contentIntent)
                                .SetSmallIcon(Resource.Drawable.icon)
                                .SetContentTitle(title)
                                .SetContentText(message)
                                .SetStyle(style)
                                .SetAutoCancel(true);

                var notification = builder.Build();
                manager.Notify(DateTime.Now.Millisecond, notification);
        }
    }

我的AutoStartUp类是扩展服务

class AutoStartUp : Service
    {
        public override IBinder OnBind(Intent intent)
        {
            return null;
        }

        public override void OnCreate()
        {
            try
            {
                Toast.MakeText(this, "boot1", ToastLength.Long).Show();
                AndroidReminderService.BootRemind(DateTime.Now, "Boot Control", "Phone Opened!", DateTime.Now.Hour, DateTime.Now.Minute + 2, DateTime.Now.Second);
                Toast.MakeText(this, "boot2", ToastLength.Long).Show();
            }
            catch (Exception e)
            {
                Toast.MakeText(this, e.Message, ToastLength.Long).Show();
                throw;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试在broadcastreceiver类中添加它们

[BroadcastReceiver]
[IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]
class BroadcastReceiverBoot : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Log.Debug("BroadcastReceiverBoot", "Boot Completed ");
    }
}