自从我使用android oreo OS在我的应用上开始测试以来,就发生了奇怪的崩溃。
在一段时间(有时是一天,其他时间需要2-3天)中,前台服务将崩溃/停止,而不会发出任何通知。根据我得到的调试信息,它显示为android.runtime.JavaProxyThrowable
,但是我不确定谁是真正的罪魁祸首。
奇怪的是,它在启动后崩溃,但是表示崩溃原因的函数应该在服务启动时才被调用一次(OnStartCommand)。
从我所看到的,罪魁祸首是OnStartCommand,或者类似的错误提示,但是我不能完全确定实际的问题是什么以及如何解决。 自从从8.0 Oreo开始工作以来,删除所有已添加的所有新代码都不会产生任何结果。
还有其他人有这个问题吗,有解决方案吗?
代码:
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
base.OnStartCommand(intent, flags, startId);
isStarted = true;
LoadSet();//load settings
accumulatedTrafficDown += intent.GetLongExtra("aDownBootup", 0);
accumulatedTrafficUp += intent.GetLongExtra("aUpBootup", 0);
connectivityManager = (ConnectivityManager)(Application.Context.GetSystemService(ConnectivityService));
wifiManager = (WifiManager)(Application.Context.GetSystemService(WifiService));
notificationManager = (NotificationManager)GetSystemService(NotificationService);
builder = new Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.ITMIcon)
.SetOngoing(true)//API 11
.SetShowWhen(false)//API 17
.SetVisibility(NotificationVisibility.Public)
;
if ((int)Build.VERSION.SdkInt >= 26)
{
string chanName = GetString(Resource.String.noti_chan_normal);
var importance = NotificationImportance.Low;
NotificationChannel chan = new NotificationChannel(DEF_CHANNEL, chanName, importance)
{
LockscreenVisibility = NotificationVisibility.Public
};
chan.EnableLights(false);
chan.EnableVibration(false);
chan.SetShowBadge(false);
chan.SetBypassDnd(true);
notificationManager.CreateNotificationChannel(chan);
builder.SetChannelId(DEF_CHANNEL);
chan.Dispose();
}
//open main app when notification is clicked
using (Intent intentMA = new Intent(this/*Application.ApplicationContext*/, typeof(MainActivity)))
{
intentMA.AddFlags(ActivityFlags.ReceiverForeground);
using (PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0, intentMA, PendingIntentFlags.UpdateCurrent))
{
builder.SetContentIntent(pendingIntent);
}
}
StartForeground(notificationId/*(int)NotificationFlags.ForegroundService*/, builder.Build());
lastNetworkTrafficDown = TrafficStats.TotalRxBytes;
lastNetworkTrafficUp = TrafficStats.TotalTxBytes;
//for drawing to tray
bm = Bitmap.CreateBitmap(bitmapSize, bitmapSize, Bitmap.Config.Argb4444, true);
canvas = new Canvas(bm);
bounds = new Rect();
paint = new Paint(PaintFlags.LinearText) { Color = Color.White };
paint.SetStyle(Paint.Style.Fill);
paint.SetShadowLayer(4f, 2f, 2f, Color.Black);//(int)(bitmapSize * 0.03125f), (int)(bitmapSize * 0.015625f), (int)(bitmapSize * 0.015625f)
paint.TextAlign = Paint.Align.Center;
using (Typeface bold = Typeface.Create(paint.Typeface, TypefaceStyle.Bold))
{
paint.SetTypeface(bold);
}
UpdateNotification();
return StartCommandResult.Sticky;
}
错误:
android.runtime.JavaProxyThrowable: at ITM.NotificationService.OnStartCommand (Android.Content.Intent intent, Android.App.StartCommandFlags flags, System.Int32 startId) [0x00016] in <8eb370332be94963ab6dd55bf21270bf>:0
at Android.App.Service.n_OnStartCommand_Landroid_content_Intent_II (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_intent, System.Int32 native_flags, System.Int32 startId) [0x0000f] in <5714ccdc0ab84d74bb3e08064f494a0a>:0
at (wrapper dynamic-method) System.Object.f8a85e62-c1d3-4a9f-baf4-1379f6e1f4f1(intptr,intptr,intptr,int,int)
at com.nevaran.ITMNotificationService.n_onStartCommand (Native Method)
at com.nevaran.ITMNotificationService.onStartCommand (ITMNotificationService.java:31)
at android.app.ActivityThread.handleServiceArgs (ActivityThread.java:3679)
at android.app.ActivityThread.-wrap21 (Unknown Source)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1801)
at android.os.Handler.dispatchMessage (Handler.java:105)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6944)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)