创建持久性通知并阻止状态栏中的通知

时间:2012-08-30 21:07:56

标签: android notifications

我有以下代码用于Android应用程序:

package com.authorwjf.e_notifications;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Bitmap bm = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.avatar), 
                getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width),
                getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height), 
                true);
        Intent intent = new Intent(this, Main.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 01, intent, Intent.FLAG_ACTIVITY_CLEAR_TASK);
        Notification.Builder builder = new Notification.Builder(getApplicationContext());
        builder.setContentTitle("This is the title");
        builder.setContentText("This is the text");
        builder.setSubText("Some sub text");
        builder.setNumber(101);
        builder.setContentIntent(pendingIntent);
        builder.setTicker("Fancy Notification");
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setLargeIcon(bm);
        builder.setAutoCancel(true);
        builder.setPriority(0);
        Notification notification = builder.build();
        NotificationManager notificationManger = 
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManger.notify(01, notification);        
    }

}

基本上推出的应用会创建通知,现在我有几个问题:

  • 启动应用时是否可以在下拉通知列表中显示通知,但状态栏中没有图标。即。参考

http://imagebin.org/226494

是否可以显示红色圆圈图标?最后,我想创建一个只停留在下拉通知中的服务。

我是Android dev的新手,只是试图掌握我可以用通知做什么。

由于

2 个答案:

答案 0 :(得分:46)

  • A1:要删除状态栏图标,请使用此技巧:

    builder.setSmallIcon(android.R.color.transparent); //Tested and worked in API 14
    
  • A2:要制作持续通知,请添加以下行:

    builder.setOngoing(true)
    

答案 1 :(得分:2)

看看ongoing event flag。我相信这可以创建类似于系统的wifi和USB连接通知的持续通知。