Android 构造函数通知(int、String、long)未定义

Android 构造函数通知(int、String、long)未定义,android,notifications,Android,Notifications,我正在尝试从主活动创建通知 这是我的密码- public class Notification extends Activity{ private static final int NOTIFY_ME_ID=1987; private int count=0; private NotificationManager mgr=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCr

我正在尝试从主活动创建通知

这是我的密码-

public class Notification extends Activity{

private static final int NOTIFY_ME_ID=1987;
private int count=0;
private NotificationManager mgr=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    mgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    notifyMe();

  }


public void notifyMe(){

Notification notification = new Notification(R.drawable.icon,
        "Notification text that will be shown on status bar.", 
        System.currentTimeMillis());

// The PendingIntent will launch activity if the user selects this
// notification
PendingIntent contentIntent = PendingIntent.getActivity(Notification.this,
        0, new Intent(Notification.this, MainActivity.class), 0);
notification.setLatestEventInfo(Notification.this, "Content Title", "Content text",
        contentIntent);
mgr.notify(NOTIFICATION_ID, notification);

}
但是我得到了以下错误:构造函数通知(int,String,long)未定义

我得到错误的行如下所示:

    Notification notification = new Notification(R.drawable.icon,
        "Notification text that will be shown on status bar.", 
        System.currentTimeMillis());

使用
Notification.Builder
创建通知,如图所示。

的构造函数没有问题。。问题在于您的活动名称,它也是
通知
。您应该更改为其他名称以解决此问题

不必更改活动名称,您可以包含程序包,让系统清楚地知道要使用哪个通知。请参见用户提供的系统32

android.app.Notification notification 
                = new android.app.Notification(icon, tickerText, when);

我建议将该活动重命名为
NotificationActivity

它是否适用于较旧的android版本?我仍然想知道我在这段代码中犯了什么错误。是的,以前的构造函数应该也可以工作,但只适用于API 9+。你在编译什么API?我自己就试过了,没有问题。检查导入并清理项目。