MqttAndroidExample icon indicating copy to clipboard operation
MqttAndroidExample copied to clipboard

Application crashed on start: Getting error in service

Open vikas442 opened this issue 6 years ago • 3 comments

2019-06-18 13:38:36.253 14693-14693/com.jflavio1.androidmqttexample E/ActivityThread: Service com.jflavio1.androidmqttexample.mqtt.SensorsMqttService has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@327eeb that was originally bound here android.app.ServiceConnectionLeaked: Service com.jflavio1.androidmqttexample.mqtt.SensorsMqttService has leaked ServiceConnection org.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@327eeb that was originally bound here at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:1532) at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:1424) at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1605) at android.app.ContextImpl.bindService(ContextImpl.java:1557) at android.content.ContextWrapper.bindService(ContextWrapper.java:684) at org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:425) at com.jflavio1.androidmqttexample.mqtt.SensorsMqttService.connectToServer(SensorsMqttService.kt:92) at com.jflavio1.androidmqttexample.mqtt.SensorsMqttService.onStartCommand(SensorsMqttService.kt:64) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3474) at android.app.ActivityThread.-wrap20(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 2019-06-18 13:38:39.284 14693-14693/com.jflavio1.androidmqttexample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jflavio1.androidmqttexample, PID: 14693 android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

vikas442 avatar Jun 18 '19 08:06 vikas442

any fix for this issue

sunilbcone avatar Oct 17 '19 07:10 sunilbcone

the software works like a charm ... Above Oreo you need to make a notification channel for the soft to work.

This is very well explained in the following youtube link https://www.youtube.com/watch?v=FbpD5RZtbCc

Once you do this, everything works as expected

-- myapplication -- @Override public void onCreate() { super.onCreate(); .... createNotificationChannel(); ... } private void createNotificationChannel(){ if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){ NotificationChannel serviceChannel = new NotificationChannel( CHANNEL_ID, "service channel", NotificationManager.IMPORTANCE_DEFAULT ); NotificationManager manager = getSystemService(NotificationManager.class); manager.createNotificationChannel(serviceChannel);

    }
}

--- MQTTService --- public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { try { Notification.Builder builder = new Notification.Builder(this, MyApplication.CHANNEL_ID) .setContentTitle(getString(R.string.app_name)) .setSmallIcon(R.drawable.ic_light) .setContentText("MQTT Running") .setAutoCancel(true);

            Notification notification = builder.build();
            startForeground(1, notification);

lock-e-box avatar Dec 27 '19 09:12 lock-e-box

Thanks @lock-e-box !!

jflavio11 avatar Feb 17 '20 03:02 jflavio11