Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put media controller button on notification bar?

I am creating a music player application. I want to show the media controller on notification bar while my application is running in background. It looks like Google player.

enter image description here

How to do this?

like image 459
Leap Bun Avatar asked Sep 21 '12 07:09

Leap Bun


People also ask

How do I show music player in notification bar?

Head over to your notification settings and ensure that your media app has not been turned off. You may need to select see all apps. Once you have turned it on, back out of settings. I didn't see it right away until I locked/unlocked fold.

How do you add a notification bar?

You can find the gear icon in the Quick Menu panel by swiping down from the top of your screen or you can find the app icon on one of your Home screens, in the app drawer, or by searching. Tap Notifications. You may see a menu called "Apps & Notifications" instead, and then you'll need to tap "Notifications" again.

How do I access the notification bar on my Android?

The Notification Panel is at the top of your mobile device's screen. It is hidden in the screen but can be accessed by swiping your finger from the top of the screen to the bottom. It is accessible from any menu or application.


1 Answers

Here is the example above done correctly to the new API

In your main, when you want to start a notification instantiate the class:

NotificationPanel nPanel = new NotificationPanel(MyActivity); 

And when you want to cancel notification: (as it is an onGoing notification)

nPanel.notificationCancel();     

Then create the class for the notification caller:

public class NotificationPanel {  private Context parent; private NotificationManager nManager; private NotificationCompat.Builder nBuilder; private RemoteViews remoteView;  public NotificationPanel(Context parent) {     // TODO Auto-generated constructor stub     this.parent = parent;     nBuilder = new NotificationCompat.Builder(parent)     .setContentTitle("Parking Meter")     .setSmallIcon(R.drawable.ic_launcher)     .setOngoing(true);      remoteView = new RemoteViews(parent.getPackageName(), R.layout.notificationview);      //set the button listeners     setListeners(remoteView);     nBuilder.setContent(remoteView);      nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);     nManager.notify(2, nBuilder.build()); }  public void setListeners(RemoteViews view){     //listener 1     Intent volume = new Intent(parent,NotificationReturnSlot.class);     volume.putExtra("DO", "volume");     PendingIntent btn1 = PendingIntent.getActivity(parent, 0, volume, 0);     view.setOnClickPendingIntent(R.id.btn1, btn1);      //listener 2     Intent stop = new Intent(parent, NotificationReturnSlot.class);     stop.putExtra("DO", "stop");     PendingIntent btn2 = PendingIntent.getActivity(parent, 1, stop, 0);     view.setOnClickPendingIntent(R.id.btn2, btn2); }  public void notificationCancel() {     nManager.cancel(2); } }     

Then add the return class that accept the pending intent:

public class NotificationReturnSlot extends Activity {  @Override protected void onCreate(Bundle savedInstanceState) {     // TODO Auto-generated method stub     super.onCreate(savedInstanceState);     String action = (String) getIntent().getExtras().get("DO");     if (action.equals("volume")) {         Log.i("NotificationReturnSlot", "volume");         //Your code      } else if (action.equals("stopNotification")) {          //Your code         Log.i("NotificationReturnSlot", "stopNotification");      }      finish();     } } 

Then you need to make a XML file for the button. This is a simple one:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" >  <Button     android:id="@+id/btn1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_margin="10dp"     android:text="volume" />  <Button     android:id="@+id/btn2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_margin="10dp"     android:text="Stop" />  <TextView     android:id="@+id/message"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_toRightOf="@+id/msglbl" /> 

Last and not least, the Manifest file:

   <activity android:name=".NotificationReturnSlot"         android:launchMode="singleTask"         android:taskAffinity=""         android:excludeFromRecents="true"/> 
like image 140
Gilco Avatar answered Sep 22 '22 17:09

Gilco