Android之開(kāi)發(fā)消息通知欄
一:先來(lái)效果圖
二:實(shí)現(xiàn)步驟
1.xml布局實(shí)現(xiàn)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="edu.feicui.notification.MainActivity"> <Button android:id="@+id/btn_create" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="發(fā)送通知" android:textSize="25sp" /> </LinearLayout>
2.activity的實(shí)現(xiàn)
package edu.feicui.notification; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.RemoteViews; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { /** * 通知欄Notification */ private NotificationManager mManager; private Notification mNotification; private PendingIntent mIntent; private String cll; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cll = "今年27號(hào)過(guò)年喲!"; ButterKnife.bind(this); } @Override public void onContentChanged() { super.onContentChanged(); init(); } private void init() { //初始化通知欄管理者 mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //意圖數(shù)組 Intent[] intents = {new Intent(this, NotificationAcitivity.class)}; //待處理意圖對(duì)象 mIntent = PendingIntent.getActivities(this, 0, intents, 0); //消息欄通知對(duì)象 mNotification = new Notification(); } @OnClick(R.id.btn_create) public void create() { //設(shè)置在通知欄的消息圖標(biāo) mNotification.icon = R.mipmap.logo_new; //設(shè)置在通知欄的信息內(nèi)容 mNotification.tickerText = "重大消息"; //設(shè)置默認(rèn)的聲音,此外還可以設(shè)置震動(dòng)(需加入權(quán)限) mNotification.defaults = Notification.DEFAULT_SOUND; //添加燈光 // mNotification.defaults=Notification.DEFAULT_LIGHTS; //不能刪除 mNotification.flags = Notification.FLAG_NO_CLEAR; //設(shè)置下拉時(shí)的顯示布局 RemoteViews convertView = new RemoteViews(getPackageName(), R.layout.layout_content); convertView.setImageViewResource(R.id.img, R.mipmap.logo_new); convertView.setTextViewText(R.id.txt, cll); mNotification.contentView = convertView; mNotification.contentIntent = mIntent; //發(fā)送通知 // 第一個(gè)參數(shù)唯一的標(biāo)識(shí)該Notification,第二個(gè)參數(shù)就是Notification對(duì)象 mManager.notify(1, mNotification); } }
3.AndroidManifest添加權(quán)限
<uses-permission android:name="android.permission.VIBRATE"/>
4.跳轉(zhuǎn)界面的xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff0000" android:textSize="20dp" android:text="今年27號(hào)過(guò)年喲!" /> </LinearLayout>
5.跳轉(zhuǎn)activity的實(shí)現(xiàn)
package edu.feicui.notification; import android.app.Activity; import android.app.NotificationManager; import android.os.Bundle; import android.widget.TextView; /** * Created by Administrator on 2017-1-20. */ public class NotificationAcitivity extends Activity { private NotificationManager mManager; private int index = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); //初始化通知欄管理者 mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); index = 2; mManager.cancelAll(); } }
簡(jiǎn)單粗暴實(shí)用,你值得擁有
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- Android中通過(guò)Notification&NotificationManager實(shí)現(xiàn)消息通知
- Android編程實(shí)現(xiàn)google消息通知功能示例
- Android消息通知欄的實(shí)現(xiàn)方法介紹
- Android自定義Notification添加點(diǎn)擊事件
- Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android編程使用Service實(shí)現(xiàn)Notification定時(shí)發(fā)送功能示例
- Android 通知使用權(quán)(NotificationListenerService)的使用
- android使用NotificationListenerService監(jiān)聽(tīng)通知欄消息
- Android消息通知Notification常用方法(發(fā)送消息和接收消息)
相關(guān)文章
Android編程實(shí)現(xiàn)設(shè)置按鈕背景透明與半透明及圖片背景透明的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)設(shè)置按鈕背景透明與半透明及圖片背景透明的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Button及ImageButton的背景屬性設(shè)置技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12詳解OpenGL Shader彩虹條紋效果的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何通過(guò)OpenGL Shader實(shí)現(xiàn)彩虹條紋效果,最后的效果和圖片處理軟件colorow中的彩虹效果濾鏡相似,需要的可以參考一下2022-02-02Android Handler中的休眠喚醒實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android Handler中的休眠喚醒實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Android AnalogClock簡(jiǎn)單使用方法實(shí)例
這篇文章主要介紹了Android AnalogClock簡(jiǎn)單使用方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了AnalogClock的布局調(diào)用技巧,需要的朋友可以參考下2016-01-01Android實(shí)現(xiàn)動(dòng)態(tài)向Gallery中添加圖片及倒影與3D效果示例
這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)向Gallery中添加圖片及倒影與3D效果的方法,涉及Android針對(duì)圖片的加載、顯示、翻轉(zhuǎn)、倒影等相關(guān)特效功能實(shí)現(xiàn)技巧2016-08-08