Android高仿UC浏览器和360手机卫士消息常驻栏(通知栏)

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


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

[java]  view plain  copy
  1. package com.example.textwsjdemo;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Notification;  
  5. import android.app.NotificationManager;  
  6. import android.app.PendingIntent;  
  7. import android.content.BroadcastReceiver;  
  8. import android.content.Context;  
  9. import android.content.Intent;  
  10. import android.content.IntentFilter;  
  11. import android.os.Bundle;  
  12. import android.view.KeyEvent;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.widget.Button;  
  16. import android.widget.RemoteViews;  
  17. import android.widget.Toast;  
  18.   
  19. public class MainActivity extends Activity {  
  20.   
  21.     private Button bt_hehe;  
  22.     private NotificationManager notificationManager;  
  23.     private Notification notification;  
  24.     private int icon;  
  25.     private CharSequence tickerText;  
  26.     private long when;  
  27.     RemoteViews contentView;  
  28.     private Intent intent;  
  29.     private PendingIntent pendingIntent;  
  30.     private int notification_id = 0;  
  31.     private MyBroadCast receiver;  
  32.     private static String ACTION = "a";  
  33.   
  34.     @Override  
  35.     protected void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.activity_main);  
  38.         receiver = new MyBroadCast();  
  39.         IntentFilter filter = new IntentFilter();  
  40.         filter.addAction("a");  
  41.         filter.addAction("b");  
  42.         filter.addAction("c");  
  43.         filter.addAction("d");  
  44.         registerReceiver(receiver, filter);  
  45.   
  46.         initView();  
  47.         initData();  
  48.   
  49.     }  
  50.   
  51.     private void initData() {  
  52.         icon = R.drawable.ic_launcher; // 通知图标  
  53.         tickerText = "Hello"// 状态栏显示的通知文本提示  
  54.         when = System.currentTimeMillis(); // 通知产生的时间,会在通知信息里显示  
  55.     }  
  56.   
  57.     private void initView() {  
  58.         bt_hehe = (Button) findViewById(R.id.bt_hehe);  
  59.         bt_hehe.setOnClickListener(new OnClickListener() {  
  60.   
  61.             @Override  
  62.             public void onClick(View v) {  
  63.                 // TODO Auto-generated method stub  
  64.                 // 启动提示栏  
  65.                 createNotification();  
  66.             }  
  67.         });  
  68.     }  
  69.   
  70.     private void createNotification() {  
  71.         notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
  72.         notification = new Notification();  
  73.         notification.icon = icon;  
  74.         notification.tickerText = tickerText;  
  75.         notification.when = when;  
  76.   
  77.         /*** 
  78.          * 在这里我们用自定的view来显示Notification 
  79.          */  
  80.         contentView = new RemoteViews(getPackageName(),  
  81.                 R.layout.notification_item);  
  82.         contentView.setTextViewText(R.id.text11, "小说");  
  83.         contentView.setTextViewText(R.id.text22, "视频");  
  84.         contentView.setTextViewText(R.id.text33, "新闻");  
  85.         contentView.setTextViewText(R.id.text44, "扯淡");  
  86.         // contentView.setTextViewText(R.id.notificationPercent, "0%");  
  87.         // contentView.setProgressBar(R.id.notificationProgress, 100, 0, false);  
  88.         // //进度条  
  89.         // contentView.setImageViewResource(R.id.image,R.drawable.more_advice);  
  90.         // //加载图片  
  91.         // contentView.setImageViewResource(R.id.image,R.drawable.more_attention);  
  92.         // contentView.setImageViewResource(R.id.image,R.drawable.more_evaluate);  
  93.         // contentView.setImageViewResource(R.id.image,R.drawable.more_about);  
  94.         // contentView.setTextViewText(R.id.text,"Hello,this message is in a custom expanded view");  
  95.         // //文本  
  96.   
  97.           
  98.         notification.flags = Notification.FLAG_ONGOING_EVENT; // 设置常驻,不能滑动取消  
  99.         //默认跳转的主界面  
  100.         intent = new Intent(this, MainActivity.class);  
  101.         intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
  102.         pendingIntent = PendingIntent.getActivity(this0, intent, 0);  
  103.           
  104.         //自定义跳转  
  105.         contentView.setOnClickPendingIntent(R.id.ll_11, PendingIntent.getBroadcast(MainActivity.this11new Intent().setAction("a"), PendingIntent.FLAG_UPDATE_CURRENT));  
  106.         contentView.setOnClickPendingIntent(R.id.ll_22, PendingIntent.getBroadcast(MainActivity.this11new Intent().setAction("b"), PendingIntent.FLAG_UPDATE_CURRENT));  
  107.         contentView.setOnClickPendingIntent(R.id.ll_33, PendingIntent.getBroadcast(MainActivity.this11new Intent().setAction("c"), PendingIntent.FLAG_UPDATE_CURRENT));  
  108.         contentView.setOnClickPendingIntent(R.id.ll_44, PendingIntent.getBroadcast(MainActivity.this11new Intent().setAction("d"), PendingIntent.FLAG_UPDATE_CURRENT));  
  109.         notification.contentView = contentView;  
  110.         notification.contentIntent = pendingIntent;  
  111.         notificationManager.notify(notification_id, notification);  
  112.     }  
  113.   
  114.     // 取消通知  
  115.     private void cancelNotification() {  
  116.         notificationManager.cancelAll();  
  117.     }  
  118.   
  119.     @Override  
  120.     protected void onDestroy() {  
  121.         cancelNotification();  
  122.         unregisterReceiver(receiver);  
  123.   
  124.     }  
  125.   
  126.     @Override  
  127.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  128.         if ((keyCode == KeyEvent.KEYCODE_BACK)) {  
  129.             System.out.println("按下了back键   onKeyDown()");  
  130.             cancelNotification();  
  131.         }  
  132.         return super.onKeyDown(keyCode, event);  
  133.     }  
  134.       
  135.     class MyBroadCast extends BroadcastReceiver {  
  136.   
  137.         @Override  
  138.         public void onReceive(Context context, Intent intent) {  
  139.             if(intent.getAction().equals("a")){  
  140.                 Toast.makeText(MainActivity.this"11111111111111",  
  141.                         Toast.LENGTH_LONG).show();  
  142.                 startActivity(new Intent(MainActivity.this, ActivityText1.class));  
  143.             }  
  144.             if(intent.getAction().equals("b")){  
  145.                 Toast.makeText(MainActivity.this"222222222222222",  
  146.                         Toast.LENGTH_LONG).show();  
  147.                 startActivity(new Intent(MainActivity.this, ActivityText2.class));  
  148.             }  
  149.             if(intent.getAction().equals("c")){  
  150.                 Toast.makeText(MainActivity.this"333333333333",  
  151.                         Toast.LENGTH_LONG).show();  
  152.                 startActivity(new Intent(MainActivity.this, ActivityText3.class));  
  153.             }  
  154.             if(intent.getAction().equals("d")){  
  155.                 Toast.makeText(MainActivity.this"4444444444444",  
  156.                         Toast.LENGTH_LONG).show();  
  157.                 startActivity(new Intent(MainActivity.this, ActivityText4.class));  
  158.             }  
  159.               
  160.         }  
  161.   
  162.     }  
  163.   
  164. }  

以下是一些属性的设置:

[java]  view plain  copy
  1. /* 
  2. * 添加声音 
  3. * notification.defaults |=Notification.DEFAULT_SOUND; 
  4. * 或者使用以下几种方式 
  5. * notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3"); 
  6. * notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6"); 
  7. * 如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT" 
  8. * 如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音 
  9. */  
  10. /* 
  11. * 添加振动 
  12. * notification.defaults |= Notification.DEFAULT_VIBRATE; 
  13. * 或者可以定义自己的振动模式: 
  14. * long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒 
  15. * notification.vibrate = vibrate; 
  16. * long数组可以定义成想要的任何长度 
  17. * 如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动 
  18. */  
  19. /* 
  20. * 添加LED灯提醒 
  21. * notification.defaults |= Notification.DEFAULT_LIGHTS; 
  22. * 或者可以自己的LED提醒模式: 
  23. * notification.ledARGB = 0xff00ff00; 
  24. * notification.ledOnMS = 300; //亮的时间 
  25. * notification.ledOffMS = 1000; //灭的时间 
  26. * notification.flags |= Notification.FLAG_SHOW_LIGHTS; 
  27. */  
  28. /* 
  29. * 更多的特征属性 
  30. * notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知 
  31. * notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知 
  32. * notification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中 
  33. * notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除, 
  34. * //经常与FLAG_ONGOING_EVENT一起使用 
  35. * notification.number = 1; //number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部 
  36. * //如果要使用此字段,必须从1开始 
  37. * notification.iconLevel = ; // 
  38. */  
demo地址
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值