user1597878
user1597878

Reputation:

Custom notification with remote view on Android

I have edited the code by using setOnClick pending intent, and this code works for ImageView and notifications text seperate as I want, but I still need some help.

I want to pause or play mediaplayer from my service class, so how could I access the stop or play method of service from Notification pending intent?

Some says broadcast receivers will help you, but I don't get exactly how it's going to work. I have managed to open a web browser from the pause button from notification, but I don't know how to access service methods. Share any sample code if you have some.

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

Upvotes: 6

Views: 12052

Answers (2)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You did not tell what version of Android you target, but in general it is not possible until Android 3.x (Honeycomb). So if you want that to happen on 2.x, then sorry - you are out of luck.

For Honeycomb, you simply have to provide a custom layout and assign PendingIntent to each button you need, by calling setOnClickPendingIntent(). If you got the SDK's samples downloaded, then see MainActivity of the HoneycombGallery app (shall be in your <SDK>\samples\android-XX\HoneycombGallery\ folder. XX is anything from 14 (or higher).

Upvotes: 6

Abdullah
Abdullah

Reputation: 8995

Check Bound Services using Messenger http://developer.android.com/guide/components/bound-services.html

Also you can check this question Binding to a local Service from a BroadcastReceiver

Upvotes: 0

Related Questions