android状态栏自定义,[Android开发]Android 自定义状态栏通知

超、凢脫俗 2022-10-16 05:47 407阅读 0赞

状态栏通知布局

custom_notification.xml

android:layout_width=”match_parent”

android:layout_height=”64dp” //这里不用match_parent,因为在有些机型上显示的背景不全,源码中的高度是64dp,这样背景能全部覆盖通知栏的背景

android:background=”@color/white” >

android:id=”@+id/image”

android:layout_width=”40dip”

android:layout_height=”40dip”

android:layout_alignParentLeft=”true”

android:layout_centerVertical=”true”

android:layout_marginLeft=”10dp”

android:layout_marginRight=”10dp”

android:contentDescription=”@string/Image” />

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_centerVertical=”true”

android:layout_toRightOf=”@id/image” >

android:id=”@+id/title”

style=”@style/NotificationTitle”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_alignParentTop=”true” />

android:id=”@+id/text”

style=”@style/NotificationText”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_below=”@id/title”

android:ellipsize=”end”

android:lines=”2” />

android:id=”@+id/time”

style=”@style/NotificationText”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_alignBottom=”@id/title”

android:layout_alignParentRight=”true”

android:layout_marginRight=”5dip”

android:layout_toLeftOf=”@id/title” />

```

这里面的style都是使用的继承系统的文字样式

```xml

#bb000000

@dimen/notification_text_size

#bb000000

/**

* 自定义通知

*/

private void createCustomNotification(Context ctxt, String tickerText,

int drawable, String title, String content, int id,

PendingIntent pendingIntent) {

int icon = R.drawable.ic_launcher;

long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);//必须要有这三个参数,不然出来的状态栏显示不全

RemoteViews contentView = new RemoteViews(mContext.getPackageName(),

R.layout.custom_notification);

contentView.setImageViewResource(R.id.image, drawable);

contentView.setTextViewText(R.id.title, title);

contentView.setTextViewText(R.id.text, content);

SimpleDateFormat df = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);// 设置日期格式

String time = df.format(new Date());

contentView.setTextViewText(R.id.time,

time.substring((time.length() - 8), (time.length() - 3)));

notification.contentView = contentView;

notification.contentIntent = pendingIntent;

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notification.defaults = Notification.DEFAULT_SOUND;

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager) ctxt

.getSystemService(ns);

mNotificationManager.notify(id, notification);

}

/**

*状态栏通知

*/

public void stateBar(View view) {

// 1.获取通知管理器

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

// 2.创建通知对象

int icon = R.drawable.icon; // 图标

CharSequence tickerText = “下载完成”; // 提示文本

long when = System.currentTimeMillis(); // 时间

Notification notification = new Notification(icon, tickerText, when);

// 3.设置通知

Context context = getApplicationContext(); // 上下文环境

String contentTitle = “FeiQ下载完成”; // 消息标题

String contentText = “FeiQ.exe下载完成, 耗时1分35秒”; // 消息内容

Intent intent = new Intent(); // 用来开启Activity的意图

intent.setClassName(“com.itheima.downloader”, “com.itheima.downloader.MainActivity”); // 意图指定Activity

PendingIntent pedningIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_ONE_SHOT); // 定义待定意图,第三个参数就是指定如果多个状态栏通知时,后面的通知怎么处理前面的通知

notification.setLatestEventInfo(context, contentTitle, contentText, pedningIntent); // 设置通知的具体信息

notification.flags = Notification.FLAG_AUTO_CANCEL; // 设置自动清除,如果设置为FLAG_NO_CLEAR就是点击不会删除,像安全卫士程序的状态栏图标点击就不会删除

// 设置通知的声音

notification.sound = Uri.parse(“file:///mnt/sdcard/jiaodizhu.mp3”);

// 4.发送通知

manager.notify(id++, notification);

}

/*这个PendingIntent是指定点击这个状态栏通知的时候的点击事件,注意点击的时候这个状态栏其实是运行在系统的程序中,并不是运行在我们自己的程序中,

而这个pendingIntent就是指定在点击这个通知的时候怎么让别的程序来执行的意图,就是包装出来一个动作让另外一个程序使用、

这个自动清除就是点击一次之后这个通知就在状态栏中自动清除了,

发送通知时的id++就是每次的id值,不同则你发一次通知状态栏上就会显示一个,点多次就发送多个,如果设置为1,则不管点多少次后面的都把前面的给覆盖了*/

发表评论

表情:
评论列表 (有 0 条评论,407人围观)

还没有评论,来说两句吧...

相关阅读