gpt4 book ai didi

android - Android 上带有远程 View 的自定义通知

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:47 27 4
gpt4 key购买 nike

我已经使用 setOnClick pending intent 编辑了代码,此代码适用于 ImageView 和通知文本,如我所愿,但我仍然需要一些帮助。

我想从我的服务类暂停或播放媒体播放器,那么如何从通知挂起 Intent 访问服务的停止或播放方法?

有人说广播接收器会帮助你,但我不明白它是如何工作的。我已经设法从通知的暂停按钮打开网络浏览器,但我不知道如何访问服务方法。分享任何示例代码(如果有)。

@SuppressWarnings("deprecation")
void showNotification() {
int pendingRequestCode = 0;
int pendingFlag = 0;

final Resources res = getResources();
final NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
Intent intent = new Intent(this,MainActivity.class);
PendingIntent pi= PendingIntent.getActivity(this, 0, intent, 0);
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_action_search)
.setAutoCancel(true)
.setTicker("this is notification")
.setContentIntent(pi);


// Sets a custom content view for the notification, including an image button.
RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification);
layout.setTextViewText(R.id.notification_title, getString(R.string.app_name));
Intent clickIntent = new Intent(Intent.ACTION_VIEW, Uri_myBlog);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),pendingRequestCode, clickIntent, pendingFlag);
layout.setOnClickPendingIntent(R.id.notification_button,pendingIntent);
builder.setContent(layout);

// Notifications in Android 3.0 now have a standard mechanism for displaying large
// bitmaps such as contact avatars. Here, we load an example image and resize it to the
// appropriate size for large bitmaps in notifications.
Bitmap largeIconTemp = BitmapFactory.decodeResource(res,
R.drawable.pause);
Bitmap largeIcon = Bitmap.createScaledBitmap(
largeIconTemp,
res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height),
false);
largeIconTemp.recycle();

builder.setLargeIcon(largeIcon);

notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification());
}

PendingIntent getDialogPendingIntent(String dialogText) {
return PendingIntent.getActivity(
this,
dialogText.hashCode(), // Otherwise previous PendingIntents with the same
// requestCode may be overwritten.
new Intent(ACTION_DIALOG)
.putExtra(Intent.EXTRA_TEXT, dialogText)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK),
0);
}

最佳答案

您没有说明您的目标是哪个版本的 Android,但通常要到 Android 3.x 才能实现(蜂窝)。所以如果你希望在 2.x 上发生这种情况,那么抱歉 - 你运气不好。

对于 Honeycomb,您只需提供自定义布局并分配 PendingIntent您需要的每个按钮,请调用 setOnClickPendingIntent() .如果您下载了 SDK 的示例,请查看 HoneycombGallery 应用程序的 MainActivity(应位于您的 <SDK>\samples\android-XX\HoneycombGallery\ 文件夹中。XX 是 14(或更高)中的任何值)。

关于android - Android 上带有远程 View 的自定义通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12214487/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com