6

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);
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Gives me a suggestion. I build a notification big view using RemoteView to control play/pause like this link (http://stackoverflow.com/questions/14508369/how-to-create-a-notification-similar-to-play-music-app-from-google) All are right but when i click device back button and out from the application click event(Play/Pause/Forward/Close) button doesn't work.Please help me. – Helal Khan Mar 11 '14 at 13:54

2 Answers2

6

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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • my target is min 2.2 to 4.1 ...its ok that froyo wont support this facility .. but honeycomb and jellybean users can uses it in my app please guide me for this features –  Aug 31 '12 at 11:39
  • is there any way to check android API number and according to that i can change my notification style like normal for less than 2.2 and button notification for 2.3 and above –  Aug 31 '12 at 12:56
  • Sure. Use `Build.VERSION.SDK_INT` as described here http://developer.android.com/reference/android/os/Build.VERSION_CODES.html – Marcin Orlowski Aug 31 '12 at 12:57
  • can you please give me sample code for above mention problem..i want only one button on notification to pause stream... i tried honeycomb method but i failed init.. can you write small code for that please –  Aug 31 '12 at 14:42
  • i haved edited as you said .. but can you tell me how can i access pause play method of service from notification –  Sep 11 '12 at 11:25
  • what exactly you mean by "from notification"? – Marcin Orlowski Sep 11 '12 at 11:26
  • I want to access particular method from my activity or service from notification 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); –  Sep 11 '12 at 11:29
  • Notification is basically `PendingIntent`, not the code that can do anything. What you should do is to make your notification point to some code which would do the job. To tell that code what to do pass some info in `Bundle` attached to your notification's `PendingIntent` – Marcin Orlowski Sep 11 '12 at 11:31
0

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

Community
  • 1
  • 1
Abdullah
  • 9,005
  • 1
  • 23
  • 24