android 360状态栏显示,Android项目仿UC浏览器和360手机卫士消息常驻栏(通知栏)...

本文主要是介绍android 360状态栏显示,Android项目仿UC浏览器和360手机卫士消息常驻栏(通知栏)...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前网上看了下自定义消息栏,通知栏,了解到了Notification这个控件,发现UC浏览器等都是这种类型,今天写个demo实现下,如图:

445ab7c812c32c0f5ff5774ce679a597.png

其中每个按钮都有不同的功能,代码如下:

package com.example.textwsjdemo;

import android.app.Activity;

import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.RemoteViews;

import android.widget.Toast;

public class MainActivity extends Activity {

private Button bt_hehe;

private NotificationManager notificationManager;

private Notification notification;

private int icon;

private CharSequence tickerText;

private long when;

RemoteViews contentView;

private Intent intent;

private PendingIntent pendingIntent;

private int notification_id = 0;

private MyBroadCast receiver;

private static String ACTION = "a";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

receiver = new MyBroadCast();

IntentFilter filter = new IntentFilter();

filter.addAction("a");

filter.addAction("b");

filter.addAction("c");

filter.addAction("d");

registerReceiver(receiver, filter);

initView();

initData();

}

private void initData() {

icon = R.drawable.ic_launcher; // 通知图标

tickerText = "Hello"; // 状态栏显示的通知文本提示

when = System.currentTimeMillis(); // 通知产生的时间,会在通知信息里显示

}

private void initView() {

bt_hehe = (Button) findViewById(R.id.bt_hehe);

bt_hehe.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

// 启动提示栏

createNotification();

}

});

}

private void createNotification() {

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notification = new Notification();

notification.icon = icon;

notification.tickerText = tickerText;

notification.when = when;

/***

* 在这里我们用自定的view来显示Notification

*/

contentView = new RemoteViews(getPackageName(),

R.layout.notification_item);

contentView.setTextViewText(R.id.text11, "小说");

contentView.setTextViewText(R.id.text22, "视频");

contentView.setTextViewText(R.id.text33, "新闻");

contentView.setTextViewText(R.id.text44, "扯淡");

// contentView.setTextViewText(R.id.notificationPercent, "0%");

// contentView.setProgressBar(R.id.notificationProgress, 100, 0, false);

// //进度条

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

// //加载图片

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

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

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

// contentView.setTextViewText(R.id.text,"Hello,this message is in a custom expanded view");

// //文本

notification.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻,不能滑动取消

//默认跳转的主界面

intent = new Intent(this, MainActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

//自定义跳转

contentView.setOnClickPendingIntent(R.id.ll_11, PendingIntent.getBroadcast(MainActivity.this, 11, new Intent().setAction("a"), PendingIntent.FLAG_UPDATE_CURRENT));

contentView.setOnClickPendingIntent(R.id.ll_22, PendingIntent.getBroadcast(MainActivity.this, 11, new Intent().setAction("b"), PendingIntent.FLAG_UPDATE_CURRENT));

contentView.setOnClickPendingIntent(R.id.ll_33, PendingIntent.getBroadcast(MainActivity.this, 11, new Intent().setAction("c"), PendingIntent.FLAG_UPDATE_CURRENT));

contentView.setOnClickPendingIntent(R.id.ll_44, PendingIntent.getBroadcast(MainActivity.this, 11, new Intent().setAction("d"), PendingIntent.FLAG_UPDATE_CURRENT));

notification.contentView = contentView;

notification.contentIntent = pendingIntent;

notificationManager.notify(notification_id, notification);

}

// 取消通知

private void cancelNotification() {

notificationManager.cancelAll();

}

@Override

protected void onDestroy() {

cancelNotification();

unregisterReceiver(receiver);

}

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

if ((keyCode == KeyEvent.KEYCODE_BACK)) {

System.out.println("按下了back键 onKeyDown()");

cancelNotification();

}

return super.onKeyDown(keyCode, event);

}

class MyBroadCast extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

if(intent.getAction().equals("a")){

Toast.makeText(MainActivity.this, "11111111111111",

Toast.LENGTH_LONG).show();

startActivity(new Intent(MainActivity.this, ActivityText1.class));

}

if(intent.getAction().equals("b")){

Toast.makeText(MainActivity.this, "222222222222222",

Toast.LENGTH_LONG).show();

startActivity(new Intent(MainActivity.this, ActivityText2.class));

}

if(intent.getAction().equals("c")){

Toast.makeText(MainActivity.this, "333333333333",

Toast.LENGTH_LONG).show();

startActivity(new Intent(MainActivity.this, ActivityText3.class));

}

if(intent.getAction().equals("d")){

Toast.makeText(MainActivity.this, "4444444444444",

Toast.LENGTH_LONG).show();

startActivity(new Intent(MainActivity.this, ActivityText4.class));

}

}

}

}

以下是一些属性的设置:

/*

* 添加声音

* notification.defaults |=Notification.DEFAULT_SOUND;

* 或者使用以下几种方式

* notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

* notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

* 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT"

* 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音

*/

/*

* 添加振动

* notification.defaults |= Notification.DEFAULT_VIBRATE;

* 或者可以定义自己的振动模式:

* long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒

* notification.vibrate = vibrate;

* long数组可以定义成想要的任何长度

* 如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动

*/

/*

* 添加LED灯提醒

* notification.defaults |= Notification.DEFAULT_LIGHTS;

* 或者可以自己的LED提醒模式:

* notification.ledARGB = 0xff00ff00;

* notification.ledOnMS = 300; //亮的时间

* notification.ledOffMS = 1000; //灭的时间

* notification.flags |= Notification.FLAG_SHOW_LIGHTS;

*/

/*

* 更多的特征属性

* notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知

* notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知

* notification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中

* notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,

* //经常与FLAG_ONGOING_EVENT一起使用

* notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部

* //如果要使用此字段,必须从1开始

* notification.iconLevel = ; //

最后附上源码:源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得牛网。

这篇关于android 360状态栏显示,Android项目仿UC浏览器和360手机卫士消息常驻栏(通知栏)...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/214745

相关文章

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

MySQL版本问题导致项目无法启动问题的解决方案

《MySQL版本问题导致项目无法启动问题的解决方案》本文记录了一次因MySQL版本不一致导致项目启动失败的经历,详细解析了连接错误的原因,并提供了两种解决方案:调整连接字符串禁用SSL或统一MySQL... 目录本地项目启动报错报错原因:解决方案第一个:第二种:容器启动mysql的坑两种修改时区的方法:本地

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

springboot项目中使用JOSN解析库的方法

《springboot项目中使用JOSN解析库的方法》JSON,全程是JavaScriptObjectNotation,是一种轻量级的数据交换格式,本文给大家介绍springboot项目中使用JOSN... 目录一、jsON解析简介二、Spring Boot项目中使用JSON解析1、pom.XML文件引入依

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A