当应用程序发送到后台时,如何在通知栏中使用MediaController?

时间:2014-06-20 09:51:25

标签: android broadcastreceiver media-player

所以我有:

AudioPlayerBroadcastReceiver Receiver = new AudioPlayerBroadcastReceiver();
         IntentFilter filter = new IntentFilter();
         filter.addCategory(Intent.CATEGORY_DEFAULT);              
         filter.addAction("aClass.ACTION_PLAY");
         filter.addAction("aClass.ACTION_PAUSE");
        registerReceiver(Receiver,filter);

Intent PlayIntent = new Intent("aClass.ACTION_PLAY");
        PendingIntent PlaypendingIntent = PendingIntent.getBroadcast(this, 100, PlayIntent, 0);

ImageView playb = (ImageView)this.findViewById(R.id.play_button);    
        notificationView.setOnClickPendingIntent(R.id.play_button, PlaypendingIntent);

同样的停止;

  RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.media_player_in_notificationBar);

如何添加播放和停止按钮(ImageViews),或者如何使用onClick事件?因此,如果我单击播放(白色按钮) - 停止变为灰色并保持此颜色,直到它被按下,因此播放变为灰色。

1 个答案:

答案 0 :(得分:0)

在我的项目中,我这样做:

  • 我定义了通知的手动布局
  • 在布局中,我添加了ImageButtons
  • 我为selector的背景定义ImageButtons(对于来源也是可能的)
  • 最后,我手动将通知的布局设置为给定here
  • 当然,我还添加了一个PendingIntent但是你已经这样做了,我在这里不详细说明

xml-layout-file包含ImageButtons

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp" <!-- This is where I manually define the height -->
    android:orientation="horizontal" >

        <ImageButton 
            android:id="@+id/big_notification_cancel_button"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:background="@drawable/notification_button_background_selector"
            android:src="@drawable/some_image_with_transparent_background"
            />
    <!-- some more elements.. --> 
</LinearLayout>

notification_button_background_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:state_pressed="true"
    android:drawable="@drawable/white_semi_opaque_background"/> 

<item
    android:drawable="@drawable/transparent_background"/>

</selector>

我不确定这是否真的是你正在寻找的答案。希望它有所帮助。

修改 我也使用了ImageView的Image更改。收到点击后,您必须更新视图。 在我更新通知的类中,我这样做:

RemoteViews smallViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.notification_layout_big);

if(playing){
    smallViews.setImageViewResource(R.id.big_notification_button_play_pause, R.drawable.notification_small_pause_button_selector);
}else{
    smallViews.setImageViewResource(R.id.big_notification_button_play_pause, R.drawable.notification_small_play_button_selector);
}
// and some more stuff