Android項目仿UC瀏覽器和360手機衛(wèi)士消息常駐欄(通知欄)
之前網(wǎng)上看了下自定義消息欄,通知欄,了解到了Notification這個控件,發(fā)現(xiàn)UC瀏覽器等都是這種類型,今天寫個demo實現(xiàn)下,如圖:

其中每個按鈕都有不同的功能,代碼如下:
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"; // 狀態(tài)欄顯示的通知文本提示
when = System.currentTimeMillis(); // 通知產(chǎn)生的時間,會在通知信息里顯示
}
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; // 設(shè)置常駐,不能滑動取消
//默認跳轉(zhuǎn)的主界面
intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
//自定義跳轉(zhuǎn)
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));
}
}
}
}
以下是一些屬性的設(shè)置:
/*
* 添加聲音
* notification.defaults |=Notification.DEFAULT_SOUND;
* 或者使用以下幾種方式
* notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
* notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
* 如果想要讓聲音持續(xù)重復(fù)直到用戶對通知做出反應(yīng),則可以在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數(shù)組可以定義成想要的任何長度
* 如果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; //重復(fù)發(fā)出聲音,直到用戶響應(yīng)此通知
* notification.flags |= FLAG_ONGOING_EVENT; //將此通知放到通知欄的"Ongoing"即"正在運行"組中
* notification.flags |= FLAG_NO_CLEAR; //表明在點擊了通知欄中的"清除通知"后,此通知不清除,
* //經(jīng)常與FLAG_ONGOING_EVENT一起使用
* notification.number = 1; //number字段表示此通知代表的當(dāng)前事件數(shù)量,它將覆蓋在狀態(tài)欄圖標的頂部
* //如果要使用此字段,必須從1開始
* notification.iconLevel = ; //
最后附上源碼:源碼下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)類似360,QQ管家那樣的懸浮窗
- Android仿360懸浮小球自定義view實現(xiàn)示例
- Android實現(xiàn)仿360桌面懸浮清理內(nèi)存
- android仿360加速球?qū)崿F(xiàn)內(nèi)存釋放
- Android開發(fā)實現(xiàn)模仿360二維碼掃描功能實例詳解
- Android靜默安裝實現(xiàn)方案 仿360手機助手秒裝和智能安裝功能
- Android實現(xiàn)360手機助手底部的動畫菜單
- Android菜單(動畫菜單、360波紋菜單)
- Android仿360桌面手機衛(wèi)士懸浮窗效果
- Android使用自定義View實現(xiàn)360手機衛(wèi)士波浪球進度的效果
相關(guān)文章
Android使用Notification實現(xiàn)寬視圖通知欄(二)
這篇文章主要為大家詳細介紹了Android使用Notification實現(xiàn)寬視圖通知欄,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
新浪微博第三方登錄界面上下拉伸圖片之第三方開源PullToZoomListViewEx(二)
這篇文章主要介紹了新浪微博第三方登錄界面上下拉伸圖片之第三方開源PullToZoomListViewEx(二) 的相關(guān)資料,需要的朋友可以參考下2015-12-12
Android編程實現(xiàn)Listview點擊展開和隱藏的方法
這篇文章主要介紹了Android編程實現(xiàn)Listview點擊展開和隱藏的方法,涉及Android中Listview的響應(yīng)點擊與樣式變換相關(guān)操作技巧,需要的朋友可以參考下2015-12-12
Android 自動補全提示輸入AutoCompleteTextView、 MultiAutoCompleteTextV
本文主要介紹了Android自動補全提示輸入AutoCompleteTextView、 MultiAutoCompleteTextView,具有一定的參考作用,下面跟著小編一起來看下吧2017-01-01
Android通過AlarmManager類實現(xiàn)簡單鬧鐘功能
這篇文章主要為大家詳細介紹了Android通過AlarmManager類實現(xiàn)簡單鬧鐘功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-06-06
Android?Flutter繪制有趣的?loading加載動畫
在網(wǎng)絡(luò)速度較慢的場景,一個有趣的加載會提高用戶的耐心和對?App?的好感。本篇我們利用Flutter?的?PathMetric來玩幾個有趣的?loading?效果,感興趣的可以動手嘗試一下2022-07-07
Android轉(zhuǎn)場效果實現(xiàn)示例淺析
這篇文章主要為大家介紹了Android轉(zhuǎn)場效果實現(xiàn)示例淺析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02

