【问题标题】:"<App> is having trouble with Google Play Services. Please try again" while usiing Google NearBy Messages API“<App> 在使用 Google Play 服务时遇到问题。请重试”,同时使用 Google NearBy Messages API
【发布时间】:2020-04-20 15:09:38
【问题描述】:

使用 google NearBy Messages API 时出现错误“Google Play 服务出现问题。请重试”

请指导我解决这个问题。

以下是 Google 消息 API 的导入

    implementation 'com.google.android.gms:play-services-nearby:17.0.0'

这是我使用代码订阅的方式

    val options = SubscribeOptions.Builder()
        .setStrategy(Strategy.BLE_ONLY)
        .build()
    Nearby.getMessagesClient(
        this, MessagesOptions.Builder()
            .setPermissions(NearbyPermissions.BLE)
            .build())
    Nearby.getMessagesClient(this).subscribe(getPendingIntent(), options)

【问题讨论】:

    标签: android kotlin google-nearby google-nearby-messages


    【解决方案1】:

    我解决了。

    所有的消息 API 都应该在前台 Activity 中使用, 除了 subscribe 的变体采用 PendingIntent 参数。您的活动应该发布(消息)或 subscribe(MessageListener) 在 onStart() 或响应 可见活动中的用户操作,您应该始终对称 onStop() 中的 unpublish(Message) 或 unsubscribe(MessageListener)。

    • 订阅时,如果使用activity,它会要求授予蓝牙、位置、麦克风的权限,如果使用服务则不会询问

    所以如果你使用服务,你必须结合使用活动。

    • 在 mainActivity 中订阅时,如果顶部出现另一个 Activity(则 MainActivty 将处于 onStop),则会出现通知。 因此,在订阅时,您必须单击“确定”才能显示另一个活动

    这是示例:

    MainActivity.tk

    private val mMessageListener: MessageListener = object : MessageListener() {
            override fun onFound(message: Message) {
                Log.d(TAG, "onFound message:"+ String(message.content))
            }
    
            override fun onLost(message: Message) {
                Log.d(TAG, "Lost sight of message: " + String(message.content))
            }
        }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val sharedPref: SharedPreferences = getSharedPreferences("MyPref", Context.MODE_PRIVATE)
        val isFirstTime = sharedPref.getBoolean("FIRST_TIME", true)
        if(isFirstTime) {
                Nearby.getMessagesClient(this).subscribe(mMessageListener).addOnCompleteListener(this, OnCompleteListener {
                    requestPermissionFirstTime()
                }).addOnCanceledListener(this, OnCanceledListener {
                    requestPermissionFirstTime()
                })
            } else {
                requestPermissionCapture()
                checkPermissionAccessibility()
                startService(Intent(this, NearbyMessageService::class.java))
            }
    
    }
    private fun requestPermissionFirstTime() {
            val sharedPref: SharedPreferences = getSharedPreferences(Utils.IAMHERE_PREF, Context.MODE_PRIVATE)
            val editor = sharedPref.edit()
            editor.putBoolean("FIRST_TIME", false)
            editor.apply()
            Nearby.getMessagesClient(this).unsubscribe(mMessageListener)
            requestPermissionCapture()
            checkPermissionAccessibility()
        }
    

    NearbyMessageService.tk

    class NearbyMessageService: IntentService("NearbyMessageService") {
    private val mMessageListener: MessageListener = object : MessageListener() {
            override fun onFound(message: Message) {
                Log.d(TAG, "onFound message:"+ String(message.content))
            }
    
            override fun onLost(message: Message) {
                Log.d(TAG, "Lost sight of message: " + String(message.content))
            }
        }
        override fun onCreate() {
            super.onCreate()
            startForeground()
            Nearby.getMessagesClient(this).subscribe(mMessageListener)
        }
    
    private fun startForeground() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val channelId = "002"
                val channelName = "Nearby Service Channel"
                val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE)
                channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
                val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                manager.createNotificationChannel(channel)
                val notification: Notification = Notification.Builder(applicationContext, channelId)
                    .setOngoing(true)
                    .setCategory(Notification.CATEGORY_SERVICE)
                    .setContentTitle(getString(R.string.app_name))
                    .build()
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    startForeground(Utils.NOTICATION_ID_NEARBY, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION)
                } else {
                    startForeground(Utils.NOTICATION_ID_NEARBY, notification)
                }
            } else {
                startForeground(Utils.NOTICATION_ID_NEARBY, Notification())
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-13
      • 1970-01-01
      • 2016-09-25
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多