2

私は小さなアプリを構築しています。これは現在、サービスとアクティビティで非常にうまく機能しています。

ただし、ログイン時にいくつかの静的情報 (サービスは既に開始されているかなど) を静的ブール値 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>
4

1 に答える 1

2

Android:プロセスを削除する

サービスが実行されるプロセスの名前。通常、アプリケーションのすべてのコンポーネントは、アプリケーション用に作成されたデフォルト プロセスで実行されます。アプリケーション パッケージと同じ名前です。要素の process 属性は、すべてのコンポーネントに対して異なるデフォルトを設定できます。ただし、コンポーネントは独自のプロセス属性でデフォルトをオーバーライドできるため、アプリケーションを複数のプロセスに分散させることができます。

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. 
于 2012-09-14T12:53:54.743 に答える