2011-03-21 3 views
5

내가 나중에 바인딩 할 수있는 포 그라운드 서비스를 수행하는 올바른 방법은 무엇입니까? 전경 서비스를 만드는 방법을 보여주는 Android API 데모를 따라했습니다. 시작 서비스가 동시에 바인드 된 예는 없습니다.바인딩 할 수있는 Android 포 그라운드 서비스

나는 활동에 "묶인"음악 플레이어 서비스의 좋은 예를보고 싶다.

있습니까? 프로그램이 처음 나는 모든 일을 전경 서비스를 시작하려면 시작 (첫 번째 서비스는 아직 시작되지 않음을 의미) 때

  • :

    내가 좋아하는 일을하고 싶습니다. 사용자 인터페이스 (작업)은 해당 작업을 제어하는 ​​것입니다.

  • 사용자가 집 버튼을 누르면 서비스가 계속 실행되어야합니다 (바 알림이 있어야 함)
  • 알림 표시 줄에서 알림을 클릭하면 서비스 (또는 이와 비슷한 방식으로 올바른 방법)에 바인딩하면 사용자가 작업을 제어합니다.
  • 활동이 활성화되어 있고 사용자가 뒤로 버튼을 누르면 활동이 파괴되고 서비스가 파괴되어야합니다.

이 작업을 수행하려면 어떤 방법을 대체해야합니까? 이 작업을 수행하는 Android 방식은 무엇입니까?

답변

-3

그것은 그 것이었다 내 활동 정의

android:launchMode="singleTop" 

에의 AndroidManifest.xml에 다음과 같은 속성을 추가했다.

감사합니다.

10

전에 froyo가 설정 되었기 때문에 쉽게 사용할 수 있지만 악용하기 쉽습니다.

이제는 알림을 활성화해야하는 startForeGround 서비스가 있습니다 (사용자가 전경 서비스가 실행중인 것을 볼 수 있도록).

내가 그것을 제어하는이 클래스를 만든 :

내 미디어 플레이어이 업데이트 알림에 대한 다음
public class NotificationUpdater { 
    public static void turnOnForeground(Service srv,int notifID,NotificationManager mNotificationManager,Notification notif) { 
     try { 
      Method m = Service.class.getMethod("startForeground", new Class[] {int.class, Notification.class}); 
      m.invoke(srv, notifID, notif); 
     } catch (Exception e) { 
      srv.setForeground(true); 
      mNotificationManager.notify(notifID, notif); 
     } 
    } 

    public static void turnOffForeground(Service srv,int notifID,NotificationManager mNotificationManager) { 
     try { 
      Method m = Service.class.getMethod("stopForeground", new Class[] {boolean.class}); 
      m.invoke(srv, true); 
     } catch (Exception e) { 
      srv.setForeground(false); 
      mNotificationManager.cancel(notifID); 
     } 
    } 
} 

- 미디어를 재생하고 정지 이후에 남아 있어야하는 동안 전경 서비스를주의 만 필요하며,이를 나쁜 습관. 바인딩으로

private void updateNotification(){ 
    boolean playing = ((mFGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING) || 
         (mBGPlayerBean.getState()==MediaPlayerWrapper.STATE_PLAYING)); 
    if (playing) { 
     Notification notification = getNotification(); 
     NotificationUpdater.turnOnForeground(this,Globals.NOTIFICATION_ID_MP,mNotificationManager,notification); 
    } else { 
     NotificationUpdater.turnOffForeground(this,Globals.NOTIFICATION_ID_MP,mNotificationManager); 
    } 
} 

- 당신은

MediaPlayerService mpService=null; 
@Override 
protected void onEWCreate(Bundle savedInstanceState) { 
    Intent intent = new Intent(this, MediaPlayerService.class); 
    startService(intent); 
} 

@Override 
protected void onStart() { 
    // assume startService has been called already 
    if (mpService==null) { 
     Intent intentBind = new Intent(this, MediaPlayerService.class); 
     bindService(intentBind, mConnection, 0); 
    } 
} 

private ServiceConnection mConnection = new ServiceConnection() { 
    public void onServiceConnected(ComponentName className, IBinder service) { 
     mpService = ((MediaPlayerService.MediaBinder)service).getService(); 
    } 

    public void onServiceDisconnected(ComponentName className) { 
     mpService = null; 
    } 
}; 
을 당신이 어떤 서비스에 바인딩하는 것처럼 당신은 단지 bindService 통화를 (이 문제는 전경 날씨 여부 나던) ONSTART 활동의 일반적인 방법으로 결합 요청 작업 내가 할 수있는 유일한 일을 수행하기 위해
+0

"onewcreate"란 무엇인가요? 필기체 오류입니까 아니면 약간의 지식입니까? – pengwang

+0

oppps 미안하지만 onCreate - 예외보고를위한 내 자신의 래퍼가 표시됩니다. – siliconeagle

+0

나는 API 데모 데모와 같은 이상한 행동을 발견했다. API 데모 (API 버전 7)로 이동하여 서비스 시작 - 서비스 -> 서비스 -> 로컬 서비스 컨트롤러로 이동하십시오. 이제 notif의 알림. 바가 가동 중입니다. 이제 홈 버튼과 notif를 눌러주십시오. 서비스 알림 (샘플 로컬 서비스)을 누릅니다. 지금!실제로 돌아가려면 뒤로 버튼을 세 번 눌러야합니다. 왜 세계에서? !!? – zmeda

관련 문제