2013-08-05 6 views
2

내 앱에 MediaPlayer을 사용하고 있습니다. 내 앱에 Ongoing Notification에 미디어 플레이어 컨트롤 (예 : 이전, 다음 및 중지 버튼)을 추가하여 사용자가 해당 컨트롤에 액세스하기 위해 앱에 갈 필요가 없도록했습니다. 플랫폼 4.x에서 잘 보입니다. 다음은 Android 4.2.2의 알림 스크린 샷입니다.Android : 맞춤 알림 모양 문제

Notification on Android 4.2.2

는하지만, 안드로이드 2.2, 그것은 다음과 같습니다 : 다음과 같이

Notification on Android 2.2

내 코드는 다음과 같습니다

private Notification generateNotification() { 
    Log.d(TAG, "generateNotification called"); 

    Intent actionIntent = new Intent(getApplicationContext(), 
      AartiActivity.class); 
    PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 
      0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    RemoteViews mNotificationView = new RemoteViews(getPackageName(), 
      R.layout.notification_view); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
      getApplicationContext()); 
    builder.setSmallIcon(R.drawable.icon); 
    builder.setContent(mNotificationView); 
    builder.setOngoing(true); 
    builder.setTicker("Aarti Sangrah"); 
    builder.setContentIntent(pi); 
    mNotificationView.setImageViewResource(R.id.imgAppIc, R.drawable.icon); 
    mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName); 

    return builder.build(); 
}// generateNotification 

그리고이 후, 나는 onPrepared()startForeground(1, generateNotification());라고 .

Remote Views은 API 레벨 1부터 사용할 수 있습니다. 또한 잘 지원됩니다. 나는 어디 론가 읽었는데, Honeycomb 이전에는 지원되지 않았다. 하지만 Android 2.x를 사용하는 여러 기기에서이 기능을 사용할 수 있습니다. 또한 이것을 확인하기 위해 안드로이드 2.2의 뮤직 플레이어 소스 코드를 here에서 살펴 봤습니다.

여기에 Music Player Service의 발췌 문장이 있습니다.

RemoteViews views = new RemoteViews(getPackageName(), 
       R.layout.statusbar); 
     views.setOnClickPendingIntent(R.id.btnTest, PendingIntent 
       .getActivity(getApplicationContext(), 0, 
         new Intent(getApplicationContext(), 
           MusicBrowserActivity.class), 
         PendingIntent.FLAG_UPDATE_CURRENT)); 
     views.setImageViewResource(R.id.icon, 
       R.drawable.stat_notify_musicplayer); 
     if (getAudioId() < 0) { 
      // streaming 
      views.setTextViewText(R.id.trackname, getPath()); 
      views.setTextViewText(R.id.artistalbum, null); 
     } else { 
      String artist = getArtistName(); 
      views.setTextViewText(R.id.trackname, getTrackName()); 
      if (artist == null || artist.equals(MediaStore.UNKNOWN_STRING)) { 
       artist = getString(R.string.unknown_artist_name); 
      } 
      String album = getAlbumName(); 
      if (album == null || album.equals(MediaStore.UNKNOWN_STRING)) { 
       album = getString(R.string.unknown_album_name); 
      } 

      views.setTextViewText(
        R.id.artistalbum, 
        getString(R.string.notification_artist_album, artist, 
          album)); 
     } 

     Notification status = new Notification(); 
     status.contentView = views; 
     status.flags |= Notification.FLAG_ONGOING_EVENT; 
     status.icon = R.drawable.stat_notify_musicplayer; 
     status.contentIntent = PendingIntent.getActivity(this, 0, 
       new Intent("com.android.music.PLAYBACK_VIEWER") 
         .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0); 
     startForeground(PLAYBACKSERVICE_STATUS, status); 

원격보기가이 코드에 사용되었습니다. 그것에는 2 개의 TextViews가 있었다. 버튼을 추가하여 코드를 수정하고 버튼 클릭시에도 액션을 수행했습니다. 모든 것이 모든 플랫폼에서 잘 작동했습니다.

같은 일, 제 신청서와 함께하고 싶습니다. 그러나 2.2에서, 내가 스크린 샷에서 보여준 것처럼 보입니다. 나는 그것이 하얀 색 텍스트와 버튼 때문에 생각했다. 버튼과 텍스트의 색을 바꾸려고 시도했지만, 행운은 없었다. 내가 아는 한, 내가 알아 낸 것만이 원격보기가 Android 2.2 (내 경우에는)에 부 풀리지 않습니다. Android 2.x 플랫폼에서 알림이 제대로 표시되지 않는 이유는 알 수 없습니다.

답변

2

내 문제가 해결되었습니다. 여기 내 해결책이있다.

private Notification generateNotification() { 
     Log.d(TAG, "generateNotification called"); 

     Intent actionIntent = new Intent(getApplicationContext(), 
       AartiActivity.class); 
     PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 
       0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     RemoteViews mNotificationView = new RemoteViews(getPackageName(), 
       R.layout.notification_view); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(
        getApplicationContext()); 
      builder.setSmallIcon(R.drawable.icon); 
      builder.setContent(mNotificationView); 
      builder.setOngoing(true); 
      builder.setTicker("Aarti Sangrah"); 
      builder.setContentIntent(pi); 
      mNotificationView.setImageViewResource(R.id.imgAppIc, 
        R.drawable.icon); 
      mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName); 
      mNotificationView.setTextColor(R.id.txtAartiPlaying, getResources() 
        .getColor(android.R.color.holo_orange_light)); 

      return builder.build(); 
     } else { 
      mNotificationView.setTextViewText(R.id.txtAartiPlaying, mediaName); 
      mNotificationView.setTextColor(R.id.txtAartiPlaying, getResources() 
        .getColor(android.R.color.holo_orange_light)); 
      Notification statusNotification = new Notification(); 
      statusNotification.contentView = mNotificationView; 
      statusNotification.flags |= Notification.FLAG_ONGOING_EVENT; 
      statusNotification.icon = R.drawable.icon; 
      statusNotification.contentIntent = PendingIntent.getActivity(this, 
        0, new Intent(getApplicationContext(), AartiActivity.class) 
          .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0); 
      return statusNotification; 
     } 
    }// generateNotification 

하지만 놀랍습니다. NotificationCompat.Builder은 지원 패키지로 제공되며 이전 버전과의 호환성을 위해 제공되었습니다. 하지만, 제 경우에는 Honeycomb 이상에서만 작동합니다. 이 section에 따라 :

는 모든 알림 기능은 방법이 그들을 지원 라이브러리 클래스 NotificationCompat.Builder에있는 설정에도 불구하고, 특정 버전을 사용할 수 있습니다. 예를 들어 확장 된 알림에 의존하는 작업 버튼은 Android 4.1 이상에서만 표시됩니다. 확장 된 알림 자체는 Android 4.1 이상에서만 사용할 수 있기 때문입니다.

하지만, 내 생각에는이 사실이 적용되지 않는다고 생각합니다. NotificationCompat.Builder는 원격보기에서 작동하지만 2.x에서는 작동하지 않았습니다 (적어도 제 경우에는 잘못되었거나 누락되었을 수 있습니다). 이런 점에서 정보 나 자료가있는 사람이 있다면 실수를 찾아 낼 수 있도록 공유하십시오.

+2

NotificationCompat는 모든 API 수준에서 작동하는 알림을 작성하는 데 도움이되지만 이전 버전의 새로운 기능을 사용할 수있는 것은 아닙니다.이전 버전에서는 맞춤 레이아웃이나 버튼을 처리 할 수 ​​없으므로 실제로는 알림 소비가 늘어 났고 새 알림이 확장되지 않았습니다 (아직 지출 할 수없는 API 수준 또는 사용자 또는 시스템에서 알림을 계약 한 경우) 및 오래된 통지 (단지 텍스트). 물론, 귀하의 최소 API 수준에 따라 다릅니다. minsdk = 14 (ICS)를 사용하기로 결정한 경우 이전 버전의 알림을 고려할 필요가 없습니다. – Teovald

+0

@Nishish가 나에게 제안을합니다. RemoteView를 사용하여이 링크와 같은 재생/일시 정지를 제어하는 ​​알림 큰보기를 만듭니다 (http://stackoverflow.com/questions/14508369/how-to-create-a-notification-similar-to-play-music-app-from). - google) 모두 괜찮아요.하지만 기기 뒤로 버튼을 클릭했을 때 응용 프로그램에서 클릭 이벤트 (재생/일시 중지/전달/닫기) 버튼이 작동하지 않습니다. 제발 도와주세요. –

+0

@HelalKhan 안녕하세요, 제발, Stackoverflow에 대한 새로운 질문을 만듭니다. 코드를 보지 않고서는 이유가 무엇인지 말할 수 없습니다. – Nitish