gpt4 book ai didi

java - 当我关闭启动它的 Activity 时,Android 服务死掉了

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:17 26 4
gpt4 key购买 nike

我正在尝试制作一个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>

字符串.xml

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

最佳答案

您的服务可能会被系统终止。来自官方文档:

The Android system force-stops a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, it's less likely to be killed; if the service is declared to run in the foreground, it's rarely killed.

因此,如果您想在应用未打开时继续运行 Service,那么您的 Service 应声明为 run in the foreground 。为此,您必须调用 startForeground(ONGOING_NOTIFICATION_ID, notification);

关于java - 当我关闭启动它的 Activity 时,Android 服务死掉了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48848004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com