最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Android中Notification通知提醒

 更新時間:2016年01月24日 14:40:42   投稿:lijiao  
這篇文章主要為大家介紹了Android中Notification通知提醒,本文演示了普通的通知和自定義視圖通知

在消息通知時,我們經(jīng)常用到兩個組件Toast和Notification。特別是重要的和需要長時間顯示的信息,用Notification就最 合適不過了。當有消息通知時,狀態(tài)欄會顯示通知的圖標和文字,通過下拉狀態(tài)欄,就可以看到通知信息了,Android這一創(chuàng)新性的UI組件贏得了用戶的一 致好評,就連蘋果也開始模仿了。今天我們就結合實例,探討一下Notification具體的使用方法。  首先說明一下我們需要實現(xiàn)的功能是:在程序啟動時,發(fā)出一個通知,這個通知在軟件運行過程中一直存在,相當于qq的托盤一樣。

然后再演示一下普通的通知和自定義視圖通知, 那我們就先建立一個安卓項目。

然后編輯/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>

上面的布局很簡單,有兩個按鈕分別用于啟動普通的notification和自定義的notification。
接下來自定義一個布局用于顯示自定義的通知的。

<?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>

接下來就是上代碼。

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("普通的通知");
 //把通知放在正在運行欄目中
 notification.flags|=Notification.FLAG_ONGOING_EVENT;
 //設定默認聲音
 notification.defaults|=Notification.DEFAULT_SOUND;
 //設定默認震動
 notification.defaults|=Notification.DEFAULT_VIBRATE;
 //設定默認LED燈提醒
 notification.defaults|=Notification.DEFAULT_LIGHTS;
 //設置點擊后通知自動清除
 notification.defaults|=Notification.FLAG_AUTO_CANCEL;

 String textTitle="Notification示例";
 String textContent="程序正在運行,點擊此處跳轉到演示界面";

 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("個性化的通知");
 //自定義通知的聲音
 notification.sound=Uri.parse(Environment.getExternalStorageDirectory()+"/non.mp3");
 //自定義震動參數(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, "這是個性化的通知");
 //指定個性化的視圖
 notification.contentView=contentView;
 Intent it=new Intent(context, MainActivity.class);
 PendingIntent pendintent=PendingIntent.getActivity(context, 0, it, 0);
 //指定內容視圖
 notification.contentIntent=pendintent;
 mNotificationManager.notify(1, notification);
 }
 //自定義一個方法創(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);
 }
}

上面的代碼我們定義了一個方法createNotification(String text).該方法是用于創(chuàng)建一個通知。注意之所以這樣寫是因為。不管是普通的通知還是自定義的通知。前面的創(chuàng)建過程都是一樣的。然后我們實現(xiàn)了兩個按鈕的點擊事件。
運行結果如下所示:

 

當點擊兩個按鈕時會出現(xiàn)以下的情況:

 

注 :這里是有聲音效果的。如果手機支持閃光,還有LED效果。

以上就是關于Android中Notification通知提醒實現(xiàn)的過程詳解,最近更新了許多關于Android中Notification通知提醒的文章,希望對大家的學習有所幫助。

相關文章

  • Kotlin函數(shù)式編程超詳細介紹

    Kotlin函數(shù)式編程超詳細介紹

    一個函數(shù)式應用通常由三大類函數(shù)構成:變換transform、過濾filters合并combineo每類函數(shù)都針對集合數(shù)據(jù)類型設計,目標是產(chǎn)生一個最終結果。函數(shù)式編程用到的函數(shù)生來都是可組合的,也就是說,你可以組合多個簡單函數(shù)來構建復雜的計算行為
    2022-09-09
  • Android實現(xiàn)彈窗進度條效果

    Android實現(xiàn)彈窗進度條效果

    這篇文章主要為大家詳細介紹了Android實現(xiàn)彈窗進度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android requestFocus詳解及實例

    Android requestFocus詳解及實例

    這篇文章主要介紹了Android requestFocus詳解及實例的相關資料,需要的朋友可以參考下
    2017-07-07
  • Android Wear計時器開發(fā)

    Android Wear計時器開發(fā)

    這篇文章主要介紹了Android Wear計時器開發(fā),需要的朋友可以參考下
    2014-11-11
  • Android中SoundPool的使用步驟實例

    Android中SoundPool的使用步驟實例

    今天小編就為大家分享一篇關于Android中SoundPool的使用步驟實例,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • Android自定義HorizontalScrollView打造超強Gallery效果

    Android自定義HorizontalScrollView打造超強Gallery效果

    這篇文章主要介紹了Android自定義HorizontalScrollView打造圖片橫向滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Kotlin中協(xié)程的創(chuàng)建過程詳析

    Kotlin中協(xié)程的創(chuàng)建過程詳析

    使用協(xié)程的專業(yè)開發(fā)者中有超過 50% 的人反映使用協(xié)程提高了工作效率,下面這篇文章主要給大家介紹了關于Kotlin中協(xié)程創(chuàng)建過程的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-01-01
  • Android 捕獲錯誤日志的方法

    Android 捕獲錯誤日志的方法

    這篇文章主要介紹了Android 捕獲錯誤日志的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • Android打開WebView黑屏閃爍問題排查

    Android打開WebView黑屏閃爍問題排查

    這篇文章主要介紹了Android打開WebView黑屏閃爍問題排查,文章通過詳細的代碼示例和圖文介紹WebView黑屏閃爍的問題,感興趣的小伙伴可以跟著小編一起來學習
    2023-05-05
  • Android軟鍵盤彈出時的界面控制方法

    Android軟鍵盤彈出時的界面控制方法

    這篇文章主要介紹了Android軟鍵盤彈出時的界面控制方法,結合實例形式分析了Android軟鍵盤彈出后的三種模式,涉及Android針對AndroidManifet.xml的修改技巧,需要的朋友可以參考下
    2016-08-08

最新評論

商河县| 饶河县| 大洼县| 衡阳县| 拉孜县| 武功县| 英吉沙县| 社旗县| 衡南县| 怀宁县| 扬中市| 永吉县| 中超| 临西县| 西乡县| 紫金县| 微山县| 民县| 武胜县| 长泰县| 香河县| 鲁甸县| 婺源县| 克拉玛依市| 景德镇市| 白山市| 蓬溪县| 宝山区| 嵩明县| 建德市| 舟曲县| 城固县| 郎溪县| 勐海县| 潞城市| 云安县| 鹰潭市| 长阳| 卓尼县| 南和县| 上高县|