2016-04-13 25 views
0

我有一個計時器,顯示剩下多少時間讓用戶再次播放。 我意識到服務可以爲我做到這一點,所以我創建一個與START_STICKY標籤單獨的過程。並使用Receiver爲了得到結果..這工作正常。 但是當我關閉應用服務將被重新啓動,一切從開始時間開始...爲什麼我的計時器在關閉應用程序後重置?

的問題是:如何防止重新啓動該服務或其他? 方法來解決這個問題?!

ty.regards。

我的活動:

public class MainActivity extends Activity{ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

    } 
    @Override 
    protected void onDestroy() { 
     super.onDestroy();; 
    } 
    public static void StartService(Context context,int globalTime,int localTime){ 

    Intent i= new Intent(context,Services.class);  
    i.putExtra("value", localTime); 
    i.putExtra("value2", true); 
    context.startService(i); 
    } 

} 

我Reciver:

public class Reciver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

    int defaultValue = -1; 
    Toast.makeText(context,String.valueOf(intent.getIntExtra("value", defaultValue), Toast.LENGTH_SHORT).show(); 
    } 
} 

我的服務:

public class Services extends Service { 

int id; 
@Override 
public void onCreate(){ 
    alarm(); 
} 

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

    id = startId; 
    maxTimerValue = mainIntent.getIntExtra("value", 0); 

if(currentTimerValue < maxTimerValue){ 
     currentTimerValue++;  
     BroadCatster(); 
    }else{ 
     currentTimerValue = 0 
    } 
} 
} 
public void alarm(){ 

    Intent i = new Intent(this, Services.class); 
    PendingIntent pi = PendingIntent.getService(this, 0, i, 0); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, 1000, pi); 

} 

public void BroadCatster(){ 

    Intent intent = new Intent(this,Reciver.class); 
    intent.setAction("RECIVER_FILTER"); 
    intent.putExtra("value", currentTimerValue); 
    this.sendBroadcast(intent); 
} 
} 

}

MainFest

<receiver android:name="com.example.Reciver"> 
    <intent-filter> 
    <action android:name="RECIVER_FILTER" /> 
    </intent-filter> 
</receiver> 
<service 
    android:enabled="true" 
    android:exported="false" 
    android:name="com.example.Services" 
    android:label="@string/app_name" 
    android:process=":Service"> 
</service> 

回答

0

有防止服務被停止沒有完美的方法。 START_STICKY並不意味着Android不會終止該服務。如果資源緊縮,Android可能仍會終止服務,STICKY只會幫助操作系統重新啓動服務,一旦資源緊縮消失。

所以基本問題是,你假設服務永遠不會被殺死,你的時間保持依賴於此。最好將上次播放的時間存儲在首選項或SQLlite中(或將其放入onSaveInstance狀態中),並在每次處於onStartCommand時從中讀取它。當然,你需要在其中有上堆棧溢出的線程,你可以參考

1

您服務的onCreate

Intent notificationIntent = new Intent(this, MainActivity.class); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent, 0); 

Notification notification = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.mipmap.app_icon) 
      .setContentTitle("Your App") 
      .setContentText("") 
      .setContentIntent(pendingIntent).build(); 

startForeground(1111, notification); 
+0

yes startForeground或創建Toast總是阻止服務在單獨進程中運行時重新啓動服務。你的想法是工作,但我不想顯示通知becaouse總是在用戶的神經:( –

+0

這不會顯示通知,據我所知 – ljpv14

+0

Oo ... omg .. w8 plz –

0

添加該@ mr.bug我沒有處理用戶時間的變化關於'服務'的任何想法,但我可以建議你一個邏輯,你的計時器只需將開始時間保存在共享首選項或任何對你很方便的應用程序中,當你的應用程序重新開始時,就可以獲得開始時間和當前時間的差異。

+0

ty。是的,在我做這樣的事情之前。我保存了curent計時器,工作正常。但我來做的更好,看看你看到了什麼... –

相關問題