android常驻通知栏能保活吗?比如我在service里面写了socket-tcp连接,想要一直保持这个连接不中断,中断自动连接,但是怕在息屏或使用其他app的时候系统关闭程序,这时用常驻通知栏能保证app不被关闭,并保持socket连接吗?
这是我服务里面写的
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
xml的也加了,现在报 Bad notification for startForeground
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
/*拿到传递过来的ip和端口号*/
ip = intent.getStringExtra("IP");
port =intent.getIntExtra("PORT",0) ;
Notification notification = new NotificationCompat.Builder(this, "1")
.setContentTitle("My App")
.setContentText("Running...")
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_LOW)
.build();
startForeground(NOTIFICATION_ID, notification);
/*初始化socket*/
initSocket();
flags = Service.START_FLAG_REDELIVERY;
return super.onStartCommand(intent, flags, startId);
}