android - "在使用 Google Play 服务时遇到问题。请再试一次”,同时使用 Google NearBy Messages API
问题描述
使用 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)
解决方案
我解决了。
- 附近建议使用活动,在活动上,该功能会更好地工作(https://developers.google.com/android/reference/com/google/android/gms/nearby/messages/MessagesClient#subscribe(android.app.PendingIntent, %20com.google.android.gms.nearby.messages.SubscribeOptions) )
所有的消息 API 都应该在前台 Activity 中使用,除了带有 PendingIntent 参数的 subscribe 变体。您的 Activity 应该在 onStart() 中发布(消息)或订阅(MessageListener),或者响应可见 Activity 中的用户操作,并且您应该始终在 onStop() 中对称地取消发布(消息)或取消订阅(消息监听)。
- 订阅时,如果使用activity,它会要求授予蓝牙、位置、麦克风的权限,如果使用服务则不会询问
所以如果你使用服务,你必须结合使用活动。
- 当你在 mainActivity 中订阅时,如果另一个活动出现在顶部(然后 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())
}
}
}
推荐阅读
- css - SVG 掩码图像未在 Firefox 和 Edge 中显示
- python - 具有内部预测的自定义 Keras 损失函数
- ruby-on-rails - 在嵌套哈希中打印字符串 - Ruby
- javascript - Javascript:函数声明/表达式中的函数/变量提升
- laravel - Laravel 实现用户好友(聊天)状态
- python - 具有动态创建的 GroupBoxes 的 PyQt5 QScrollArea 小部件
- android - 房间语用查询
- php - Ajax 调用包括在 Wordpress 中不起作用
- java - 通过Hashmap迭代ArrayList以找到未使用的元素
- macos - OS/X "etc/resolver/dev" 不工作——为什么不呢?