Android 服务完成时在状态栏上发送通知

Android 服务完成时在状态栏上发送通知,android,Android,我有一个应用程序正在使用服务下载图像,但我希望在下载完成时通知用户,即使用户在应用程序之外(状态栏中的通知),也可以在minSdkVersion=“8”中使用,我知道有一些关于此的答案,但我没有收到通知:以下是我的一些代码(只有通知是问题所在时,它才起作用): 如何修改sendNotification()?? 谢谢试试这个: private void sendNotification(){ CharSequence title = "The title"; CharSequen

我有一个应用程序正在使用服务下载图像,但我希望在下载完成时通知用户,即使用户在应用程序之外(状态栏中的通知),也可以在minSdkVersion=“8”中使用,我知道有一些关于此的答案,但我没有收到通知:以下是我的一些代码(只有通知是问题所在时,它才起作用):

如何修改sendNotification()?? 谢谢

试试这个:

private void sendNotification(){

    CharSequence title = "The title";
    CharSequence message = "My message";

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                      .setSmallIcon(R.drawable.ic_launcher)
                      .setAutoCancel(true)
                      .setContentTitle(title);

    mBuilder.setContentText(message);
    mBuilder.setTicker(message);
    mBuilder.setWhen(System.currentTimeMillis());

    NotificationManager notificationManager =   
           (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    mBuilder.setContentIntent(pendingIntent);
    notificationManager.notify(0, mBuilder.build());
}
试试这个:

private void sendNotification(){

    CharSequence title = "The title";
    CharSequence message = "My message";

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                      .setSmallIcon(R.drawable.ic_launcher)
                      .setAutoCancel(true)
                      .setContentTitle(title);

    mBuilder.setContentText(message);
    mBuilder.setTicker(message);
    mBuilder.setWhen(System.currentTimeMillis());

    NotificationManager notificationManager =   
           (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    mBuilder.setContentIntent(pendingIntent);
    notificationManager.notify(0, mBuilder.build());
}
试试这个:

private void sendNotification(){

    CharSequence title = "The title";
    CharSequence message = "My message";

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                      .setSmallIcon(R.drawable.ic_launcher)
                      .setAutoCancel(true)
                      .setContentTitle(title);

    mBuilder.setContentText(message);
    mBuilder.setTicker(message);
    mBuilder.setWhen(System.currentTimeMillis());

    NotificationManager notificationManager =   
           (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    mBuilder.setContentIntent(pendingIntent);
    notificationManager.notify(0, mBuilder.build());
}
试试这个:

private void sendNotification(){

    CharSequence title = "The title";
    CharSequence message = "My message";

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                      .setSmallIcon(R.drawable.ic_launcher)
                      .setAutoCancel(true)
                      .setContentTitle(title);

    mBuilder.setContentText(message);
    mBuilder.setTicker(message);
    mBuilder.setWhen(System.currentTimeMillis());

    NotificationManager notificationManager =   
           (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
    mBuilder.setContentIntent(pendingIntent);
    notificationManager.notify(0, mBuilder.build());
}