When I debug application builds successfully, but crushes fatally on my monitoring device with "Application Stopped" notification. In my logcat, I have spotted two errors starting with lines caused by... . Now, my issue here is, I don't know how to handle these errors so I fix my app. I am quite new to Android Programming.
Logcat:
2020-05-07 06:02:51.037 27871-27871/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.groupkse.indupendo, PID: 27871
java.lang.RuntimeException: Unable to start service net.groupkse.indupendo.PlayerService@bdbfc6 with Intent { act=net.groupkse.indupendo.action.startforeground cmp=net.groupkse.indupendo/.PlayerService (has extras) }: java.lang.SecurityException: Permission Denial: startForeground from pid=27871, uid=10172 requires android.permission.FOREGROUND_SERVICE
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3804)
at android.app.ActivityThread.access$1700(ActivityThread.java:213)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1731)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
Caused by: java.lang.SecurityException: Permission Denial: startForeground from pid=27871, uid=10172 requires android.permission.FOREGROUND_SERVICE
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.os.Parcel.readException(Parcel.java:1868)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5067)
at android.app.Service.startForeground(Service.java:695)
at net.groupkse.indupendo.PlayerService.showNotification(PlayerService.java:95)
at net.groupkse.indupendo.PlayerService.onStartCommand(PlayerService.java:43)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3785)
at android.app.ActivityThread.access$1700(ActivityThread.java:213)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1731)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.enforcePermission(ActivityManagerService.java:10030)
at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:1268)
at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:949)
at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:21931)
at android.app.IActivityManager$Stub.onTransact$setServiceForeground$(IActivityManager.java:10685)
2020-05-07 07:06:27.533 7058-7058/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: net.groupkse.indupendo, PID: 7058
java.lang.RuntimeException: Unable to start service net.groupkse.indupendo.PlayerService@bdbfc6 with Intent { act=net.groupkse.indupendo.action.startforeground cmp=net.groupkse.indupendo/.PlayerService (has extras) }: java.lang.SecurityException: Permission Denial: startForeground from pid=7058, uid=10172 requires android.permission.FOREGROUND_SERVICE
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3804)
at android.app.ActivityThread.access$1700(ActivityThread.java:213)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1731)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
Caused by: java.lang.SecurityException: Permission Denial: startForeground from pid=7058, uid=10172 requires android.permission.FOREGROUND_SERVICE
at android.os.Parcel.createException(Parcel.java:1950)
at android.os.Parcel.readException(Parcel.java:1918)
at android.os.Parcel.readException(Parcel.java:1868)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:5067)
at android.app.Service.startForeground(Service.java:695)
at net.groupkse.indupendo.PlayerService.showNotification(PlayerService.java:95)
at net.groupkse.indupendo.PlayerService.onStartCommand(PlayerService.java:43)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3785)
at android.app.ActivityThread.access$1700(ActivityThread.java:213)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1731)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6819)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
Caused by: android.os.RemoteException: Remote stack trace:
Below are two methods I suspect error to arise from:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getStringExtra("url") != null)
playStream(intent.getStringExtra("url"));
if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {
Log.i("info", "Start foreground service!");
showNotification();
} else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) {
Log.i("info", "Prev pressed.");
} else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) {
Log.i("info", "Play pressed.");
} else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) {
Log.i("info", "Next pressed.");
} else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
Log.i("info", "Stop foreground received.");
stopForeground(true);
stopSelf();
}
return START_REDELIVER_INTENT;
}
and:
private void showNotification () {
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
notificationIntent.setFlags((Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent previousIntent = new Intent(this, MainActivity.class);
previousIntent.setAction(Constants.ACTION.PREV_ACTION);
PendingIntent ppreviousIntent = PendingIntent.getActivity(this, 0, previousIntent, 0);
Intent playIntent = new Intent(this, MainActivity.class);
playIntent.setAction(Constants.ACTION.PLAY_ACTION);
PendingIntent pplayIntent = PendingIntent.getActivity(this, 0, playIntent, 0);
Intent nextIntent = new Intent(this, MainActivity.class);
nextIntent.setAction(Constants.ACTION.NEXT_ACTION);
PendingIntent pnextIntent = PendingIntent.getActivity(this, 0, nextIntent, 0);
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_kwacha_coin);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("Music Player")
.setTicker("Playing music")
.setContentText("My song")
.setSmallIcon(R.drawable.ic_kwacha_coin)
.setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false))
.setContentIntent(pendingIntent)
.setOngoing(true)
.addAction(android.R.drawable.ic_media_previous, "Previou", ppreviousIntent)
.addAction(android.R.drawable.ic_media_play, "Play", pplayIntent)
.addAction(android.R.drawable.ic_media_next, "Next", pnextIntent)
.build();
startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
}
NB: I have noticed that inside Android Studio the new assignment "Notification.Builder" is stricken through with a line like cancelling; I don't know what that means.