如何将媒体控制器按钮放在通知栏上?

我正在创建一个音乐播放器应用程序。我想在后台运行应用程序时在通知栏上显示媒体控制器。看起来像Google播放器。

这该怎么做?


素胚勾勒不出你
浏览 441回答 3
3回答

慕少森

从通知中返回/发送数据到您的原始活动;上面的示例所需的更改:NotificationPanel nPanel = new NotificationPanel(MyActivity)如何使用通知按钮来恢复与创建通知相同的堆栈和活动:1)确保“活动”未销毁(可选),更改“后退”按钮以将任务放在后面,但不要销毁它:@Override    void onBackPressed() {   Log.d("onBackPressed", "onBackPressed Called");   moveTaskToBack(true);}2)在清单中,将其添加到活动中:android:launchMode="singleTop"3)为您的Intent实例添加这些标志:(volume是Intent实例)Intent volume = new Intent(....);....volume.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);volume.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);并且您还可以添加以下内容(可选):volume.setAction(Intent.ACTION_MAIN);volume.addCategory(Intent.CATEGORY_LAUNCHER)4)在PendingIntent实例中,不要使用任何标志:PendingIntent btn1 = PendingIntent.getActivity(parent, 0, volume, 0);5)并通过onNewIntent(Intent intent)回调来捕获您的Activity中的意图:@Overrideprotected void onNewIntent(Intent intent) {    // TODO Auto-generated method stub    super.onNewIntent(intent);    setIntent(intent);    Log.i("onNewIntent", intent.toString());    // DEBUG - very useful    if (intent.getExtras() != null) {   // As the Intent we send back has extras, if it don't, it is a different Intent. it is possible to use TRY {} CATCH{} for this as well to find if Extras is NULL.        String tmp;        tmp = intent.getExtras().getString("DO");        if (tmp != null) {              if (tmp.equals("volume"))                Log.i("onNewIntent", "Volume");            else if (tmp.equals("stop"))                Log.i("onNewIntent", "Stop");            else                Log.i("onNewIntent", "Didnt capture the extras of the Intent - " + tmp);        } else {            Log.i("onNewIntent", "No new Intent");        }    }}
打开App,查看更多内容
随时随地看视频慕课网APP