2012-09-14 69 views
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> 
+1

你確定'onCreate'被調用了嗎? 'activity'&'service'運行在同一個進程/ JVM中嗎? –

+0

我測試了你的建議,是的,添加Log.v(LOG_TAG,「Setting isRunning to TRUE」);在合適的時間調用參數之前進行調用。所以是的,這是有效的。活動和服務通過活頁夾進行通信,我不確定這是否意味着它們在單獨的進程中運行。 –

+0

也許嘗試把'super.onCreate(); * * * isRunning = true;' – alex

回答

2

刪除安卓過程

的過程,其中服務是運行的名稱。通常,應用程序的所有組件都在爲應用程序創建的默認進程中運行。它與應用程序包具有相同的名稱。元素的流程屬性可以爲所有組件設置不同的默認值。但是組件可以用自己的進程屬性覆蓋缺省值,從而允許將應用程序分佈在多個進程中。

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. 
+0

應該是一個評論 – Blackbelt

+0

好的,謝謝!但是我希望服務繼續運行,即使應用程序(活動)被終止。這不需要我以不同的流程啓動它嗎? –

+0

是的。但是,你將無法在活動和服務中共享靜態變量。 '這是因爲每個進程都有它自己的類加載器' –