我已经使用setOnClick pending intent编辑了代码,这段代码可以根据我的需要对ImageView和通知文本进行分离,但我仍然需要一些帮助。
我想从我的服务类暂停或播放mediaplayer,那么我如何从Notification pending intent访问服务的stop或play方法?
有些人说广播接收器会帮助你,但我不知道它到底是如何工作的。我已经设法从通知中的暂停按钮打开了web浏览器,但我不知道如何访问服务方法。如果你有的话,可以分享任何示例代码。
@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);
}发布于 2012-08-31 19:37:03
你没有说出你的目标是什么版本的安卓,但一般来说,这在Android 3.x (蜂巢)之前是不可能的。所以,如果你想在2.x上发生这种情况,那么很抱歉--你运气不好。
对于Honeycomb,您只需提供一个自定义布局,并通过调用setOnClickPendingIntent()将PendingIntent分配给您需要的每个按钮。如果您下载了SDK的示例,请参阅HoneycombGallery应用程序的MainActivity (应该在您的<SDK>\samples\android-XX\HoneycombGallery\文件夹中。XX是14 (或更高)之间的任何值。
发布于 2013-01-31 20:05:05
使用信使http://developer.android.com/guide/components/bound-services.html检查绑定服务
你也可以查看这个问题Binding to a local Service from a BroadcastReceiver
https://stackoverflow.com/questions/12214487
复制相似问题