Tôi thấy rằng khi bạn sử dụng các nút tác vụ trong thông báo mở rộng, bạn phải viết thêm và bạn bị hạn chế hơn.
Trước khi sử dụng thông báo mở rộng, hành động mặc định trong thông báo tải xuống tệp của tôi là bắt đầu hoạt động XEM trên tệp. Ý định VIEW được bao bọc bởi ý định của Người chọn. Tôi không thể sử dụng mục đích đang chờ xử lý cho ý định Trình chọn trực tiếp từ thông báo vì Trình chọn sẽ bị lỗi nếu không có hoạt động nào để xem loại tệp. Vì vậy, tôi đã có một BroadcastReceiver mà sẽ bắt đầu ý định Chooser.
Với thông báo mở rộng, tôi quyết định thay đổi thông báo tải xuống tệp để hành động mặc định là hiển thị hoạt động chi tiết tệp, với các nút tác vụ cho Xem và gửi. Như đã lưu ý bởi user2536953, bắt đầu một bộ thu phát sóng từ thông báo không đóng ngăn thông báo. Dựa trên thông tin của anh ấy rằng một hoạt động sẽ đóng ngăn kéo, tôi đã thay đổi bộ thu phát sóng của tôi thành NotificationActivity mà không có bất kỳ giao diện người dùng nào.
Như được nêu trong bài đăng này How to dismiss Android notification after action has been clicked, một vấn đề khác là bạn phải hủy thủ công thông báo của mình khi người dùng nhấp vào nút tác vụ. Thông báo chỉ bị hủy tự động cho hành động mặc định. Tôi cũng đã thêm mã trong NotificationActivity để xử lý việc này.
Xây dựng thông báo mở rộng với xem và gửi cho nút:
NotificationCompat.Builder builder = new NotificationCompat.Builder(m_context).setAutoCancel(true);
final PendingIntent contentIntent = DownloadedFileIntentUtils.buildPendingItemDetailIntent(m_context, item);
builder.setContentIntent(contentIntent);
PendingIntent viewIntent = DownloadedFileIntentUtils.buildNotificationActionIntent(m_context, Intent.ACTION_VIEW,
m_context.getString(R.string.action_open), uri, MimeTypeUtil.getMimeType(item), id);
builder.addAction(R.drawable.actionbar_open_with, m_context.getString(R.string.action_open), viewIntent);
PendingIntent sendIntent = DownloadedFileIntentUtils.buildNotificationActionIntent(m_context, Intent.ACTION_SEND,
m_context.getString(R.string.action_send), uri, MimeTypeUtil.getMimeType(item), id);
builder.addAction(R.drawable.actionbar_share, m_context.getString(R.string.action_send), sendIntent);
builder.setTicker(title)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.notification_download);
.setStyle(new NotificationCompat.BigTextStyle().bigText(text));
getNotificationManager().notify(id, builder.build());
Xây dựng mục đích để bắt đầu một hoạt động từ các nút hành động thông báo:
public static PendingIntent buildNotificationActionIntent(Context context, String action, String actionTitle, Uri uri,
String mimeType, int notificationId) {
// Build the file action intent (e.g. VIEW or SEND) that we eventually want to start.
final Intent fileIntent = buildFileActionIntent(action, actionTitle, uri, mimeType);
// Build the intent to start the NotificationActivity.
final Intent notificationIntent = new Intent(context, NotificationActivity.class);
// This flag must be set on activities started from a notification.
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Pass the file action and notification id to the NotificationActivity.
notificationIntent.putExtra(Intent.EXTRA_INTENT, fileIntent);
notificationIntent.putExtra(IIntentCode.INTENT_EXTRA_NOTIFICATION_ID, notificationId);
// Return a pending intent to pass to the notification manager.
return PendingIntent.getActivity(context, s_intentCode.getAndIncrement(), notificationIntent, PendingIntent.FLAG_ONE_SHOT);
}
public static Intent buildFileActionIntent(String action, String actionTitle,
Uri uri, String mimeType) {
Intent intent = new Intent(action);
intent.addCategory(Intent.CATEGORY_DEFAULT);
if (action.equals(Intent.ACTION_SEND)) {
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType(mimeType);
} else {
intent.setDataAndType(uri, mimeType);
}
intent.putExtra(Intent.EXTRA_TITLE, actionTitle);
// Grant read permission on the file to other apps without declared permission.
int flags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
intent.setFlags(flags);
return intent;
}
hoạt động thông báo mà không cần bất kỳ Giao diện người dùng:
public class NotificationActivity extends Activity {
private final static Logger s_logger = LogUtil.getLogger(NotificationActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
// Cancel the notification that initiated this activity.
// This is required when using the action buttons in expanded notifications.
// While the default action automatically closes the notification, the
// actions initiated by buttons do not.
int notificationId = intent.getIntExtra(IIntentCode.INTENT_EXTRA_NOTIFICATION_ID, -1);
if (notificationId != -1) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(notificationId);
}
// If there is an activity to handle the action, start the file action.
if (DownloadedFileIntentUtils.verifyActivityIsAvailable(this, fileActionIntent, false)) {
fileActionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
DownloadedFileIntentUtils.startFileActionActivity(this, fileActionIntent);
}
// Finish activity.
finish();
}
public static void startFileActionActivity(Context context, Intent fileActionIntent) {
// Start chooser intent.
Intent chooser = Intent.createChooser(fileActionIntent, fileActionIntent.getStringExtra(Intent.EXTRA_TITLE));
// Copy the flags from fileActionIntent to chooser intent.
// FileActionExecutor must set FLAG_ACTIVITY_NEW_TASK on the intent passed to startActivity
// because the flag is required when starting an activity from a context that is not an activity.
chooser.addFlags(fileActionIntent.getFlags());
context.startActivity(chooser);
}
Đừng quên thêm NotificationActivity vào AndroidManifest.xml.
Bạn đã bao giờ tìm thấy giải pháp cho điều này chưa? –
Không. Chỉ cần một số hacks để sử dụng phản ánh và mở rộng, thu gọn thanh trạng thái. Rõ ràng, nếu Mục đích đang chờ của bạn chứa Intent phân giải hoạt động, ngăn kéo sẽ tự động bị thu gọn. Tuy nhiên, nếu Mục đích đang chờ xử lý của bạn kết thúc một BroadcastReceiver hoặc Dịch vụ, khay Thông báo sẽ giữ trạng thái (mở rộng) của nó. – Aster