首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当在Galaxy 8和9上设置徽章号时,Xamarin获得许可拒绝。

当在Galaxy 8和9上设置徽章号时,Xamarin获得许可拒绝。
EN

Stack Overflow用户
提问于 2018-07-19 20:38:38
回答 1查看 503关注 0票数 0

我有一个应用程序,它在应用图标上的小红点上显示通知号。这在所有型号上都很好,三星Galaxy S9除外。我尝试过许多解决方案,但总是会出错。

权限拒绝:从com.sec.android.provider.badge.permission.WRITE,编写com.sec.android.provider.badge.BadgeProvider uri内容://com.sec.徽章/应用程序,uid=10233需要pid=32464或grantUriPermission()

该项目是VisualStudio2017中的Xamarin Android项目,下面是用于除S9以外的所有模型的代码

在舱单上

代码语言:javascript
复制
<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />

The C#

代码语言:javascript
复制
        try
        {
            var context = Android.App.Application.Context;

            // This is the content uri for the BadgeProvider
            var uriPath = "content://com.sec.badge/apps"; //"com.sec.android.app.launcher"; 
            Android.Net.Uri uri = Android.Net.Uri.Parse(uriPath); //new Uri("content://com.sec.badge/apps");

            ICursor cursor = context.ContentResolver.Query(uri, new string[] { "_id" }, "package=?", new string[] { context.PackageName }, null);

            if (cursor == null || !cursor.MoveToFirst())
            {
                ContentValues cv = new ContentValues();
                cv.Put("package", context.PackageName);
                cv.Put("class", context.PackageManager.GetLaunchIntentForPackage(context.PackageName).Component.ClassName);
                cv.Put("badgecount", count);
                context.ContentResolver.Insert(uri, cv); //Errors here
            }
            else
            {
                int idColumnIndex = cursor.GetColumnIndex("_id");

                ContentValues cv = new ContentValues();
                cv.Put("badgecount", count);
                context.ContentResolver.Update(uri, cv, "_id=?", new string[] { cursor.GetInt(idColumnIndex).ToString() });
            }

        }
        catch (System.Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message); 
        }

就像我说的,这一切对早期的模型都很好,但是对于新的模型却是错误的。是否有需要添加到权限中的内容,还是我缺少了其他内容?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-20 13:39:48

我已经解决了我的问题。现在,您需要将NotificationChannel与您的NotificationManager结合使用。我用一个按钮创建了一个小测试应用程序。在按钮单击中,我调用下面的代码。我还没有把它移到我的实时应用程序中,但是它在这个测试应用程序中运行得很好。

首先,为NotificationChannel的id参数添加一个const。

代码语言:javascript
复制
public const string CLICKS_CHANNEL = "com.yourcompany.SamsungBadgeTest.clicks";

现在:

  1. 将NotificationManager的声明移动到NotificationBuilder之上
  2. 创建NotificationChannel
  3. 将通道作为参数调用管理器的CreateNotificationChannel
  4. 让你的Notification.Builder
  5. 建() 尝试{/这是在类级别声明的,最初设置为0//增量,每个按钮单击clickCount++;int默认值= 0;缺省值|= (int)NotificationDefaults.Sound;缺省值|= (int)NotificationDefaults.Lights;默认值|= (int)NotificationDefaults.Vibrate;var notificationManager =NotificationDefaults.Lightsvar chan =新NotificationChannel(CLICKS_CHANNEL,"Button Click",NotificationImportance.Default) { LockscreenVisibility = NotificationVisibility.Private };var notificationBuilder =新的NotificationCompat.Builder(这个,.SetSmallIcon(Resource.Drawable.ic_stat_button_click) .SetBadgeIconType(NotificationCompat.BadgeIconSmall ( CLICKS_CHANNEL) .SetAutoCancel(真).SetContentText(“您收到了新消息”).SetContentTitle(“新消息”) .SetNumber(clickCount) //您需要在您的项目中添加一个图标并在这里获取它).SetDefaults(默认值);//..Build();notificationManager.Notify(0,notificationBuilder.Build());} catch(System.Exception ex) {notificationBuilder.Build}

别忘了把这个加到你的声明里

代码语言:javascript
复制
<uses-permission android:name="com.sec.android.provider.badge.permission.READ" />
<uses-permission android:name="com.sec.android.provider.badge.permission.WRITE" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51431348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档