【问题标题】:Android Studio Notification: use Android.support.v4.app or v7?Android Studio 通知:使用 Android.support.v4.app 还是 v7?
【发布时间】:2016-06-10 16:54:14
【问题描述】:

我正在使用开发者网站上的一些代码构建我的第一个通知:

import android.support.v7.app.NotificationCompat; //added by Android Studio
[...]
NotificationCompat.Builder mBuilder =
   new NotificationCompat.Builder(this)
   .setSmallIcon(R.drawable.notification_icon)
   .setContentTitle("My notification")
   .setContentText("Hello World!");

但是当我这样做时,Android Studio 会抛出一个错误:

required: android.support.v7.app.notificationCombat.Builder
found:    android.support.v4.app.notificationCombat.Builder 

似乎所有解决方案都围绕着我是否要为此指定 v4 或 v7。我没有导入 v4,所以我假设这是原生的?否则我不确定为什么当它们的拼写完全相同时它会说它找到了 v4。据我了解,该软件包提供了向后兼容性支持,所以 v7 总是会更好吗?

目标 sdk 为 23,最小 sdk:16

【问题讨论】:

  • 你在grade中导入了什么?
  • 只需将 import 语句中的 android.support.v7.app.notificationCombat.Builder 更改为 android.support.v4.app.notificationCombat.Builder ,伙计。它们是相同的,但如果您需要更多通知功能,请使用 v7。

标签: android android-studio backwards-compatibility


【解决方案1】:

只需在创建object 之前添加NotificationCompat.Builder

NotificationCompat.Builder mBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setSmallIcon(android.R.drawable.ic_notification_overlay)
                        .setContentTitle("Works!")
                        .setContentText("Works!");

【讨论】:

    【解决方案2】:

    现在不推荐使用 V7,建议使用 v4 作为替代。

    【讨论】:

      猜你喜欢
      • 2014-05-27
      • 2019-02-22
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      相关资源
      最近更新 更多