안드로이드 개발 질문/답변
(글 수 45,052)
백그라운드로 돌면서 서버에서 보내는 메시지를 보여주는 프로그램을 만들고 있어요.
앱이 실행될 때와 폰이 부팅될 때 서비스도 같이 시작을 해야해요.
일단 application을 한개로 써버리면 어플을 종료할 때 서비스도 같이 죽더라구요 ㅠㅠ
그래서 조언을 구했더니 android:sharedUserId, android:process 를 이용하면 같이 살고 따로 죽는다해서 해봤는데,
이렇게 에러가 떠버리네요 ㅠㅠ
restartPackage가 너무 강력해서라고 어디선가 찾았던것 같은데, 저건 어쩔수가 없는 상태고요...ㅠㅠ
며칠 째 검색을 하고 있는데, 해결을 못하고 있네요 ㅠㅠ
어디 좋은 방법 없나요?
폰은 갤럭시s를 사용하고 있어요~
앱이 실행될 때와 폰이 부팅될 때 서비스도 같이 시작을 해야해요.
AndroidManifest.xml
// AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="android.uid.aaa">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true"
android:process="android.process.bbb">
<activity android:name=".receive.andro2"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:process="android.process.bbb">
<receiver android:name=".receive.MessageReceive">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
<service android:name=".receive.MessageService">
<intent-filter>
<action android:name="MessageService" />
</intent-filter>
</service>
<activity android:name=".receive.showAlert" android:configChanges="orientation"
android:theme="@android:style/Theme.Dialog"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.AlertDialogs" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
// 서비스 시작 getApplicationContext().startService(new Intent(this, MessageService.class));
// 어플 종료 ActivityManager manager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE); manager.restartPackage(activity.getPackageName());
일단 application을 한개로 써버리면 어플을 종료할 때 서비스도 같이 죽더라구요 ㅠㅠ
그래서 조언을 구했더니 android:sharedUserId, android:process 를 이용하면 같이 살고 따로 죽는다해서 해봤는데,
Unable to start service Intent { cmp=com.test/.receive.MessageService }: not found
이렇게 에러가 떠버리네요 ㅠㅠ
restartPackage가 너무 강력해서라고 어디선가 찾았던것 같은데, 저건 어쩔수가 없는 상태고요...ㅠㅠ
며칠 째 검색을 하고 있는데, 해결을 못하고 있네요 ㅠㅠ
어디 좋은 방법 없나요?
폰은 갤럭시s를 사용하고 있어요~
2010.10.01 15:59:24
서비스 생명주기
시스템이 서비스를 실행시키는 두가지 경우가 있다.
안드로이드 애플리케이션에서 Context.startService()를 호출하면 시스템은 서비스를 생성한다.(필요하다면 서비스의 onCreate() 메서드를 실행한다.) 그리고 시스템은 클라이언트에서 전달한 인자와 함께 서비스의 onStartCommand(Intent, int, int) 메서드를 실행한다. 이렇게 실행된 서비스는 Context.stopService() 또는
stopSelf()가 호출될 때까지 계속 동작한다.
Context.startService()를 여러번 호출해도 중첩되지 않는다.
(비록 서비스와 일치하는 onStartCommand()가 여러번 호출된지라도).
따라서서비스가 시작되는 횟수에 관계없이 일단 Context.stopServce() or stopSelf()를 호출하면 서비스는멈춘다. 그러나 서비스는 시작시에 전달된 인텐트가 처리될 때까지는 서비스를 종료하지 않기 위해 stopSelf(int) 메서드를 이용할 수 있다.시작된 서비스에 대해서 onStartCommand() 함수의 리턴 값에 따라,
서비스가 어떻게 동작해야 하는지를 결정하는 두가지 추가 모드가 있다.
START_STICKY 모드 : 명시적으로 서비스가 시작되고, 필요하면 종료되는 모드
START_NOT_STICKY 모드 또는
START_REDELIVER_INTENT 모드 : 서비스가 받은 어떤 명령을 처리하는 동안에는 서비스의 실행이 유지되어야 하는 경우에 이용
클라이언트는 동작 중인 서비스와의 지속적인 커넥션을 얻기 위해서 Context.bindService()를 이용한다. 이 메서드는 만약 연결하려는 서비스가 동작하지 않고 있다면 서비스를 먼저 실행한다.(이때 서비스 생성 시 onCreate()가 호출되지만, onStartCmmand()는 호출되지 않는다). 클라이언트는 서비스의 onBind(Intent) 메서드로부터 리턴되는 IBinder 객체를 얻는다. 이 객체를 통해 클라이언트는 서비스의 기능을 호출할 수 있다.
서비스는 연결이 유지되어 있는 한 계속 동작한다 (클라이언트가 서비스의 IBinder 객체의 레퍼런스를 얻는 지에 대해 상관없이). 보통 리턴된 IBinder객체는 AIDL에 의해 작성된 복잡한 인터페이스를 위한 것이다.서비스는 시작함과 동시에 자신과 바인딩된 커넥션을 가질 수 있다. 이와 같은 경우 서비스가 동작하거나 또는 Context.BIND_AUTO_CREATE 플래그로 서비스와 바인딩된 하나 이상의 커넥션이 존재할 경우 시스템은 서비스를 항상 동작시킨다. 만약 위의 두 경우가 아닌 서비스의 경우는 서비스의 onDestroy() 메서드가 호출되고 서비스는 종료한다. 모든 리소스 해제(스레드 종료, 리시버 제거)는 onDestory() 함수 실행 후 완료된다.
--------------------------------
http://kaludin.egloos.com/2653128 여기에 번역되어있어서 올립니다.
근데 문제는 역시 안되네요 ㅠㅠ
분명 제가 잘못해서일텐데.......ㅠㅠ
안드로이드2.1인 갤럭시S에서는 안되고, 2.2의 에뮬이나 2.2의 evo에서는 잘되네요 ㅠㅠ




Service Lifecycle
There are two reasons that a service can be run by the system. If someone calls
Context.startService()then the system will retrieve the service (creating it and calling itsonCreate()method if needed) and then call itsonStartCommand(Intent, int, int)method with the arguments supplied by the client. The service will at this point continue running untilContext.stopService()orstopSelf()is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use theirstopSelf(int)method to ensure the service is not stopped until started intents have been processed.For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand():
START_STICKYis used for services that are explicitly started and stopped as needed, whileSTART_NOT_STICKYorSTART_REDELIVER_INTENTare used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.이 부분에 답이 있지 않을까요
영어가 딸려서 번역은 직접...