0

I have problem with my app for Android. When I run my app using Android studio, I start a servis (foreground) and it works. But When I disconnect my device from computer, and run service, he doesnt work.

I run NotificationListenerService. Someone knows what could be the cause of this ? Please help

 public int onStartCommand(Intent intent, int flags, int startId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(
                "abc.eu", "xyzabc", NotificationManager.IMPORTANCE_HIGH
        );
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(notificationChannel);
    }
    Intent intent1 = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

    Notification notification = new NotificationCompat.Builder(this, "abc.eu") 
            .setContentTitle("title")
            .setContentText("Content")
            .setContentIntent(pendingIntent).build();

    startForeground(1, notification);
    packageManager = getPackageManager();
    apps = loadData(apps);
    Log.d("MainService", "onStartCommand");
    return START_STICKY;  
}
  • If I go to the settings and grant my app notification permissions again, the service starts working properly. Even if the app had these permissions. Anyone know how to fix this? – quwertysokman Dec 16 '20 at 20:44

1 Answers1

0

You may be didn't stop the previous Foreground Service from launching the app for the first time, so when you launch it for the second time, it doesn't start a new Foreground service until the first one is over.

So, first Make sure that your foreground service is stopped by calling stopForeground(true) followed by stopSelf()

Confirm that there is no notification in status bar related to your app.

If still you face a problem: add PendingIntent.FLAG_ONE_SHOT in the pending intent so that this PendingIntent can be used only once

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, 
                              PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Zain
  • 37,492
  • 7
  • 60
  • 84
  • I've tried it and it doesn't work. If I reset the phone, it also doesn't work in the first start. But if I start the service and go to the settings and give the permissions again. This is an old working service. Do you know what this could be about? – quwertysokman Dec 16 '20 at 22:26
  • I can't see more issues in code, can you try it on different devices and API levels – Zain Dec 17 '20 at 09:25
  • Well thank you. Or do you know how to create a service that will work without notification and will not die after a minute? – quwertysokman Dec 17 '20 at 18:08
  • Do you know, why I have notification: "myApp is running Tap for more information or stop the app" but I on StartCommand I create notyfication and set up this: .setContentTitle("title").setContentText("Content") -Why i doesn't see this ? – quwertysokman Dec 17 '20 at 18:30
  • Services by default doesn't require a notification, but you use Foreground service, in order to live even if the user closes the app.. If it doesnt matter that the service is discontinued if the user closes their app, then use normal `Service` with `startService(intent)`... – Zain Dec 17 '20 at 19:10
  • `JobSerice` & `JobIntentService` can also be alternatives for your , you can have a look at [here](https://stackoverflow.com/questions/46336977/how-is-jobintentservice-related-to-jobservice) for more info – Zain Dec 17 '20 at 19:12
  • You have a notification because `Foreground Service` must show a notification , this is implied in using `startForeground` – Zain Dec 17 '20 at 19:13