2017-09-25 21 views
-1

백그라운드에서 실행중인 IntentService에 이미지 업로드 api를 호출하고 이미지를 서버에 업로드합니다.앱이 안드로이드에서 죽은 경우에도 이미지를 업로드하는 방법

IntentService, onHandleEvent 메서드가 백그라운드에서 호출되고 실행되면 IntentService은 작업을 실행하고 stopSelf() 메서드를 호출합니다.

내 앱에서 내 앱을 죽일 때 업로드가 진행 중일 때 업로드가 종료되고 IntentService이 업로드 작업을 마쳤습니다.

어떻게 응용 프로그램이 종료 되더라도 IntentService을 실행할 수 있습니까?

편집 1 : 나는 스티커 서비스, 나는 서비스가 다시 시작 응용 프로그램과 onStartCommand 메서드에 전달 의도 데이터를 죽일 때 코드 아래에이 시도 할 수

+0

끈끈한 서비스를 사용합니다. –

답변

0

널을 사용했습니다. 당신이 매니페스트 파일에

<service 
     android:name=".service.Service" 
     android:enabled="true" 
     android:icon="@drawable/ic_launcher" 
     android:isolatedProcess="true"> 

    </service> 

을 서비스의 속성을 추가 또한 서비스에 START_STICKY를 추가 할 필요가 우선.

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    return START_STICKY; 
} 
0

서비스 onStartCommand 방법 반환 START_STICKY에서 모든 시간

1)를 실행하는 서비스를 유지하기 위해 아래 단계에서 언급 한 바와 같이 당신은 서비스를 만들 수 있습니다.

public int onStartCommand(Intent intent, int flags, int startId) { 
return START_STICKY; 
} 

2)는 관계없이 항상 경계 클라이언트의 수의 활성 상태를 유지하도록 startService (이면 MyService)를 사용하여 백그라운드에서 서비스를 시작합니다.

Intent intent = new Intent(this, PowerMeterService.class); 
startService(intent); 

3) 바인더를 만듭니다.

public class MyBinder extends Binder { 
public MyService getService() { 
     return MyService.this; 
} 
} 

4) 서비스 연결을 정의하십시오.

private ServiceConnection m_serviceConnection = new ServiceConnection() { 
public void onServiceConnected(ComponentName className, IBinder service) { 
     m_service = ((MyService.MyBinder)service).getService(); 
} 

public void onServiceDisconnected(ComponentName className) { 
     m_service = null; 
} 
}; 

5) bindService를 사용하여 서비스에 바인딩합니다.

Intent intent = new Intent(this, MyService.class); 
bindService(intent, m_serviceConnection, BIND_AUTO_CREATE); 
+0

끈적 끈적한 시작, 앱을 죽인 후에 서비스에 의도 데이터를 보냅니 까? 서비스를 다시 시작하는 방법은 무엇입니까? – Praneeth