Android Auto和Xamarin兼容性



我的应用程序与Android auto连接时遇到问题。它是用Xamarin.Android制作的。我把XML链接到了Android清单中,但它仍然不起作用。

清单包含:

<meta-data android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc" />

XML包含:

<?xml version="1.0" encoding="UTF-8" ?>
<automotiveApp>
<uses name="notification"/>
</automotiveApp>

这就是我构建通知的方式:

NotificationCompat.Builder notificationBuilder = new 
NotificationCompat.Builder(ApplicationContext);
notificationBuilder.SetSmallIcon(Resource.Mipmap.ic_push_icon)
.SetContentText(msg)
.SetWhen(timestamp)
.SetContentTitle(content)
.SetContentIntent(readIntent)
.Extend(new CarExtender()
.SetUnreadConversation(unReadConversation)
.SetColor(ApplicationContext.GetColor(Resource.Color.purple)))
.SetChannelId(Fields.CHANNEL_ID)
.AddAction(CreateActionFromRemoteInput(replyIntent,remoteInput));
mNotificationManager.Notify(conversation.Id, notificationBuilder.Build());

有什么建议吗?谢谢

编辑:我正在使用minSdk 21和targetSdk 26

编辑:

我唯一的日志是:

[Notification]有关使用内容,请参阅setSound((的文档而是使用android.media.AudioAttributes来限定您的播放用例

[通知]对于其他操作,不赞成使用流类型比音量控制

我设法让android自动与Xamarin一起工作。

我学到的是,设置SetReadPendingIntent((SetReplyAction((是强制性的。

在github上查看我的测试应用程序https://github.com/Verthosa/Xamarin_Android_Auto_Test

我在您提供的代码片段中没有看到这一点,但看起来缺少的一件事是将消息添加到unReadConversation中。

unReadConversation.addMessage(messageString).setLatestTimestamp(currentTimestamp);

更多详细信息请参阅文档的"发送消息"部分,很可能与它现在如何处理会话中的多条消息有关。

下面是一个更完整的片段,我正在开发一个示例应用程序,在Auto的桌面主机上显示消息。

private void sendNotification(int conversationId, String title, String message,
String participant, long timestamp) {
// Build a pending Intent for reads
PendingIntent readPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
conversationId,
createIntent(conversationId, READ_ACTION),
PendingIntent.FLAG_UPDATE_CURRENT);
// Build a Pending Intent for the reply action
PendingIntent replyPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
conversationId,
createIntent(conversationId, REPLY_ACTION),
PendingIntent.FLAG_UPDATE_CURRENT);
// Build a RemoteInput for receiving voice input in a Car Notification
RemoteInput remoteInput = new Builder(EXTRA_VOICE_REPLY)
.setLabel(getString(R.string.reply_by_voice))
.build();
// Build an Android N compatible Remote Input enabled action.
NotificationCompat.Action actionReplyByRemoteInput = new NotificationCompat.Action.Builder(
R.drawable.notification_icon, getString(R.string.reply), replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
// Create the UnreadConversation and add the participant name,
// read and reply intents.
UnreadConversation.Builder unReadConversation =
new UnreadConversation.Builder(participant)
.setLatestTimestamp(timestamp)
.setReadPendingIntent(readPendingIntent)
.setReplyAction(replyPendingIntent, remoteInput);
// Add the message to the unread conversation
unReadConversation.addMessage(message).setLatestTimestamp(timestamp);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.android_contact))
.setContentText(message)
.setWhen(timestamp)
.setContentTitle(title)
.setContentIntent(readPendingIntent)
.extend(new CarExtender()
.setUnreadConversation(unReadConversation.build())
.setColor(getApplicationContext().getResources()
.getColor(R.color.default_color_light))).addAction(actionReplyByRemoteInput);

mNotificationManager.notify(conversationId, notificationBuilder.build());
}
private Intent createIntent(int conversationId, String action) {
return new Intent()
.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
.setAction(action)
.putExtra(CONVERSATION_ID, conversationId)
.setPackage(getPackageName());
}

我看到这个线程已经有4年的历史了。但它仍有一些观点,所以如果人们真的偶然发现了它,我会在这里给出一个答案,让他们走上正确的道路。这取决于你想要创建的Android Auto应用程序的类型。流程略有不同。为普通的Android或Xamarin开发是惊人的相似,所以一定要看看Android for cars开发人员文档。

但请记住,Xamarin创建Android清单的方式与普通Android中的方式有点不同。默认情况下,Xamarin为您的服务名称分配Guid。看看微软的文档,更好地了解如何使用属性将标签注入到与类链接的清单中。如果你查看Visual studio项目中的obj文件夹,你可以看到最终的Android清单是什么样子的。

我只处理过使用CarAppLibrary的类别。在Xamarin中,您必须安装Xamarin.AndroidX.Car.App.App-nuget包,您将拥有他们在谷歌开发人员文档中使用的所有类。

另一件需要记住的事情是,如果您在类中实现任何Java接口(如OnClickListeners(,也要从Java.Lang.Object继承,以便它为您实现大多数抽象成员。

我在YouTube上为毛伊岛创建了一个循序渐进的教程,因为这是Xamarin的进化。但Xamarin的步骤完全相同。

希望这能有所帮助!祝你好运。

最新更新