gpt4 book ai didi

android - 如何检查服务是否正在运行

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:59 27 4
gpt4 key购买 nike

<分区>

我想显示服务状态,是运行还是停止。我正在使用下面的代码,但它在开始服务之前显示“已停止”。服务启动时显示“正在运行”。当它再次停止时,它只显示“正在运行”。我在设置 sharedPreference 状态时犯了什么错误吗? 在主 Activity 中

  onCreate()
{
checkServiceStatus() ;
}

private void checkServiceStatus()
{
Toast.makeText(getApplicationContext(),"in enableControls", Toast.LENGTH_LONG).show();
boolean isServiceRunning = AppSettings.getServiceRunning(this);
if (isServiceRunning)
{
Toast.makeText(getApplicationContext(),"service running", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"service stopped", Toast.LENGTH_LONG).show();
}
}


public class AppSettings
{
public static final String SERVICE_STATE = "isServiceRunning";
public static boolean getServiceRunning(Context context){
SharedPreferences pref = context.getSharedPreferences(GPSLOGGER_PREF_NAME, 0);

return pref.getBoolean(SERVICE_STATE, false);
}

public static void setServiceRunning(Context context, boolean isRunning){
SharedPreferences pref = context.getSharedPreferences(GPSLOGGER_PREF_NAME, 0);
SharedPreferences.Editor editor = pref.edit();

editor.putBoolean(SERVICE_STATE, isRunning);
editor.commit();
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com