2013-08-31 2 views
0

다음은 서비스를 시작하기위한 코드입니다.서비스를 중지 할 수 없습니까?

내 활동의 onCreate()에서 서비스를 시작합니다.

Intent svc=new Intent(BeapSoundActivity.this, BackgroundService.class); 
       pendingIntent = PendingIntent.getService(BeapSoundActivity.this, 0, svc, 0); 

       AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

       Calendar calendar = Calendar.getInstance(); 
       calendar.setTimeInMillis(System.currentTimeMillis()); 
       calendar.add(Calendar.SECOND, 2); 
       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent); 

내가

stopService를 호출하여 서비스를 중지 할 수 캔트();

여기 내 서비스 클래스 : 나는 또한 나의들의 OnDestroy에서 stopSelf()()을 시도

public class BackgroundService extends Service{ 

// private static final String TAG = null; 
// MediaPlayer player; 
    MediaPlayer mMediaPlayer; 
    String strResponse; 
    int i=0; 

    static { 

//   StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

      StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build()); 
     } 
    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mMediaPlayer = new MediaPlayer(); 
     Log.e("Oncreate","oncreate"); 


    } 
    public int onStartCommand(Intent intent, int flags, int startId) { 
//  player.start(); 
     Log.e("Onstartcommand","onstartcommand"); 

      pendingCount(); 

     return 1; 
    } 

    public void onStart(Intent intent, int startId) { 
     // TO DO 
//  super.onStart(intent,startId); 



     Log.e("Onstart","onstart"); 
//  pendingCount(); 
    } 
    public IBinder onUnBind(Intent arg0) { 
     // TO DO Auto-generated method 
     return null; 
    } 

    public void onStop() { 

    this.stopSelf(); 


    } 
    public void onPause() { 

    } 
    @Override 
    public void onDestroy() { 

     super.onDestroy(); 

      this.stopSelf(); 

//  player.stop(); 
//  player.release(); 
    } 

    @Override 
    public void onLowMemory() { 

    } 


} 

    public void pendingCount(){ 

      my operations during the service 

       } 


    } 


} 

뿐만 아니라 나를 위해 일한 didnt한다.

어떻게이 문제를 해결할 수 있습니까?

+0

정적 블록이 필요합니다. –

+0

그냥 내 웹 서비스를 사용할 수없는 몇 번만,이 문제는, stopService() 호출 onDestroy 메서드를 어디에 this.stopSelf() 계속 호출 할 수 있지만 그것은 작동하지 않았다 – user2717010

+0

u는 익명 사용하여 모든 카운터 작업을하고 있습니다 클래스 또는 pendingCount() 내부의 다른 의미 ... 예, 그렇다면 솔루션을 알고 있다고 생각합니다. –

답변

0

다른 활동에서 stopService()으로 전화해야합니다. 또는 필요한 작업이 완료되었을 때 stopSelf() (onDestroy() 아님) 서비스에 전화 할 수 있습니다.

+0

예 stopService()를 호출했습니다. 사용자가 뒤로 버튼을 클릭 할 때 내가 서비스를 시작한 활동. – user2717010

+0

정적 블록을 제거해보십시오. 서비스를 유지하고있는 것 같습니다. 또한'onStartCommand'에서'2'를 리턴하고'onStart()'메소드를 제거하십시오. 또한'onDestroy()'의'stopSelf()'는 중복되어 있습니다. – sha256

0

이 코드를 사용하면 도움이됩니다.

stopService(new Intent(MainActivity.this,BackgroundService.class)); 
0

나는 당신의 stopservice()가 서비스하지만, 익명의 클래스 또는 정지 서비스가 파괴 된 경우에도 작동하고 이제까지 당신이 (pendingCount 내에서 사용을) 중지 잘 작동되는 것을 볼 수 있습니다 무엇 ... 그래서 어떤을 이

같은 것은들의 OnDestroy에서 플래그()를 상승 및 일부 루핑이 내부 pendingCount()을 확인하고이 플래그 인 경우 높은이 작업을 파괴한다. 더 정확한 답변을 위해 전체 pendingCount()를 추가하십시오. 코드

0

이전에 게시 한 answer을 확인하고이를 구현할 수 있습니다. 희망이 당신을 위해 작동합니다.

관련 문제