2014-04-17 19 views
2

Spotify Android應用程序將音樂控件放入通知(播放/暫停,下一首歌曲)中。將控件放入Android通知中

我想讓用戶從控件裏面的通知中控制Service。我怎麼做?

enter image description here

回答

3

爲此,您需要爲通知自己的佈局。

你可以試試下面的代碼。

首先爲您的通知創建一個xml。

custom_notification.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp" > 
    <ImageView android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10dp" /> 
    <TextView android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     style="Custom Notification Title" /> 
    <TextView android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/image" 
     android:layout_below="@id/title" 
     style="Custom Notification Text" /> 
</RelativeLayout> 

現在的Java代碼:

public class MainActivity extends Activity { 

    @SuppressWarnings("deprecation") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     int icon = R.drawable.ic_launcher; 
     long when = System.currentTimeMillis(); 
     Notification notification = new Notification(icon, "Custom Notification", when); 

     NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 

     RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
     contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher); 
     contentView.setTextViewText(R.id.title, "Custom notification"); 
     contentView.setTextViewText(R.id.text, "This is a custom layout"); 
     notification.contentView = contentView; 

     Intent notificationIntent = new Intent(this, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     notification.contentIntent = contentIntent; 

     notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification 
     notification.defaults |= Notification.DEFAULT_LIGHTS; // LED 
     notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration 
     notification.defaults |= Notification.DEFAULT_SOUND; // Sound 

     mNotificationManager.notify(1, notification); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 
+0

嗨陽光明媚的可能請你展示如何在運行時與動態?我可以動態創建RelativeLayout嗎? – Noitidart

+1

Hi Noitidart,你可以通過在XML中定義一個容器佈局,然後在Java文件中定義容器佈局,你可以使用LayoutInflater來擴充佈局,將它添加到之前在XML文件中定義的容器。 –

+0

非常感謝Sunny我對Java非常陌生,所以現在我只是處於從Java到JNI的移植的初始階段,我可能會問一個單獨的問題,你會對此表示滿意嗎?謝謝,我知道這是勺子餵養,但這是我學習最好的方式。 – Noitidart

0

NotificationCompat.Builder.addAction()

添加操作此通知。操作通常由系統顯示爲與通知內容相鄰的按鈕。 動作按鈕不會出現在Android 4.1之前的平臺上。

+0

有沒有什麼辦法可以在Android 4.1以下的版本中添加類似的東西? – syntagma

+0

看起來您必須創建自定義通知佈局。 –