詳解Android中Notification通知提醒
在消息通知時(shí),我們經(jīng)常用到兩個(gè)組件Toast和Notification。特別是重要的和需要長(zhǎng)時(shí)間顯示的信息,用Notification就最 合適不過(guò)了。當(dāng)有消息通知時(shí),狀態(tài)欄會(huì)顯示通知的圖標(biāo)和文字,通過(guò)下拉狀態(tài)欄,就可以看到通知信息了,Android這一創(chuàng)新性的UI組件贏得了用戶的一 致好評(píng),就連蘋果也開始模仿了。今天我們就結(jié)合實(shí)例,探討一下Notification具體的使用方法。 首先說(shuō)明一下我們需要實(shí)現(xiàn)的功能是:在程序啟動(dòng)時(shí),發(fā)出一個(gè)通知,這個(gè)通知在軟件運(yùn)行過(guò)程中一直存在,相當(dāng)于qq的托盤一樣。
然后再演示一下普通的通知和自定義視圖通知, 那我們就先建立一個(gè)安卓項(xiàng)目。
然后編輯/res/layout/main.xml文件,代碼如下:
<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:background="@android:color/black" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/title" android:textColor="#0f0" android:textSize="20sp" android:textStyle="bold" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="30dp" android:onClick="normal" android:text="@string/notification" android:textColor="#0f0" android:textSize="20sp" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="50dp" android:layout_marginRight="50dp" android:layout_marginTop="30dp" android:onClick="custom" android:text="@string/custom" android:textColor="#0f0" android:textSize="20sp" /> </LinearLayout>
上面的布局很簡(jiǎn)單,有兩個(gè)按鈕分別用于啟動(dòng)普通的notification和自定義的notification。
接下來(lái)自定義一個(gè)布局用于顯示自定義的通知的。
<?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:background="#000" android:orientation="vertical" > <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="wrap_content" android:contentDescription="@string/action_settings" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textColor="#0f0" android:textSize="15sp" /> </LinearLayout>
接下來(lái)就是上代碼。
package com.itfom.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.RemoteViews; public class MainActivity extends Activity { private NotificationManager mNotificationManager; private Context context; private Notification notification; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //普通的通知 @SuppressWarnings("deprecation") public void normal(View v){ //創(chuàng)建通知 createNotification("普通的通知"); //把通知放在正在運(yùn)行欄目中 notification.flags|=Notification.FLAG_ONGOING_EVENT; //設(shè)定默認(rèn)聲音 notification.defaults|=Notification.DEFAULT_SOUND; //設(shè)定默認(rèn)震動(dòng) notification.defaults|=Notification.DEFAULT_VIBRATE; //設(shè)定默認(rèn)LED燈提醒 notification.defaults|=Notification.DEFAULT_LIGHTS; //設(shè)置點(diǎn)擊后通知自動(dòng)清除 notification.defaults|=Notification.FLAG_AUTO_CANCEL; String textTitle="Notification示例"; String textContent="程序正在運(yùn)行,點(diǎn)擊此處跳轉(zhuǎn)到演示界面"; Intent it=new Intent(context, MainActivity.class); PendingIntent pendintent=PendingIntent.getActivity(context, 0, it, 0); notification.setLatestEventInfo(context, textTitle, textContent, pendintent); mNotificationManager.notify(0, notification); } //自定義的通知 public void custom(View v){ //創(chuàng)建通知 createNotification("個(gè)性化的通知"); //自定義通知的聲音 notification.sound=Uri.parse(Environment.getExternalStorageDirectory()+"/non.mp3"); //自定義震動(dòng)參數(shù)分別為多長(zhǎng)時(shí)間開始震動(dòng)、第一次震動(dòng)的時(shí)間、停止震動(dòng)的時(shí)間 long[] vibrate={0,100,200,300}; notification.vibrate=vibrate; //自定義閃光燈的方式 notification.ledARGB=0xff00ff00; notification.ledOnMS=500; notification.ledOffMS=500; notification.flags|=Notification.FLAG_SHOW_LIGHTS; RemoteViews contentView=new RemoteViews(this.getPackageName(),R.layout.notify); contentView.setTextViewText(R.id.tv, "這是個(gè)性化的通知"); //指定個(gè)性化的視圖 notification.contentView=contentView; Intent it=new Intent(context, MainActivity.class); PendingIntent pendintent=PendingIntent.getActivity(context, 0, it, 0); //指定內(nèi)容視圖 notification.contentIntent=pendintent; mNotificationManager.notify(1, notification); } //自定義一個(gè)方法創(chuàng)建通知 @SuppressWarnings("deprecation") public Notification createNotification(String text){ context = this; mNotificationManager=(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); int icon=R.drawable.ic_launcher; long when=System.currentTimeMillis(); return notification = new Notification(icon, text, when); } //重寫onBackpressed事件 @Override public void onBackPressed() { super.onBackPressed(); finish(); //取消通知 mNotificationManager.cancel(0); android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0); } }
上面的代碼我們定義了一個(gè)方法createNotification(String text).該方法是用于創(chuàng)建一個(gè)通知。注意之所以這樣寫是因?yàn)椤2还苁瞧胀ǖ耐ㄖ€是自定義的通知。前面的創(chuàng)建過(guò)程都是一樣的。然后我們實(shí)現(xiàn)了兩個(gè)按鈕的點(diǎn)擊事件。
運(yùn)行結(jié)果如下所示:
當(dāng)點(diǎn)擊兩個(gè)按鈕時(shí)會(huì)出現(xiàn)以下的情況:
注 :這里是有聲音效果的。如果手機(jī)支持閃光,還有LED效果。
以上就是關(guān)于Android中Notification通知提醒實(shí)現(xiàn)的過(guò)程詳解,最近更新了許多關(guān)于Android中Notification通知提醒的文章,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Android中通知Notification使用實(shí)例(振動(dòng)、燈光、聲音)
- Android中通過(guò)Notification&NotificationManager實(shí)現(xiàn)消息通知
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android開發(fā) -- 狀態(tài)欄通知Notification、NotificationManager詳解
- android中創(chuàng)建通知欄Notification代碼實(shí)例
- Android 中Notification彈出通知實(shí)現(xiàn)代碼
- Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
- Android 通知使用權(quán)(NotificationListenerService)的使用
- Android種使用Notification實(shí)現(xiàn)通知管理以及自定義通知欄實(shí)例(示例四)
- Android中的Notification機(jī)制深入理解
相關(guān)文章
android中WebView和javascript實(shí)現(xiàn)數(shù)據(jù)交互實(shí)例
這篇文章主要介紹了android中WebView和javascript實(shí)現(xiàn)數(shù)據(jù)交互實(shí)例,需要的朋友可以參考下2014-07-07Android開發(fā)之ContentProvider的使用詳解
本篇文章介紹了Android開發(fā)之ContentProvider的使用詳解。需要的朋友參考下2013-04-04Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)
這篇文章主要介紹了Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android實(shí)現(xiàn)有視差效果的ListView
這篇文章給大家詳解介紹了在Android中如何實(shí)現(xiàn)帶有視差效果的ListView,文章給出了示例代碼相信對(duì)大家的理解和學(xué)習(xí)更有幫助,有需要的朋友們下面來(lái)一起看看吧。2016-09-09詳解Android App中的AsyncTask異步任務(wù)執(zhí)行方式
這篇文章主要介紹了Android App中的AsyncTask異步任務(wù)執(zhí)行方式,文中舉了一個(gè)打開網(wǎng)絡(luò)圖片的例子幫助大家直觀理解,需要的朋友可以參考下2016-04-04Android使用ViewPager實(shí)現(xiàn)滾動(dòng)廣告
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)滾動(dòng)廣告,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11詳解Android中使用OkHttp發(fā)送HTTP的post請(qǐng)求的方法
OkHttp(github.com/square/okhttp)是近來(lái)人氣迅速攀升的一款第三方安卓HTTP支持包,這里我們就來(lái)詳解Android中使用OkHttp發(fā)送HTTP的post請(qǐng)求的方法2016-07-07