2014-05-24 4 views
0

메신저 내 고등학교 최종 안드로이드 프로젝트를위한 간단한 매크로 안드로이드 응용 프로그램을 만들고 난 (사용자가 결정한) 일정한 시간마다 실행되는 백그라운드 프로세스를 만들어 현재 위치 및 기타 몇 가지를 업데이트합니다 밝기를 낮추거나 스마트 폰을 자동 모드 또는 기타 몇 가지 동작으로 변경하는 것과 같은 작업을 시작하기위한 조건으로 .... 그 프로세스 (또는 서비스)를 만드는 방법 및 전체 초보자임을 명심하십시오. 나는 심지어 프로세스 나 서비스의 차이점을 모른다.) .. 가능한 경우 예제 코드로 대답 해주십시오. 감사합니다 ! : D배경 프로세스/서비스

답변

0

주기적으로 업데이트/모니터링/기타 작업을 수행하려면 앱에서 서비스를 시작해야합니다. 길고 상세한 하나

http://www.basic4ppc.com/android/forum/threads/creating-a-sticky-service-long-running-background-tasks.27065/

그리고 여기에 있습니다 : : 여기에 대한 간략한 설명입니다에 따라,도

http://developer.android.com/guide/components/services.html

당신은 아마 전경 서비스를 필요로하지만, 끈적 작동 할 수 특정 요구 사항.

<service android:name="Foo"/> 

는 서비스 클래스를 확장 :

이의 AndroidManifest.xml에서 서비스를 선언 : 여기 (첫 번째 링크에 설명 된 방식과 조금 다른,하지만 같은 일반적인 생각)로 시작하는 몇 가지 기본적인 코드는 당신의 주요 활동의에서 onCreate 방법에서

public class FooService extends Service { 

    .......... 

     @Override 
    public void onCreate() { 

     //if you want this service to run in the foreground so it doesn't get 
      //killed by the system, create a notification, and call this 
      startForeground(id, notification); //look at the Service API 

      //start your periodic monitoring thread 
      ................ 

     } 

     @Override 
    public void onDestroy() { 

      //do the cleanup here 
      ................ 

    } 


    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

      //whatever you need to do when the app calls the start command, 
      //which may happen multiple times, goes here 
      ............... 

      //this makes sure the process is sticky (see links above for details 
      return START_STICKY; 
    } 


     .......... 


    } 

은 이렇게 :

Context context = getApplicationContext(); 
    Intent i= new Intent(context, FooService.class); 
    i.putExtra(whetever you want to pass to the service) 
    context.startService(i); 
    context.bindService(i, connection, 0); //read the API