在Android上使用远程视图自定义通知

时间:2012-08-31 11:33:05

标签: android android-layout android-widget

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

我想暂停或从我的服务类播放媒体播放器,那么如何从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);
    }

2 个答案:

答案 0 :(得分:6)

您没有告诉您目标的Android版本,但通常只有Android 3.x(Honeycomb)才能实现。所以如果你希望在2.x上发生这种情况,那么抱歉 - 你运气不好。

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

答案 1 :(得分:0)