gpt4 book ai didi

java - 静态变量没有正确返回?

转载 作者:行者123 更新时间:2023-11-30 09:32:55 25 4
gpt4 key购买 nike

我正在构建一个小应用程序,它现在可以很好地处理服务和 Activity 。

不过,我试图在登录时将一些静态信息(比如服务是否已经启动?)保存到静态 boolean 值 isRunning 中。它会在 onCreate() 时设置为 true,但当我稍后从 Activity 中调用它时,它总是返回 false。

来自服务:

public static boolean isRunning = false;

public void onCreate() {
super.onCreate();
isRunning = true;
}

有谁知道为什么这不起作用?我试过使用一些日志来弄清楚发生了什么,但我似乎无法弄清楚。

来自 Activity

public void onResume() {
super.onResume();
if(mIsBound) {
Log.i(LOG_TAG, "Resuming: Service is running");
if(Service.isRunning) {
Log.e(LOG_TAG, "SERVICE IS RUNNING!");
} else {
Log.e(LOG_TAG, "SERVICE IS NOT RUNNING!");
}
} else {
Log.i(LOG_TAG, "Resuming: Service NOT running");
}
StopCheck.setChecked(mIsBound);
}

mIsBound 是由绑定(bind)到服务的 Activity 创建的(我希望它重新绑定(bind),但这似乎是不可能的),并且它在当前状态下是可靠的。但不是在该 Activity 之外,这就是我想使用静态变量的目的。如果 mIsBound 等于 true,则 Service.isRunning 应返回 true。然而,我日志中那个小测试的结果是“正在恢复:服务正在运行”,然后是“服务未运行”。

非常感谢任何建议或问题。

根据要求:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.somnu.ServiceTest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="7" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Login"
android:debuggable="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity"
android:debuggable="true"
android:label="@string/app_name" >
</activity>

<service
android:name=".Service"
android:process=":Service" >
</service>

</application>

</manifest>

最佳答案

移除android:process

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage. 

关于java - 静态变量没有正确返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12424776/

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