0

MainActivity.java我需要制作一个状态栏通知,并且我从研究中获得了此代码,但是当我将代码放入AIDE时,它似乎并没有奏效

NotificationManager
mNotificationManager = (NotificationManager)
getSytemService(Context.N);
Notification notification = new notification(R.drawble.ic_launcher,
"Notification Test", System.currentT;
Context context = getApplicationContext();
CharSequence contentTitle = "My notification Title";
CharSequence contentText ="This is the message";
Intent notificationIntent = new Intent(this, MainActivity.class);

错误似乎Line 3与出现错误消息的(Context.N)位置有关: 。另一条错误消息在第五行,它读取的错误消息为.NUnknown member 'N' of 'android.content.Context'System.currentTUnknown member 'currentT' of 'java.lang.Sytem'

4

1 回答 1

0

Context.N应该是Context.NOTIFICATION_SERVICE

System.currentT应该是System.currentTimeMillis()

似乎您正在从某个地方复制代码,但代码被截断了?


这是生成通知的示例方法。

这是基于 Android 的文档:Building a Notification。有关更多详细信息(例如如何显示进度条或扩展视图),请阅读文档。

private static void generateNotification(Context context, String messageTitle, String messageText)
{
    // Get notification manager.
    final NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Setup notification builder.
    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(messageTitle)
        .setContentText(messageText)
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL);

    // Create intent.
    final Intent resultIntent = new Intent(context, MainActivity.class);

    // Setup task stack builder.
    final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(MainActivity.class);
    taskStackBuilder.addNextIntent(resultIntent);

    // Create pending intent.
    final PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    // Update notification builder.
    notificationBuilder.setContentIntent(resultPendingIntent);

    // Post notification.
    notificationManager.notify(0, notificationBuilder.build());
}

该类NotificationCompat.Builder需要 Android 的第 4 版支持库

于 2013-02-02T03:10:01.597 回答