2014-11-11 4 views
4

나는 다시 시작 다음 코드는 서비스가 특정 작업에 파괴가 생성되면 이제 안드로이드 서비스를 다시 만드는 다시

Calendar cal = Calendar.getInstance(); 

         cal.add(Calendar.SECOND, 10); 

         Intent intent = new Intent(Formact.this, MyService.class); 


         MyService.pintent = PendingIntent.getService(Formact.this, 0, intent, 0); 


         MyService.alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
            MyService.alarm.setRepeating(AlarmManager.RTC_WAKEUP,cal.getTimeInMillis(), 5000, MyService.pintent); 

를 작성하지만 할 때마다 후이 파괴되어있는 버튼이 있습니다 . 는 여기에 내가 alarm.cancle 설정 한 내 서비스 클래스

public class MyService extends Service { 
public static int counter = 0; 
public static PendingIntent pintent; 
public static AlarmManager alarm; 
Boolean save=false; 
public MyService() { 

} 

@Override 
public IBinder onBind(Intent intent) { 
    return new Binder() ; 
} 
@Override 
public void onCreate() { 
    Toast.makeText(this, "Service was Created", Toast.LENGTH_SHORT).show(); 
    } 

@Override 
public void onStart(Intent intent, int startId) { 

    counter++; 
    Toast.makeText(this, " Service Started" + " " + counter, Toast.LENGTH_SHORT).show(); 


    SaveForm handler = new SaveForm(getApplicationContext()); 



    handler.setobj(getApplicationContext()); 

    handler.setText(Formact.sendform, Formact.listString); 

    handler.stratConnection(); 

    String m = ""; 
    int val = 0; 
    try{ 
     Log.e("val",SaveForm.msg); 
     if(SaveForm.msg!=null) 
     { 
     m=SaveForm.msg.substring(SaveForm.msg.length() - 1); 
     } 
     val=Integer.parseInt(m); 

     Log.e("val",m); 



    if(val>0) 
    { 
     Toast toast = Toast.makeText(getApplicationContext(), "Data saved", 100); 
     toast.show(); 
    save=true; 
     MyService.this.stopSelf(); 

    //  alarm.cancel(pintent); 



     if(alarm!=null) 
     { 
      try{ 
      alarm.cancel(pintent); 
      } 
      catch(Exception e) 
      { 
       Toast toasdst = Toast.makeText(getApplicationContext(), "Massi", 100); 
       toasdst.show(); 
      } 
      alarm=null; 
     }  
    } 
    } 

    catch(Exception e) 
    { 
     Toast toast = Toast.makeText(getApplicationContext(), "Data Not saved", 100); 
     toast.show(); 
     ///responseStr = responseStrr; 
    } 

} 

@Override 
public void onDestroy() { 
    Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show(); 
    super.onDestroy(); 



    if(save) 

    { 
     try{ 


      stopSelf(); 


     } 
     catch(Exception e) 
     { 
      Toast.makeText(this, "Head Bang", Toast.LENGTH_SHORT).show(); 
      super.onDestroy(); 
     } 
    } 

    } 
    } 

하지만 알람 이미 나는이

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    try{ 
     // String parameter = intent.getStringExtra("param_name"); 
     if(save){ 
      stopSelf(); 
     } 
    }catch(Exception ex){ 
    } 
    return startId; 
} 
시도했다

이기 때문에 예외가 발생합니다

하지만 서비스가 계속해서 다시 시작됩니다. 모든 것을 완벽하게 작동시키는 것보다 응용 프로그램을 닫지 않은 경우 한 가지 더 많은 일이 발생합니다. 그러나 응용 프로그램을 닫고 동일한 실행을 기대할 때 반복해서 생성되기 시작합니다.

도와주세요. 이 값은 널 (null) 응용 프로그램이 예외에 그것을 잡을 던질 것이다 때

+0

반복 모드로 서비스를 만들기 때문에 @setrepeat alarm이 매 5 초마다 호출됩니다. aproxx – koutuk

+0

추가 : MyService.alarm.cancel (MyService.pintent); –

+0

당신은 맞습니다 @ koutuk하지만 어떻게 그것을 파멸시킬 수 있습니까 –

답변

-1

하면 데이터가 저장됩니다 간단하게 한 번

handler.setText(Formact.sendform, Formact.listString); 

를 사용하여

Formact.sendform=null 
Formact.listString=null 

을 서버에 데이터를 만든다 저장되기 때문에 캐치 블록을 닫고이 줄을 사용하여 닫으십시오.

MyService.alarm.cancel(MyService.pintent); 
    MyService.this.stopService(); 

서비스가 다시 시작되지 않습니다. .

2

1.Do 문서에서, onStart() 사용하지 :

onStart(Intent intent, int startId) 
This method was deprecated in API level 5. Implement onStartCommand(Intent, int, int) instead. 

사용 onStartCommand 대신하고 return START_NOT_STICKY 또는 START_STICKY하지 startId.
2. bindService()을 사용하여 아무것도 바인딩하지 않은 경우 연결 handler.stratConnection();을 시작한 것 같습니다. unbindService(mConnection)을 사용하여 서비스에서 바인딩을 해제해야합니다. 문서에 주어진 : 당신은 아무것도 결합하지 않는 경우

Disconnect from an application service. You will no longer receive calls as the service is restarted, and the service is now allowed to stop at any time. 

다음 onBind()null을 반환합니다. 이 같은

@Override 
public IBinder onBind(Intent intent) { 
    return null; 
} 

3. super.onDestroy() :

@Override 
public void onDestroy() { 
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show(); 
super.onDestroy(); 
/***** No need to put this as it is already going to be destroyed 
if(save) 

{ 
    try{ 


     stopSelf(); 


    } 
    catch(Exception e) 
    { 
     Toast.makeText(this, "Head Bang", Toast.LENGTH_SHORT).show(); 
     super.onDestroy(); 
    } 
} 

} 
*****/ 
} 

4. stopSelf()를 호출하기 전에 alarm.cancel(pintent)하여 알람을 취소합니다.

if(alarm!=null) 
    { 
     try{ 
     alarm.cancel(pintent); 
     } 
     catch(Exception e) 
     { 
      Toast toasdst = Toast.makeText(getApplicationContext(), "Massi", 100); 
      toasdst.show(); 
     } 
     alarm=null; 
    } 
MyService.this.stopSelf(); 

5. 당신은 그것을 초기화하지 않고 alarmpintent을 사용하고 있습니다. 코드에서 초기화되지 않습니다. 6.context.stopService(intent)을 사용하여 Intent intent = new Intent(Formact.this, MyService.class);과 동일한 의도로 활동중인 서비스를 중지하십시오.

관련 문제