为什么我的计时器在关闭应用程序后被重置?
Why my timer be reset after closing application?
我有一个计时器,可以显示还剩多少时间允许用户再次播放。
我意识到服务可以为 me.so 做到这一点,我在 单独的进程 中创建了一个带有 START_STICKY 标签的服务。并使用 Receiver 来获取结果……是否可以正常工作。 但是当我关闭应用程序服务时将重新启动并且一切都从头开始...
Question is : How do I prevent to the restart the service or another?
way to fix this problem ?!
ty.regards.
my activity:
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);
}
}
my 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();
}
}
my Service:
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>
没有完美的方法可以防止您的服务被停止。 START_STICKY 并不意味着 Android 不会终止服务。 Android 如果存在资源紧缩,可能仍会终止服务,STICKY 只会有所帮助,而不是 OS 可能会在资源紧缩消失后重新启动服务。
所以最基本的问题是你假设服务永远不会被终止,而你的时间保持依赖于此。最好将上次播放时间存储在首选项或 SQLlite 中(或将其放在 onSaveInstance 状态的捆绑包中),并在每次进入 onStartCommand 时从那里读取它。当然你需要处理用户时间的变化,这上面有很多线程堆栈溢出,你可以参考
将此添加到您服务的 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);
@mr.bug 我对 'service' 没有任何想法,但我可以建议您为您的计时器制定一个逻辑,只需将开始时间保存在共享偏好或任何您方便的地方,当您的应用程序再次启动时,该时间取开始时间和当前时间的差异。
我有一个计时器,可以显示还剩多少时间允许用户再次播放。 我意识到服务可以为 me.so 做到这一点,我在 单独的进程 中创建了一个带有 START_STICKY 标签的服务。并使用 Receiver 来获取结果……是否可以正常工作。 但是当我关闭应用程序服务时将重新启动并且一切都从头开始...
Question is : How do I prevent to the restart the service or another? way to fix this problem ?!
ty.regards.
my activity:
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);
}
}
my 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();
}
}
my Service:
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>
没有完美的方法可以防止您的服务被停止。 START_STICKY 并不意味着 Android 不会终止服务。 Android 如果存在资源紧缩,可能仍会终止服务,STICKY 只会有所帮助,而不是 OS 可能会在资源紧缩消失后重新启动服务。
所以最基本的问题是你假设服务永远不会被终止,而你的时间保持依赖于此。最好将上次播放时间存储在首选项或 SQLlite 中(或将其放在 onSaveInstance 状态的捆绑包中),并在每次进入 onStartCommand 时从那里读取它。当然你需要处理用户时间的变化,这上面有很多线程堆栈溢出,你可以参考
将此添加到您服务的 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);
@mr.bug 我对 'service' 没有任何想法,但我可以建议您为您的计时器制定一个逻辑,只需将开始时间保存在共享偏好或任何您方便的地方,当您的应用程序再次启动时,该时间取开始时间和当前时间的差异。