Tôi có một IntentService và tôi muốn làm cho nó dính với một thông báo đang diễn ra. Vấn đề là thông báo xuất hiện và sau đó biến mất ngay lập tức. Dịch vụ tiếp tục chạy. Tôi nên sử dụng startForeground trong IntentService như thế nào?StartForeground for IntentService
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Notification notification = new Notification(R.drawable.marker, "Notification service is running",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, DashboardActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "App",
"Notification service is running", pendingIntent);
notification.flags|=Notification.FLAG_NO_CLEAR;
startForeground(1337, notification);
return START_STICKY;
}
@Override
protected void onHandleIntent(Intent intent) {
String id = intent.getStringExtra(ID);
WebSocketConnectConfig config = new WebSocketConnectConfig();
try {
config.setUrl(new URI("ws://" + App.NET_ADDRESS
+ "/App/socket?id="+id));
} catch (URISyntaxException e) {
e.printStackTrace();
}
ws = SimpleSocketFactory.create(config, this);
ws.open();
}
Cảm ơn
bao gồm mã của bạn quá – waqaslam