我正在与其他贡献者合作开发一个Android应用程序的开源项目。每当打开应用程序时,我们都会收到一条通知,通知您该应用程序在后台运行,即使该应用程序会话终止了,该通知也不会消失。 Here is the link for the project
该应用程序有很多类,这可能是原因,但我们无法缩小范围。尽管我们确实认为可能是使用蓝牙信号的BluetoothService
活动。该通知会在应用程序启动后发出,然后停留在该位置。
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();
}
}
这可能是发布该应用程序时进行卸载的主要原因,因此任何输入都将非常有帮助。