私は小さなアプリを構築しています。これは現在、サービスとアクティビティで非常にうまく機能しています。
ただし、ログイン時にいくつかの静的情報 (サービスは既に開始されているかなど) を静的ブール値 isRunning に保存しようとしています。onCreate() で true に設定されますが、後でアクティビティから呼び出すと、常に false が返されます。
サービスから:
public static boolean isRunning = false;
public void onCreate() {
super.onCreate();
isRunning = true;
}
これが機能しない理由を誰かが知っていますか?何が起こっているのかを把握するためにいくつかのログを使用しようとしましたが、把握できないようです。
活動から
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 は、サービスにバインドするアクティビティによって作成されているものであり (再バインドしたかったのですが、それは不可能のようです)、現在の状態では信頼できます。しかし、そのアクティビティの外ではありません。それが静的変数を使用したい目的です。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>