- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试使用 kotlin 构建一个 android 应用程序,我在其中连接、发送和读取来自 BLE(蓝牙低功耗)设备的数据。我设计了一个 Activity ,它将向我显示连接统计信息和从 BLE 设备接收的数据。我有一个前台服务正在运行,以保持与 BLE 设备的连接处于 Activity 状态并收听统计信息。
我已使用待处理的 Intent 从我的前台服务通知中打开此 Activity 。
以下代码显示了创建通知的方法
private fun getServiceNotification(textToShowInNotification: String)
{
val pendingIntent = Intent(<Static context from application class>,ActivityName::class.java)
pendingIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
val contentIntent = PendingIntent.getActivity(<static_context_from_application_class>, 0, pendingIntent, 0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
val notificationChannel = NotificationChannel("DATA_CHANNEL", <Static context from application class>.resources.getString(R.string.app_name),NotificationManager.IMPORTANCE_DEFAULT)
notificationManager!!.createNotificationChannel(notificationChannel)
}
notification = notificationBuilder!!.setOngoing(true)
.setOnlyAlertOnce(true)
.setContentText(textToShowInNotification)
.setContentIntent(contentIntent)
.build()
notificationManager!!.notify(NOTIFICATION_ID, notification)
}
因为我有一个用例来打开显示 BLE 连接和数据接收的 Activity 。创建了 Activity 的多个实例,并且前一次没有被破坏,因此我无法获取正在运行的 Activity 的上下文,因此我面临在 Activity 中显示警报对话框的问题。
D/DialogExecption: Unable to add window -- token null is not valid; is your activity running?
D/DialogExecption: Unable to add window token android.os.BinderProxy@4250d6d8 is not valid; is your activity running?
我使用下面的代码来显示对话框
private fun showAlertDialog()
{
val dialogAlertDialog = AlertDialog.Builder(<Activity Context>)
val inflater: LayoutInflater = layoutInflater
val dialogAlertView: View = inflater.inflate(R.layout.activity_xml_file,null)
dialogAlertDialog.setView(dialogAlertView)
dialogAlertDialog.setCancelable(false)
val builderAlertDialog : AlertDialog = dialogAlertDialog.create()
try
{
builderAlertDialog.show()
}
catch(exception: Exception)
{
Log.d("DialogExecption",""+exception.message)
}
}
我也试过方法
if (!this.isFinishing)
{
builderAlertDialogCycleCancelled.show()
}
但这也无济于事。上面的代码将抑制执行,但我不想这样做,而是我想不惜一切代价显示对话框。
<activity android:name=".ActivityName"
android:alwaysRetainTaskState="true"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="@style/AppThemeGeneral">
</activity>
我必须以最好的方式使用警报对话框并且跳过它不是一个选择。
最佳答案
观察
notification = notificationBuilder!!.setOngoing(true)
.setOnlyAlertOnce(true)
.setContentText(textToShowInNotification)
.setContentIntent(contentIntent)
.build()
notificationManager!!.notify(NOTIFICATION_ID, notification)
Activity
因为
notification
缺少
setSmallIcon
或类似的
setIcon
称呼。要修复它,请设置一些图标,如下所示:
notification = notificationBuilder!!.setOngoing(true)
.setOnlyAlertOnce(true)
.setContentText(textToShowInNotification)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher_background)
.build()
notificationManager!!.notify(NOTIFICATION_ID, notification)
previous activity is not destroyed
Intent.FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_CLEAR_TASK
当开始
Activity
,这将完成所有以前的 Activity 。否则,请出示相关代码。
When the connection with device is established and data is listenedfrom service at that time when I kill the app and open it fromforeground service notification, the dialog which has to show upsaying that my device is still connected and data is being received isnot shown
Activity
中展示它, 但它不太可能失败,特别是如果你从
onCreate
显示它您的
Activity
:
override fun onCreate(savedInstanceState: Bundle?) {
...
showAlertDialog()
}
D/DialogExecption: Unable to add window -- token null is not valid; isyour activity running? D/DialogExecption: Unable to add window tokenandroid.os.BinderProxy@4250d6d8 is not valid; is your activityrunning?
Context
的对话框这里
AlertDialog.Builder(<context>)
.例如,如果您使用
applicationContext
或
Service
context
在那里,它会因为这样的异常而失败。
Activity
开始对话上下文如下:
...
val dialogAlertDialog = AlertDialog.Builder(<Activity Context>)
...
但是,从您的代码中不清楚
showAlertDialog()
被调用,并且上下文对象也没有显示。因此,我创建了一个示例项目来测试所描述的行为。
BluetoothDeviceService
负责启动
Activity
,当点击通知时。
class BluetoothDeviceService: Service() {
private val SERVICE_NOTIFICATION_ID = 123
private val SERVICE_NOTIFICATION_CHANNEL_ID = "channel_01"
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
SERVICE_NOTIFICATION_CHANNEL_ID,
"Bluetooth service",
NotificationManager.IMPORTANCE_DEFAULT)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
val pendingIntent = Intent()
pendingIntent.setClass(this,BluetoothDeviceActivity::class.java)
pendingIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
val contentIntent = PendingIntent.getActivity(this, 0,
pendingIntent, 0)
val notification = NotificationCompat.Builder(this, SERVICE_NOTIFICATION_CHANNEL_ID)
.setOnlyAlertOnce(true)
.setOngoing(true)
.setContentText("Bluetooth service running...")
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher_background)
.build()
startForeground(SERVICE_NOTIFICATION_ID, notification)
}
}
override fun onBind(intent: Intent?): IBinder? {
TODO("Not yet implemented")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return START_STICKY
}
}
我创建了一个
MainActivity
那必须启动前台服务。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val startServiceIntent = Intent(this, BluetoothDeviceService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(startServiceIntent)
} else {
startService(startServiceIntent)
}
finish()
}
}
请注意 Service
也可以由 BroadcastReceiver
启动,这样会更合适,但我使用了 Activity
为简单起见。
BluetoothDeviceActivity
由服务在
PendingIntent
的帮助下启动:
class BluetoothDeviceActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showAlertDialog()
}
}
private fun showAlertDialog() {
val dialogAlertDialog = AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("This is a test")
.setTitle("Information")
.create()
dialogAlertDialog.show()
}
以防万一,我也把我的 list 。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.stackoverflowquestion2">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
android:name=".MainApplication"
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.StackOverflowQuestion2"
android:fullBackupContent="@xml/backup_descriptor">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.StackOverflowQuestion2.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".BluetoothDeviceActivity"/>
<service android:name=".BluetoothDeviceService"/>
</application>
</manifest>
结果
AlertDialog
至
Activity
并将其用作
Dialog
.为此,您需要做两件事:
Àctivity
如下:class AlertDialogActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val dialogAlertDialog = AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("This is a test")
.setTitle("Information")
.setPositiveButton("OK") { dialog, which -> finish() }
.create()
dialogAlertDialog.show()
}
}
Dialog
:<activity android:name=".AlertDialogActivity" android:theme="@style/Theme.AppCompat.Dialog"/>
Service
中创建一个方法,并在需要时随时使用它:private fun showAlertDialog() {
val intent = Intent(applicationContext, AlertDialogActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
applicationContext.startActivity(intent)
}
关于运行前台服务的 Android 'BadTokenException window token android.os.BinderProxy@4250d6d8 is not valid',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66689566/
我正在关注 Android guide在媒体播放器应用程序中使用 MediaBrowserServiceCompat,但该服务在应用程序退出时被销毁。
我使用System.Windows.Window.IsActive来检测窗口是否位于前台,并且在某些情况下有效。但我发现了一些情况,它没有,我想知道是否有任何方法可以检测到它。 最佳答案 除非仅后台进
我需要为 EditText 的一部分设置样式。我希望文本为白色,背景为灰色。看起来很简单,但事实并非如此。 spanRange.setSpan(new BackgroundColorSpan(Colo
我决定在一个帖子中发布两个问题,因为这是完全相同的问题。 我需要知道屏幕何时打开或关闭,以便我可以打开 LED。第二个我需要知道我的应用程序是在后台还是在前台,以便在应用程序处于后台时管理在某些操作上
关于双高红色录音状态栏有很多问题( here , here ),但是当应用程序退出到后台时,所有这些问题都引用闪烁。我得到了一个闪光,我假设来自 AVCaptureSession设置,而应用程序在前台
我有一个奇怪的案例。我的 swift ios 应用程序已连接到 Cloudkit。如果应用程序未运行(后台状态),我每次都会收到通知徽章和警报!如果应用程序正在运行,则不会收到任何通知!我知道它没有击
我是 firebase 和 android 的新手,我想在我的应用程序中包含实时聊天。但我对 firebase 有以下疑问。请帮忙。 1) 如果应用程序在前台,系统托盘中是否会有默认通知,还是我必须在
我正在为 Excel 编写 VSTO 加载项,我注意到,如果我锁定工作表并使用密码保护它(因此只有我的加载项可以写入它,但用户可以查看它),如果用户尝试编辑工作表时,他们收到“此工作表已锁定”弹出窗口
我正在开发一个 iOS 应用程序,此应用程序允许其用户添加他稍后必须执行的任务。完成添加此任务后,它将发送到服务器以保存在服务器端。现在我对某些情况感到困惑:我的用户在输入任务详细信息时有什么电话..
我正在阅读内存不足 (OOM) killer ,以及 Android 如何确定进程的优先级 (https://developer.android.com/guide/components/proces
我已经在我的新应用程序中启动了一项服务。该服务是前台的,带有通知。当它在 AVD 2.1 API Level 7 中运行时,一切正常。但是当它在运行 Gingerbread 的 Samsung Gal
我的 Laravel 应用程序的结构需要帮助。 我想要的基本上是这个结构: 应用程序接口(interface) 管理面板 公共(public)网站 我开始构建我认为非常正确的文件夹结构: app/
我正在尝试用 CardView 填充我的 RecyclerView,CardView 使用 Android 数据绑定(bind)来设置属性,例如 TextView 中的文本。在未完成喷射的项目上,我想
我想使用 Window Script Host(WSH) 查找当前事件(具有焦点)的窗口的标题,因为我希望我的 WSH 脚本仅在所需窗口处于事件状态时才发送键。 注意*我无法使用替代方案,即在调用 s
如何调试 react-scripts 启动? 这工作正常,我不知道发生了什么变化(我没有改变任何东西) 看来 react-scripts start 无法作为前台进程继续运行。 我的 Dockerfi
我想制作像endonmondo那样的秒表。当我们启动应用程序时,它应该计算时间并更新主 Activity 中的textView(实际上它是一个 fragment )。 我做了后台服务(如 here )
我遇到了一个需求,但我无法获得正确的实现方式,因此需要您的帮助。 我想做什么? - 我想根据收到的通知执行操作,如下所示: 当应用程序打开并位于前台时,即对用户可见并且我收到通知时,我只是显示一个弹出
在 Android 10 中,我注意到我从操作系统收到一条 Toast 消息,说明“此 NFC 标签不支持应用程序”或“此 NFC 标签不支持应用程序”(取决于设备): 奇怪的是,我在 enableR
我是一名优秀的程序员,十分优秀!