簡體   English   中英

當我關閉啟動它的活動時,Android Service死亡

[英]Android Service dies in when I close the Activity that launched it

我正在嘗試制作一個可啟動服務的android應用程序,該服務在應用程序打開時在后台運行,並且在應用程序關閉時不會消失。 但是,我的服務似乎確實會在退出時(或無論如何在某種程度上)消失,因為當我重新啟動應用程序時,我檢查服務是否正在運行是錯誤的。 我的代碼:

MainActivity.java

public class MainActivity extends AppCompatActivity {

  private ToggleButton enableButton;
  private boolean isRunning;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    enableButton = findViewById(R.id.enableButton);
    setRunning(MyService.running());
    enableButton.setChecked(isRunning);
    enableButton.setEnabled(true);
    enableButton.setClickable(true);
  }

  private void setRunning(boolean isRunning) {
    this.isRunning = isRunning;
  }

  public void onToggleEnable(View view) {
    boolean enabled = enableButton.isChecked();
    Intent intent = new Intent(this, MyService.class);
    if (enabled) startService(intent);
    else stopService(intent);
    setRunning(enabled);
  }

}

MyService.java

public class MyService extends Service {

  private static boolean running = false;

  public static boolean running() {
    return running;
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    running = true;
    return START_STICKY;
  }

  @Override
  public void onDestroy() {
    running = false;
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.chuckles.iscream">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="false" />
    </application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ToggleButton
            android:id="@+id/enableButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:onClick="onToggleEnable"
            android:textOff="@string/screamOff"
            android:textOn="@string/screamOn"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>

</ScrollView>

strings.xml

<resources>
    <string name="app_name">iScream</string>
    <string name="screamOn">Disable iScream</string>
    <string name="screamOff">Enable iScream</string>
</resources>

您的Service可能會被系統殺死。 從官方文檔:

Android系統僅在內存不足時強制停止服務,並且必須為具有用戶焦點的活動恢復系統資源。 如果服務綁定到以用戶為中心的活動,則它被殺死的可能性較小; 如果服務被聲明為在前台運行,則很少被殺死。

因此,如果要在未打開應用程序的情況下繼續運行Service ,則應聲明Service 在前台運行 為此,您必須調用startForeground(ONGOING_NOTIFICATION_ID, notification);

如此處所述:

https://developer.android.com/guide/components/services.html

注意:服務在其托管過程的主線程中運行; 除非另行指定,否則該服務不會創建自己的線程,也不會在單獨的進程中運行。

如果希望它在單獨的線程中運行,請改用IntentService

https://developer.android.com/guide/components/services.html#ExtendingIntentService

IntentService類執行以下操作:

它創建一個默認的工作線程,該線程執行應用程序的主線程之外的所有傳遞給onStartCommand()的意圖。

在清單文件中的服務標簽中,只需添加:

android:process=":externalProcess"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM