2

I am working with other contributors on an open-source project in which we have made an Android App. Whenever the app is opened we get a notification that the app is running in the background and even when the app session is terminated, the notification doesn't go away. Here is the link for the project

The app has a lot of classes which could be the reason but we are not able to narrow it down. Though we do think that it could be the BluetoothService activity which uses Bluetooth signals. The notification comes as soon as the app is launched and then just stays there.

public class BluetoothService extends Service {

private static final Long TIMEOUT = Long.valueOf(2000);
private Application bluetoothApplication = null;
private static final String CHANNEL_ID = "BluetoothServiceChannel";
private static final String TAG = "BluetoothService";
private BluetoothScanner bluetoothScanner = null;
private BluetoothAdvertiser bluetoothAdvertiser = null;

public static final String ADVERTISE_FAILED =
        "com.example.android.bluetoothadvertisements.advertising_failed";



@Override
public void onCreate() {
    super.onCreate();
    bluetoothApplication = getApplication();
    bluetoothAdvertiser = new BluetoothAdvertiser(bluetoothApplication.getApplicationContext(), BluetoothAdapter.getDefaultAdapter());
    bluetoothScanner = new BluetoothScanner(bluetoothApplication.getApplicationContext(), BluetoothAdapter.getDefaultAdapter());

}


@Override
public IBinder onBind(Intent intent) {
    return null;
}



private void sendFailureIntent(int errorCode) {
    Intent failureIntent = new Intent();
    failureIntent.setAction(ADVERTISE_FAILED);
    // failureIntent.putExtra(ADVERTISE_FAIL_EXTRA_CODE, errorCode);
    sendBroadcast(failureIntent);
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        this.CreateChannelIfNeeded();

    Intent notificationIntent = new Intent(this, PhysicalDistancerActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);


    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();

    startForeground(6, notification);
    bluetoothAdvertiser.startAdvertising();
    bluetoothScanner.startScanning();
    return START_STICKY;
}

private void CreateChannelIfNeeded() {
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

}

@Override
public void onDestroy() {
   //  Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
    // running = false;
   bluetoothAdvertiser.stopAdvertising();
    bluetoothScanner.stopScanning();
    stopForeground(true);
    super.onDestroy();
}
}

This could be a major reason for uninstallations when the app is released so any input will be really helpful.

Ravish Jha
  • 481
  • 3
  • 25
  • Giving it a quick look, I can see that there is a service which keeps on running in the background. BluetoothService (under contactTracer\bluetooth). Check it out. – Atish Agrawal Jun 07 '20 at 10:40
  • how do I make sure it closes with the app? I have tried to add the code in this link but it has no effect https://stackoverflow.com/questions/41033400/my-background-music-service-in-android-app-keeps-on-playing-even-when-i-close-th – Ravish Jha Jun 07 '20 at 10:47
  • This may be helpful https://stackoverflow.com/questions/20857120/what-is-the-proper-way-to-stop-a-service-running-as-foreground – Atish Agrawal Jun 07 '20 at 10:49
  • Will check it out. One more thing, the notification comes as soon as the app is opened so does that help you narrow it down? – Ravish Jha Jun 07 '20 at 10:55

0 Answers0