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

相关文章

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

SpringIntegration消息路由之Router的条件路由与过滤功能

《SpringIntegration消息路由之Router的条件路由与过滤功能》本文详细介绍了Router的基础概念、条件路由实现、基于消息头的路由、动态路由与路由表、消息过滤与选择性路由以及错误处理... 目录引言一、Router基础概念二、条件路由实现三、基于消息头的路由四、动态路由与路由表五、消息过滤

一文教你如何将maven项目转成web项目

《一文教你如何将maven项目转成web项目》在软件开发过程中,有时我们需要将一个普通的Maven项目转换为Web项目,以便能够部署到Web容器中运行,本文将详细介绍如何通过简单的步骤完成这一转换过程... 目录准备工作步骤一:修改​​pom.XML​​1.1 添加​​packaging​​标签1.2 添加