2014-04-17 1 views
2

Spotify 안드로이드 애플 리케이션은 음악 컨트롤이 알림 (재생/일시 중지, 다음 노래)에 넣어 있습니다.안드로이드에서 알림 안에 컨트롤 넣기

사용자가 제어 내에서 제어 내에서 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> 

이제 자바 코드 :

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

안녕하세요 Sunny는 동적으로 런타임시 수행 방법을 보여 주시겠습니까? 마찬가지로 RelativeLayout을 동적으로 만들 수 있습니까? – Noitidart

+1

안녕하세요 Noitidart, XML에서 컨테이너 레이아웃을 정의하고 나중에 Java 파일에서 LayoutInflater를 사용하여 레이아웃을 확장하여 XML 파일에 정의 된 컨테이너에 추가 할 수 있습니다. –

+0

대단히 고맙습니다. 저는 Java에 매우 익숙합니다. 지금은 Java에서 JNI로 포팅하는 시작 단계에 있습니다. 별도의 질문을해도 괜찮을까요? 고마워요. 스푼 푸딩이라고 알고 있는데, 이것이 제가 가장 잘 배웁니다. – Noitidart

0

NotificationCompat.Builder.addAction()

이 통지에 작업을 추가합니다. 조치는 일반적으로 통지 내용에 인접한 단추로 시스템에 표시됩니다. Android 4.1 이전 버전의 플랫폼에는 작업 버튼이 표시되지 않습니다.

+0

Android 4.1 이하 버전에서 비슷한 것을 추가 할 수 있습니까? – syntagma

+0

사용자 정의 알림 레이아웃을 만들어야하는 것처럼 보입니다. –