javascript - Appcelerator 通知 createRemoteViews 点击事件监听器
问题描述
我正在尝试构建一个音乐播放器,为此我需要在通知托盘中进行音乐控制。我为此使用 RemoteViews 但无法为播放/暂停、下一个和上一个添加事件侦听器。下面是源代码。
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/gradient_background"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon_logo"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:src="@drawable/app_logo_notification"/>
<TextView android:id="@+id/lbl_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textColor="#b8a25e"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:text="TEST" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#b8a25e"
android:layout_marginLeft="20dp"
android:text="Now Playing James Bond anthem"/>
<ImageView
android:id="@+id/icon_previous"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:src="@drawable/icon_previous_home"/>
<ImageView
android:id="@+id/icon_play"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:src="@drawable/ic_action_playback_play"/>
<ImageView
android:id="@+id/icon_next"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="20dp"
android:src="@drawable/icon_next_home"/>
</LinearLayout>
</LinearLayout>
通知代码 -
var AppR = Ti.App.Android.R;
var customLayout = Ti.Android.createRemoteViews({
layoutId : AppR.layout.custom_layout
});
var notification = Titanium.Android.createNotification({
contentView : customLayout,
visibility : Titanium.Android.VISIBILITY_PUBLIC,
icon : AppR.drawable.app_logo_notification,
});
customLayout.setTextViewText(AppR.id.message, "Now Playing...\n");
customLayout.setImageViewResource(AppR.id.icon_play, AppR.drawable.ic_action_playback_pause);
var icon_play = AppR.id.icon_play;
Ti.Android.NotificationManager.notify(1, notification);
任何建议,将不胜感激。
解决方案
RemoteView 通知的完整示例位于:
https
://docs.axway.com/bundle/Titanium_SDK_allOS_en/page/android_notifications.html#AndroidNotifications-Customlayout
,其中包含按钮和单击事件。
在您的示例中,它应该是:
customLayout.setOnClickPendingIntent(Ti.App.Android.R.id.icon_play, onClickCallback);
function onClickCallback(){}
推荐阅读
- angular - 仅在移动视图上隐藏 ngx-popover
- c++ - 使用指针变量打印特定的数组变量
- android - 将 FloatArray 音频缓冲区保存到 Android 上的 wav 文件
- excel - 我如何将此代码转换为更短的代码?可能通过数组?
- javascript - 在 shell exec 期间无法使用 socket.io 发出当前服务器状态
- javascript - 如何从 ids 对象中删除项目
- python - 从 Postgresql 查询大数据集并进行处理
- javascript - JS 开发人员是否应该阅读 ECMAScript 规范?
- javascript - 更改嵌套对象的引用是否正在更改对根对象的引用?
- matplotlib-basemap - 如何使用 python 在伦敦地图上添加散点图