英文:
Android Java - Why kill server after 60
问题
为什么在Android中的60秒后系统会终止服务器?
服务是否能够始终在系统的干扰下工作?
该问题在新的Android版本中出现。
所使用的代码:
是否有解决此错误的方法?
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String data = intent.getExtras().getString("LCD1");
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
}
}
<service
android:name=".MyService"
android:label="Test"
android:exported="true">
<intent-filter >
<action android:name="com.mhm.servertest.MyService" />
</intent-filter>
</service>
英文:
Why kills server by system after 60 in Android
Can the service always work without interruption by the system?
The problem appears in new Android releases
The code used
Is there any solution to the error
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
String data = intent.getExtras().getString("LCD1");
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
}
}
<service
android:name=".MyService"
android:label="Test"
android:exported="true">
<intent-filter >
<action android:name="com.mhm.servertest.MyService" />
</intent-filter>
</service>
答案1
得分: 1
这对于长期运行的服务,您在实现Android中的长期运行服务之前应该考虑几件事情,比如:
- Doze模式,在此模式下,Android操作系统掌管后台服务的执行时间。
- 参考这个答案以更好地了解带有内存和CPU处理的服务的工作原理。
- 可以使用WorkManager来执行并发作业。
- 您应该在清单xml中添加android:stopWithTask="false",以便在任务清除时继续服务运行。
英文:
This for long runnig service, Your should consider several things before implementing long running Service in Android like
- Doze mode in which Android OS takes charge of background services execution time.
- Refer this answer to understand better working of service with memory and CPU processing.
- Workmanager can be used to do concurrent job
- You should add android:stopWithTask="false" in manifest xml.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论