国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android Notification通知解析

2020-04-11 10:54:50
字體:
來源:轉載
供稿:網友

Notification是顯示在手機狀態欄的通知,Notification通知是具有全局性的通知,一般通過NotificationManager來進行管理.
一般運用Notification的步驟如下:

  • 1.調用getSysytemService(NOTIFICATION_SERVICE)來獲取系統的NotificationManager,進行Notification的發送和回收
  • 2.通過構造器建立一個Notification
  • 3.為Notification set各種屬性,然后builder()建立
  • 4.通過NotificationManager發送通知

下面通過一個實例來演示上面的用法,先看一張效果圖

一.獲取系統的NotificationManager

private NotificationManager nm;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //獲取系統的通知管理    nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  }

二.為主布局的兩個按鈕添加監聽事件,然后分別設置啟動通知,并設置各種屬性和取消通知
各種屬性代碼中介紹的很詳細,具體可以參考API

啟動通知

public void send(View view){    //用于打開通知啟動另一個Activity    Intent intent = new Intent(MainActivity.this,OtherActivity.class);    //用于延遲啟動    PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);    //設置通知    Notification notify = new Notification.Builder(this)        //設置打開該通知,通知自動消失        .setAutoCancel(true)        //設置顯示在狀態欄的通知提示消息        .setTicker("新消息")        //設置通知欄圖標        .setSmallIcon(R.mipmap.ic_launcher)        //設置通知內容的標題        .setContentTitle("一條新通知")        //設置通知內容        .setContentText("恭喜你通知欄測試成功")        //設置使用系統默認的聲音,默認的led燈        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS)        //ALL的話則是全部使用默認,聲音,震動,閃光燈,需要添加相應權限//        .setDefaults(ALL)        //或者自定義聲音        //setSound(Uri.parse())        //設置要啟動的程序        .setContentIntent(pi)        //最后用build來建立通知        .build();    //發送當前通知,通過NotificationManager來管理    nm.notify(1,notify);  }

這里用的OtherActivity是通過通知啟動的另一個Activity,為了啟動需要在清單文件中加入此Activity,并且因為用到了閃光燈和振動器,所以也需要添加相應的權限

<activity android:name=".OtherActivity"> </activity>  <uses-permission android:name="android.permission.FLASHLIGHT"/>  <uses-permission android:name="android.permission.VIBRATE"/>

取消通知

//取消通知  public void closed(View view){    nm.cancel(1);  }

用起來相當很方便.最后附上主界面布局

<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:paddingLeft="@dimen/activity_horizontal_margin"  android:orientation="horizontal"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  <Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="開啟通知"    android:onClick="send"    android:id="@+id/btnstartnotification"     />  <Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="關閉通知"    android:onClick="closed"    android:id="@+id/btnstopnotification"     /></LinearLayout>

以上就是關于Android Notification通知的詳細內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 高邑县| 芦山县| 灌阳县| 天祝| 台前县| 张家川| 五峰| 全南县| 嘉兴市| 公安县| 荥经县| 万载县| 麻城市| 鄂托克旗| 南皮县| 哈密市| 岱山县| 郎溪县| 乌什县| 阿拉善右旗| 揭阳市| 文登市| 凉山| 湖州市| 夏邑县| 托克逊县| 甘南县| 平邑县| 安达市| 扬中市| 安远县| 邵阳市| 龙泉市| 太保市| 山西省| 盈江县| 德惠市| 武穴市| 萍乡市| 瑞安市| 郎溪县|