2017-06-09 89 views
0

此服務會導致設備在10秒後震動,但當活動關閉或應用程序從最近的應用程序中刪除時,服務會重新啓動。活動關閉時重新啓動服務

public class Vibrar extends Service { 

    @Override 
    public IBinder onBind(Intent p1) { 
     // TODO: Implement this method 
     return null; 
    } 

    int delay = 10000; //milliseconds 

    @Override 
    public void onCreate() { 
     // TODO: Implement this method 
     Toast.makeText(getApplicationContext(),"created",Toast.LENGTH_SHORT).show(); 
     super.onCreate(); 
     Vibrar(); 
    } 

    public void Vibrar() { 
     new Handler().postDelayed(new Runnable() { 
      public void run() { 
       Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
       v.vibrate(200); 
      } 
     }, delay); 
    } 
} 

我使用onStartComand法的return START_STICKSTART_NOT_STICK價值已經嘗試過,但它一直在重新啓動或立即終止。

即使應用程序已從最近的應用程序中刪除,我也希望它可以繼續停留。有沒有辦法做到這一點?

+0

你有多個活動或只有一個活動..? –

+0

而你只是希望一次只在該服務中振動,或者你想在服務中做更多的操作..? –

+0

僅一個活動,更多操作 – Mssjim

回答

0

您需要啓動和停止您的服務。

要啓動服務:

Intent intent_current = new Intent(activity,Vibrar.class); 
activity.startService(intent_current); 

當你需要它停下來:

Intent intent_current = new Intent(activity,Vibrar.class); 
activity.stopService(intent_current); 

重要提示:您的服務將運行就像一個獨立的活動,所以你需要確保你停止該服務,否則它將繼續運行,直到你用完內存。 您應該看看Activity life cycle

+0

在活動中,我**開始**和**停止**服務。唯一的問題是應用程序從最近的應用程序中刪除或活動結束。 – Mssjim

+0

這通常發生在您未啓動服務時。當你啓動服務時,它就像一個不同的活動。你使用onDestroy還是onPause? –

+0

是的,我使用'onDestroy'但裏面只有'super.onDestroy();' – Mssjim

0

您是否嘗試過使用報警管理器?它的主要目的是安排事件,以適應您的問題。 https://developer.android.com/training/scheduling/alarms.html請檢查此鏈接。

如果這不能解決您的解決方案,我有一個替代解決方案,它可以將最後一次振動時間保存到內部存儲,並檢查當前時間和節省的時間。

public class Vibrar extends Service { 
    private static final String TIME_KEY = "lastinsertedtime"; 
    @Override 
    public IBinder onBind(Intent p1) { 
     // TODO: Implement this method 
     return null; 
    } 

    int delay = 10000; //milliseconds 

    @Override 
    public void onCreate() { 
     // TODO: Implement this method 
     Toast.makeText(getApplicationContext(),"created",Toast.LENGTH_SHORT).show(); 
     super.onCreate(); 
     Vibrar(); 
    } 
    public long getLastVibrationTime(){ 
     SharedPreferences preferences = getApplicationContext().getSharedPreferences("INTERNAL_STORAGE_NAME",Context.MODE_PRIVATE); 
     return Long.parseLong(preferences.getString(TIME_KEY,"0")); 
    } 
    public void saveVibrationTime(){ 
     SharedPreferences preferences = getApplicationContext().getSharedPreferences("INTERNAL_STORAGE_NAME",Context.MODE_PRIVATE); 
     SharedPreferences.Editor editor = preferences.edit(); 
     editor.putString(TIME_KEY,String.valueOf(System.currentMillis())); 
     editor.commit(); 
    } 
    public void Vibrar() { 
     new Handler().postDelayed(new Runnable() { 
      public void run() { 
       if(System.currentMillis() - getLastVibrationTime() > delay){ 
        Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); 
        v.vibrate(200); 
        saveVibrationTime(); 
       } 

      } 
     }, 100);// check it for 100 ms 
    } 
} 
2

我見過很多編碼人員使用匿名處理程序時發生這種錯誤。

使用此:

Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 
      if(System.currentMillis() - getLastVibrationTime() > delay){ 
       Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE); 
       v.vibrate(200); 
       saveVibrationTime(); 
      } 
     } 
    }; 
    handler.postDelayed(runnable,10000); 

並在銷燬:

handler.removeCallbacks(runnable); 

這將確保當您退出該應用或服務的處理程序也將被取消。

希望這會有所幫助。

+0

中的** onClickListener按鈕**上啓動請標記答案正確,以便其他人可以直接參考。 –

+0

對不起,但我需要該服務才能繼續運行。問題是,如果服務啓動,並且從最近的應用程序中移除5秒鐘後,總共需要15秒才能振動10秒。 – Mssjim