Câu hỏi ngắn:Cách sử dụng NotificationCompat.Builder và startForeground?
Tôi đang cố gắng sử dụng lớp NotificationCompat.Builder để tạo thông báo sẽ được sử dụng cho dịch vụ, nhưng vì một lý do nào đó, tôi không thấy thông báo hoặc có thể 't hủy bỏ nó khi dịch vụ sẽ bị phá hủy (hoặc dừng lại ở phía trước).
mã của tôi:
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
final String action = intent == null ? null : intent.getAction();
Log.d("APP", "service action:" + action);
if (ACTION_ENABLE_STICKING.equals(action)) {
final NotificationCompat.Builder builder = new Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("content title");
builder.setTicker("ticker");
builder.setContentText("content text");
final Intent notificationIntent = new Intent(this, FakeActivity.class);
final PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder.setContentIntent(pi);
final Notification notification = builder.build();
// notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
// notification.flags |= Notification.FLAG_NO_CLEAR;
// notification.flags |= Notification.FLAG_ONGOING_EVENT;
startForeground(NOTIFICATION_ID, notification);
// mNotificationManager.notify(NOTIFICATION_ID, notification);
} else if (ACTION_DISABLE_STICKING.equals(action)) {
stopForeground(true);
stopSelf();
// mNotificationManager.cancel(NOTIFICATION_ID);
}
return super.onStartCommand(intent, flags, startId);
}
Các lệnh nhận xét là thử nghiệm của tôi để làm cho nó làm việc. không có lý do gì.
Tôi thậm chí còn thêm hoạt động giả mạo vì nó muốn contentIntent, nhưng nó vẫn không hoạt động.
Mọi người có thể giúp bạn không?
bài này, cùng với câu trả lời được chấp nhận, cố định vấn đề của tôi sau khi làm việc trong nhiều ngày trên một giải pháp. –