安卓远程查看通知

标签 android notifications remoteview

我有一个使用远程 View 的安卓通知栏。我在那里有 2 个按钮用于播放和暂停音频,还有一个用于返回应用程序的图标。我希望能够单击图标(或除 2 个按钮以外的任何位置)并返回到应用程序。这是我的代码

Intent returnIntent = new Intent(_context, SplashScreenActivity.class);
returnIntent.setAction(Intent.ACTION_MAIN);
returnIntent.addCategory(Intent.CATEGORY_LAUNCHER);

PendingIntent remoteViewPendingIntent = PendingIntent.getActivity(_context,1, returnIntent,0);
remoteView.setOnClickPendingIntent(R.id.btnAppIcon, remoteViewPendingIntent);

这在 Android 4.1 的模拟器中一切正常。当按下通知中的图标时,它会成功返回到应用程序。然而,在三星 S3 上的 Android 4.0.3 上,Activity 在后台启动,但通知屏幕未隐藏。我希望在选择图标后清除通知屏幕。我尝试在通知中使用 Notification.FLAG_AUTO_CANCEL 但这并没有解决问题。任何帮助将不胜感激。

最佳答案

.setOnClickPendingIntent 行为因您所体验的不同 MFG 和版本而异。

要完成您想要的并自动折叠通知窗口(不使用反射),您必须为通知 View 设置一个“全局”挂起 Intent 。您还需要将 R.id.btnAppIcon 包含在该“捕获所有” Intent View 中。

“全局/默认”通知挂起 Intent 示例:

contentIntent = PendingIntent.getActivity(YourClass.this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;

当与 PendingIntent.getActivity 一起使用时,“一般”通知 Intent 将关闭任何设备上的通知窗口(据我观察)。

关于安卓远程查看通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13694975/

相关文章:

android - 更新 AppWidget 后未调用 RemoteViewsFactory 上的 onDataSetChanged()

java - Android 应用程序计算器在启动时退出

android - Cordova 通知中的多行消息

android - 使用 AsyncTask 将图像下载到 AppWidget 中的 ListView

android - 如何在android中显示多个通知

ios - 如何使用从 Swift 到 Objective-C 的 Notification.Name 扩展?

android - RemoteView setLayoutParams - 在 HomeScreen Widget 中更改 ImageView 大小

android - 带有 Proguard 的 Ant - java.lang.NoClassDefFoundError

android - NoClassDefFoundError 与 Kotlin 中的 ?.let{} block

android - 如何使用 rx java 的间隔进行后台任务