6

我已经使用 setOnClick 挂起意图编辑了代码,并且此代码适用于 ImageView 和我想要的通知文本分开,但我仍然需要一些帮助。

我想从我的服务类暂停或播放媒体播放器,那么如何从通知待处理意图访问服务的停止或播放方法?

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

@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);
    }
4

2 回答 2

6

您没有说明您的目标是什么版本的 Android,但通常在Android 3.x (Honeycomb)之前是不可能的。因此,如果您希望在 2.x 上发生这种情况,那么对不起-您不走运。

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

于 2012-08-31T11:37:02.943 回答
0

使用 Messenger http://developer.android.com/guide/components/bound-services.html检查绑定服务

你也可以检查这个问题 Binding to a local Service from a BroadcastReceiver

于 2013-01-31T12:05:05.210 回答