Shadow icon indicating copy to clipboard operation
Shadow copied to clipboard

连接Service超时

Open gushiyong opened this issue 3 years ago • 2 comments

我们在插件里面写了一个PluginTakeService,目的是插件下载完启动PluginTakeService来唤醒插件进程,现在遇到了一个问题,插件在下载后第一次启动会出现【连接Service超时】,清除缓存在重新下载安装就正常,大佬帮看一下原因 public final void waitServiceConnected(int timeout, TimeUnit timeUnit) throws TimeoutException { if (Looper.myLooper() == Looper.getMainLooper()) { throw new RuntimeException("waitServiceConnected 不能在主线程中调用"); } try { if (mLogger.isInfoEnabled()) { mLogger.info("waiting service connect connectCountDownLatch:" + mConnectCountDownLatch); } long s = System.currentTimeMillis(); boolean isTimeout = !mConnectCountDownLatch.get().await(timeout, timeUnit); if (isTimeout) { throw new TimeoutException("连接Service超时 ,等待了:" + (System.currentTimeMillis() - s)); } if (mLogger.isInfoEnabled()) { mLogger.info("service connected " + (System.currentTimeMillis() - s)); } } catch (InterruptedException e) { throw new RuntimeException(e); } }

gushiyong avatar Aug 29 '22 07:08 gushiyong

别人又不知道你们的PluginTakeService是怎么写的,怎么能帮你呢?开源项目的交流要建立在开源的代码上。

https://github.com/Tencent/Shadow/blob/abb72db13292982ebf5bf9f36af6ae8d2a3678a6/CONTRIBUTING.md

shifujun avatar Aug 30 '22 15:08 shifujun

就这个简单的简单的service class TakePluginService : Service() {

private val NAME = "前台服务"
private val ID = "channel_20"

override fun onBind(p0: Intent?): IBinder? {
    return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    return super.onStartCommand(intent, flags, startId)

}

override fun onCreate() {
    super.onCreate()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        setForeground()
        //注册网络状态监控
        NetUtil.registerNetConnChangedReceiver(this)
    }
}

@RequiresApi(api = Build.VERSION_CODES.O)
private fun setForeground() {
    val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    val channel = NotificationChannel(ID, NAME, NotificationManager.IMPORTANCE_HIGH)
    manager.createNotificationChannel(channel)
    val notification: Notification =
        Notification.Builder(this, ID).setContentTitle("取派插件服务").setContentText("取派插件服务运行中")
            .setSmallIcon(R.mipmap.ic_my_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_my_launcher))
            .build()
    startForeground(1, notification)
}

override fun onDestroy() {
    //注销
    NetUtil.unregisterNetConnChangedReceiver(this)
    super.onDestroy()
}

}

gushiyong avatar Sep 01 '22 02:09 gushiyong