开发者社区> 问答> 正文

无法清除通知并从通知中打开片段

我正在尝试清除通知。但是它仍然在那里。无法从通知中打开片段活动。下面是我的代码,

 @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int sticky;

        try {
            AndroidLogger.log(5, TAG, "Missed call notification on start");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
                NotificationChannel channel = new NotificationChannel("1", "Call Notification", importance);
                channel.setDescription("Get alert for missed call");
                channel.setShowBadge(true);
                channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
            Intent notifyIntent = new Intent(this, ViewFragment.class);
            notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


            PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
                    .setSmallIcon(R.drawable.nexge_logo)
                    .setContentTitle("Missed Call")
                    .setContentText(intent.getStringExtra("Number"))
                    .setContentIntent(pIntent)
                    .setAutoCancel(true)
                    .setPriority(Notification.PRIORITY_MAX);

            Notification buildNotification = mBuilder.build();
            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            //mNotifyMgr.notify(1, buildNotification);

            startForeground(1,buildNotification);
        } catch (Exception exception) {
            AndroidLogger.error(1, TAG, "Exception while starting service", exception);
        }
        return START_NOT_STICKY;
    }
}

有人帮我解决这个问题。提前致谢。以下是我没有得到正确答案的另一个问题。帮助我解决 关于android中未接来电的通知

展开
收起
Puppet 2020-01-08 17:26:13 407 0
1 条回答
写回答
取消 提交回答
  • HttpURLConnection urlConnection = null; 您永远不会为分配值urlConnection,因此也是如此null。

    您需要添加:

    urlConnection = url.openConnection(); 在尝试使用之前urlConnection。

    URL而且HttpURLConnection已经很老了,很少有开发人员再使用它们。使用更现代的HTTP客户端API(例如OkHttp)可能会更好。

    2020-01-17 08:58:18
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载