Reputation: 3891
I'm building a little app, that's working quite nicely with a service and activity right now.
Although, I'm trying to save some static information upon login (like has the service been started already?) into a static boolean, isRunning. It will set to true upon onCreate(), but when I call it later from the activity, it always returns false.
From the service:
public static boolean isRunning = false;
public void onCreate() {
super.onCreate();
isRunning = true;
}
Does anyone know why this doesn't work? I've tried using some logs to figure out what's happening but I can't seem to figure it out.
From the 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);
}
The mIsBound is what is being created by the activity to bind to the service (I wanted it to rebind, but that seems to be impossible), and it's reliable in its current state. But not outside of that activity, that's what I want to use the static variable for. The Service.isRunning should return true, if mIsBound equals to true. Yet the result of that little test in my log is "Resuming: Service is running" followed by "SERVICE IS NOT RUNNING".
Any suggestions or questions are very much appreciated.
AS REQUESTED: 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>
Upvotes: 2
Views: 231
Reputation: 19185
Remove 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.
Upvotes: 2