簡體   English   中英

當應用程序發送到后台時,如何在通知欄中使用MediaController?

[英]How can i use MediaController in the notification bar when app is sent to background?

所以我有:

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事件? 因此,如果我單擊播放(白色按鈕),則停止位置將變為灰色,並保持此顏色直到被按下為止,因此播放變為灰色。

在我的項目中,我這樣做是這樣的:

  • 我為通知定義了手動布局
  • 在布局中,我添加ImageButtons
  • 我為ImageButtons背景定義了一個selector (來源也可以)
  • 最后,我手動設置通知的布局給出這里
  • 當然,我還添加了一個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM