[英]My background service stops when the screen turns off on my Moto G6
我创建了一个应用程序来在我的 Moto G6 android 设备上工作。 基本上,这个启动本地服务以定期访问 web 站点的应用程序。 一切看起来都很好,但是当屏幕关闭时,服务就暂停了。 当屏幕打开时,它再次变为活动状态。 我在论坛中发现了很多关于这种行为的讨论,在建议的事情中,这是我尝试过的:
Moto G6 有什么特别之处吗?...这是我的清单文件:
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.coininverst">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<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/Theme.CoinInverst">
<service android:name=".CoinGeckoService" />
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.CoinInverst.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我启动服务的方式(在主要活动中):
mServiceIntent = new Intent(this, CoinGeckoService.class);
startService(mServiceIntent);
bindService(mServiceIntent, Connection, Context.BIND_AUTO_CREATE);
仍在主要活动中,嵌套的 class:
private ServiceConnection Connection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
// We've bound to LocalService, cast the IBinder and get LocalService instance
CoinGeckoService.LocalBinder oBinder = (CoinGeckoService.LocalBinder) service;
mService = oBinder.getService();
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg0)
{
mBound = false;
}
};
在服务方面:
public class CoinGeckoService extends Service
{
private Looper m_oServiceLooper = null;
private ServiceHandler m_oServiceHandler = null;
private HandlerThread m_oHandlerThread = null;
private boolean m_bExitFlag = false;
private boolean m_bExitEcho = false;
private int m_iSnapshotPeriodSec = 300;
private final IBinder m_oBinder = new LocalBinder();
private Intent m_oIntent = null;
private Thread m_oStillRunningProc = null;
private Message m_oHandleMessage = null;
private PowerManager m_oPowerManager = null;
private PowerManager.WakeLock m_oWakeLock = null;
private Context m_oContext = null;
// Handler that receives messages from the thread
private final class ServiceHandler extends Handler
{
public ServiceHandler(Looper oLooper)
{
super(oLooper);
}
@Override
public void handleMessage(Message msg)
{
m_oHandleMessage = msg;
m_oStillRunningProc = new Thread()
{
public void run()
{
InfiniteProcees();
}
};
m_oStillRunningProc.start();
}
}
private void InfiniteProcees()
{
while (!m_bExitFlag)
{
long lStartTimeMs = System.currentTimeMillis();
long lElapsedTimeMs = 0L;
long lWakeLockTimeoutMSec = 10 * 60 * 1000L;
m_oWakeLock.acquire(lWakeLockTimeoutMSec);
....
m_oWakeLock.release();
lElapsedTimeMs = (System.currentTimeMillis() - lStartTimeMs);
if ((m_iSnapshotPeriodSec * 1000) > (int)lElapsedTimeMs)
SleepMSec((m_iSnapshotPeriodSec * 1000) - (int)lElapsedTimeMs);
//do
//{
// SleepMSec(1000);
// lElapsedTimeMs = (System.currentTimeMillis() - lStartTimeMs);
//}
//while (!m_bExitFlag && (lElapsedTimeMs < (m_iSnapshotPeriodSec * 1000)));
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(m_oHandleMessage.arg1);
m_bExitEcho = m_bExitFlag;
}
public void onCreate()
{
int iNbPage = 3;
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work doesn't disrupt our UI.
m_oLogPath = getExternalFilesDir(null) + "/CoinDBase" ;
m_oImgPath = getExternalFilesDir(null) + "/CoinImages";
m_aoPages = new String[iNbPage];
m_oSyncExport = new Object();
m_oContext = this;
m_oPowerManager = (PowerManager)m_oContext.getSystemService(Context.POWER_SERVICE);
m_oWakeLock = m_oPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CoinGeckoService:WakeLock");
....
m_oHandlerThread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND);
m_oHandlerThread.start();
// Get the HandlerThread's Looper and use it for our Handler
m_oServiceLooper = m_oHandlerThread.getLooper();
m_oServiceHandler = new ServiceHandler(m_oServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Toast.makeText(this, "CoinGeckoService starting", Toast.LENGTH_SHORT).show();
m_oIntent = intent;
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = m_oServiceHandler.obtainMessage();
msg.arg1 = startId;
m_oServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent)
{
return m_oBinder;
}
@Override
public void onDestroy()
{
super.onDestroy();
m_bExitFlag = true;
int iTimeoutMsec = 2000;
while (!m_bExitEcho && (iTimeoutMsec > 0) && SleepMSec(100))
{
iTimeoutMsec -= 100;
}
//Toast.makeText(this, "CoinGeckoService done", Toast.LENGTH_SHORT).show();
Intent oBroadcastIntent = new Intent();
oBroadcastIntent.setAction("RestartService");
oBroadcastIntent.setClass(this, ServiceRestarter.class);
this.sendBroadcast(oBroadcastIntent);
}
public class LocalBinder extends Binder
{
CoinGeckoService getService()
{ // Return this instance of LocalService so clients can call public methods
return CoinGeckoService.this;
}
}
}
请注意,该服务始终在应用程序关闭时停止。 即使那不是预期的,但这是我现在可以忍受的。 每一个建议或评论将不胜感激。 先感谢您。
混合服务:您想要一个已启动的服务,但能够像绑定服务一样在客户端和服务之间进行通信。其工作方式是让绑定服务实现 onStartCommand()。 通常,启动的服务实现 onStartCommand() 以接收来自客户端的意图。 但绑定服务通常不实现 onStartCommand,而是实现 onBind() 挂钩方法。 但是,如果绑定的服务实现了 onStartCommand,则向 Android 活动管理器服务框架表明,当绑定到它的所有客户端解除绑定时,不应销毁该服务。
看起来您正在使用混合服务 model。通常后台媒体播放器应用程序使用此 model。您可以解决问题的方法如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.