问题
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.
来源:https://stackoverflow.com/questions/62244020/constant-background-running-process-even-when-app-is-not-in-use