Android项目仿UC浏览器和360手机卫士消息常驻栏(通知栏)
短信预约 -IT技能 免费直播动态提醒
之前网上看了下自定义消息栏,通知栏,了解到了Notification这个控件,发现UC浏览器等都是这种类型,今天写个demo实现下,如图:
其中每个按钮都有不同的功能,代码如下:
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;
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.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,QQ管家那样的悬浮窗Android仿360悬浮小球自定义view实现示例Android实现仿360桌面悬浮清理内存android仿360加速球实现内存释放Android开发实现模仿360二维码扫描功能实例详解Android静默安装实现方案 仿360手机助手秒装和智能安装功能Android实现360手机助手底部的动画菜单Android菜单(动画菜单、360波纹菜单)Android仿360桌面手机卫士悬浮窗效果Android使用自定义View实现360手机卫士波浪球进度的效果
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341