Android崩溃与StartService()调用

时间:2017-11-03 09:38:35

标签: android xamarin service

我正在尝试启动服务,以便我可以接收预定的通知,但是当预定的时间到达应用程序崩溃时(不幸的是应用程序已经停止),为什么会发生这种情况。

  

我遇到的挑战之一,不幸的是,我不知道如何看待例外   日志中服务

我的舱单如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.diabetics.Diabetes">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.REBOOT" />
    <application android:label="Diabetes">
        <!-- Will not be called unless the application explicitly enables it -->
        <service android:name="com.diabetics.Diabetes.AppStickyService">
            <intent-filter>
                <action android:name="com.diabetics.Diabetes.AppStickyService" />
            </intent-filter>
        </service>
        <receiver android:name="com.diabetics.Diabetes.AlarmReceiver" android:enabled="false" android:exported="true" android:process=":remote" android:label="AlarmReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

然后我的服务类

[Service(Exported = true, Name = "com.diabetics.Diabetes.AppStickyService")]
    public class AppStickyService : Service
    {

        public override void OnCreate()
        {
            base.OnCreate();
            Toast.MakeText(this, "Service started", ToastLength.Long).Show();

            System.Diagnostics.Debug.WriteLine("Sticky Service - Created");
        }

        public override StartCommandResult OnStartCommand(Android.Content.Intent intent, StartCommandFlags flags, int startId)
        {

            return StartCommandResult.Sticky;
        }

        public override Android.OS.IBinder OnBind(Android.Content.Intent intent)
        {
            System.Diagnostics.Debug.WriteLine("Sticky Service - Binded");
            Toast.MakeText(this, "Service started", ToastLength.Long).Show();

            return null;
        }

        public override void OnDestroy()
        {
            try
            {
                base.OnDestroy();
            }
            catch (Java.Lang.IllegalStateException ex)
            {
                //Log.Debug("MainActivity.OnDestroy", ex, "The activity was destroyed twice");
            }

            cancelnotification(0);

        }
}

然后如何调用StartService()中的MainApplication.cs

[Application]
public class MainApplication : Application
{
    ISetAlarm alarmService;
    public static Context AppContext;

    public MainApplication()
    {

    }
    public MainApplication(IntPtr handle, JniHandleOwnership transer) : base(handle, transer)
    {
    }


    public override void OnCreate()
    {

        base.OnCreate();
        AppContext = this.ApplicationContext;

        try
        {

            StartService();
            alarmService = new SetAlarmImplementation();

            alarmService.SetAlarm(12, 14, "hello", "great work ");
        }

        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("Error  " + e);

        }
    }



    public void SetAlarm(int hour, int minute, string title, string message)
    {

        AppContext.StartService(new Intent(AppContext, typeof(AppStickyService)));
        Intent myintent = new Intent(Android.App.Application.Context, typeof(AppStickyService));
        myintent.PutExtra("message", message);
        myintent.PutExtra("title", title);
        //PendingIntent pendingintent = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, myintent, PendingIntentFlags.UpdateCurrent);
        PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(AppStickyService)), 0);

        Java.Util.Date date = new Java.Util.Date();
        Java.Util.Calendar cal = Java.Util.Calendar.Instance;
        cal.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
        cal.Set(Java.Util.CalendarField.HourOfDay, hour);
        cal.Set(Java.Util.CalendarField.Minute, minute);
        cal.Set(Java.Util.CalendarField.Second, 0);
        AlarmManager alarmManager = Android.App.Application.Context.GetSystemService(Android.Content.Context.AlarmService) as AlarmManager;
        alarmManager.Set(AlarmType.RtcWakeup, cal.TimeInMillis, pintent);
        // alarmManager.Cancel(pintent);

    }





    public void StartService()
    {

        AppContext.StartService(new Intent(AppContext, typeof(AppStickyService)));
        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
        {

            StartService(new Intent(this, typeof(AppStickyService)));


        }




    }

    public static void StopService()
    {
        AppContext.StopService(new Intent(AppContext, typeof(AppStickyService)));
        if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat)
        {
            // Toast.MakeText(this,"Service started", ToastLength.Long).Show();

            //PendingIntent pintent = PendingIntent.GetService(AppContext, 0, new Intent(AppContext, typeof(AppStickyService)), 0);
            // AlarmManager alarm = (AlarmManager)AppContext.GetSystemService(Context.AlarmService);
            //alarm.Cancel(pintent);
        }
    }
  

应用程序崩溃,我不知道确切的异常,怎么可能   我完美了,请。

我的UPDATED堆栈跟踪:

  11-03 18:45:03.314 27931 27931 E AndroidRuntime: FATAL EXCEPTION: main
11-03 18:45:03.314 27931 27931 E AndroidRuntime: Process: com.diabetics.Diabetes, PID: 27931
11-03 18:45:03.314 27931 27931 E AndroidRuntime: android.runtime.JavaProxyThrowable: System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it.
11-03 18:45:03.314 27931 27931 E AndroidRuntime:   at Xamarin.Forms.Device.get_PlatformServices () [0x00007] in <1982f4f36e0b4e118327ea411be26e7b>:0 
11-03 18:45:03.314 27931 27931 E AndroidRuntime:   at Xamarin.Forms.Device.GetAssemblies () [0x00000] in <1982f4f36e0b4e118327ea411be26e7b>:0 
11-03 18:45:03.314 27931 27931 E AndroidRuntime:   at Xamarin.Forms.DependencyService.Initialize () [0x00008] in <1982f4f36e0b4e118327ea411be26e7b>:0 
11-03 18:45:03.314 27931 27931 E AndroidRuntime:   at Xamarin.Forms.DependencyService.Get[T] (Xamarin.Forms.DependencyFetchTarget fetchTarget) [0x00000] in <1982f4f36e0b4e118327ea411be26e7b>:0 
11-03 18:45:03.314 27931 27931 E AndroidRuntime:   at Diabetes.localDB.MedicationDatabase..ctor () [0x00008] in <1d32457e60b941eda588a8ccf582a78e>:0 
11-03 18:45:03.314 27931 27931 E AndroidRuntime:   at Diabetes.Droid.AlarmReceiver.OnReceive (Android.Content.Context context, Android.Content.Intent intent) [0x000e9] in <a946f75a1e344852a2da6cb15bcf50b1>:0 
11-03 18:45:03.314 27931 27931 E AndroidRuntime:   at Android.Content.BroadcastReceiver.n_OnReceive_Landroid_content_Context_Landroid_content_Intent_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_context, System.IntPtr native_intent) [0x00017] in <d278c06ad5684d6882c743a94a93ebc2>:0 
11-03 18:45:03.314 27931 27931 E AndroidRuntime:   at (wrapper dynamic-method) System.Object:6ad989d4-d363-48db-abae-68048f2d711b (intptr,intptr,intptr,intptr)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at md5d7f88582010464bb2d25fa2f1bb63590.AlarmReceiver.n_onReceive(Native Method)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at md5d7f88582010464bb2d25fa2f1bb63590.AlarmReceiver.onReceive(AlarmReceiver.java:29)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2910)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at android.app.ActivityThread.access$1800(ActivityThread.java:175)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:111)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:207)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5728)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-03 18:45:03.314 27931 27931 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

0 个答案:

没有答案