1. 程式人生 > >Android-系統自帶通知欄and自定義通知欄

Android-系統自帶通知欄and自定義通知欄

系統自帶通知欄:

在佈局檔案中寫一個button,寫一個sendNotification方法

public void sendNotification(View view){
      //例項化通知管理器
        NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //例項化通知
        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
        builder.setContentTitle("系統訊息");
        builder.setContentText("來自系統的訊息");
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        builder.setSmallIcon(android.R.drawable.ic_media_play);
        builder.setContentIntent(PendingIntent.getActivity(this,0x102,new Intent(this,RingActivity.class),0));

        Notification notification=builder.build();
        //發出通知.
        notificationManager.notify(0x101,notification);
    }

自定義通知欄:

佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:clickable="true"
        />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        />
</LinearLayout>

Activity:
package com.example.android_alarmandnotification;

import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.graphics.Color;
import android.widget.RemoteViews;;
import android.content.Context;
import android.content.Intent;
public class NotifictionActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Notification notification = new Notification();
      //設定圖示,後面的自定義佈局的圖片會覆蓋它,但還是得設定,不然不會顯示到通知欄
        notification.icon = android.R.drawable.ic_media_play;
        notification.tickerText = "來資訊啦";
        notification.when = System.currentTimeMillis();
        notification.flags = Notification.FLAG_AUTO_CANCEL;
     //傳入當前專案的包名,和你通知欄上要顯示的自定義佈局的ID
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_notifiction);
      //下面都是設定通知欄佈局裡面控制元件的屬性
        remoteViews.setImageViewResource(R.id.imageView1, android.R.drawable.ic_media_play);
        remoteViews.setTextColor(R.id.textView1, Color.RED);
        remoteViews.setTextViewText(R.id.textView1, "今日頭條");
        //	PendingIntent有4種flag.
     //	- FLAG_ONE_SHOT 只執行一次
     //	- FLAG_NO_CREATE 若描述的Intent不存在則返回NULL值
    //	- FLAG_CANCEL_CURRENT 如果描述的PendingIntent已經存在,則在產生新的Intent之前會先取消掉當前的
    //	- FLAG_UPDATE_CURRENT 總是執行,這個flag用的最多
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class),
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.textView1, pendingIntent);
        notification.contentView = remoteViews;
        notification.contentIntent = pendingIntent;
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1, notification);
    }
}


相關推薦

Android-系統通知欄and定義通知欄

系統自帶通知欄:在佈局檔案中寫一個button,寫一個sendNotification方法public void sendNotification(View view){ //例項化通知管理器 NotificationManager notif

vue Element-ui 表格篩選框定義高度

image n-k pre chrome code lac 分享 sso spa el-table中可以在一行的某列進行篩選,代碼如下: <el-table-column prop="classOfTest" class="test" label="測試類名" :

python匯入模組和定義模組

模組就是已經開發好的程式碼功能,自己直接匯入進來,直接使用。匯入分為python 或selenium 自帶的模組和自己寫的自定義模組,下面根據這兩個方向寫一下分別匯入的方法。 1.匯入自帶模組import sys #匯入sys模組,呼叫此模組時的方法 print sys.argvfrom sys impor

spring 容器的事件以及 定義事件

ApplicationEvent spring 的事件是為bean與bean 之間的訊息通訊提供了支援,當一個bean 處理完一個任務後,希望另外一個bean 知道並能夠做出相應的處理,這時需要另外一個bean監聽當前bean 所傳送的事件。 ApplicationEvent以及Li

Python:python的以字母開頭的函式或方法集合

一、python以字母開頭的函式或方法 A assert斷言用法:防禦性的程式設計、執行時對程式邏輯的檢測、合約性檢查(比如前置條件,後置條件)、程式中的常量、檢查文件 assert 12==12 #assert語句判斷兩個整數是否相等,如果相等就

android 系統主題樣式及定義主題樣式

From: http://blog.csdn.net/dawanganban/article/details/17732701 http://www.cnblogs.com/bluestorm/archive/2012/07/12/2588724.html

調用aNDROID系統功能

ont hao123 provide music content vid uri android系統 系統 CONTENTpROVIDER%E5%9F%BA%E7%A1%80%E4%B9%8BURI http://music.hao123.com/songlist/495

Android分享---調用系統的分享功能

contex source dem jpg 用戶 資源 相同 一個 style 以前我們總想到友盟等平臺分享功能的集成,集成這玩意還得下載對應的jar包。當然,用這些平臺的分享並不是說什麽好處都沒有,至少人家的統計功能還是很實用的。不過有的時候我們是不需要多余功能的,只需要

Android系統下用js定義gesture事件(仿ios實現移動端事件一致)

initial path acc mtab uil 查看 sans fault default 一、手勢事件 下面二維碼是一個實例dome,可掃碼直接查看: 在ios系統中,系統自帶了gesture事件,兩個手指操作的時候,就會產生一下三種手勢

Android系統的MediaRecorder結合Camera實現視訊錄製及播放功能。

    近期,公司專案新增了需求,需要視訊錄製,然後儲存到本地,再播放...。 看了很多其他的框架,說不出好壞,應該說各有千秋吧。但我覺得還是原生的靠譜,就是谷歌系統自帶的MediaRecorder。 不多說上程式碼吧(已經測試,沒問題)。 程式碼沒什麼複雜的,都是些

Android 系統圖片裁剪功能(適配7.0、8.0、對了還有小米手機)

前段時間寫了如何獲取相簿和拍照之後的照片並且進行顯示和上傳,這一次是如何進行圓形影象製作,經常看我寫的筆記的人會知道,我很懶。那麼我就懶的自定義了,目前需求就用原生的就好了,大神的輪子,我會在後面進行推薦。這篇筆記是依賴於:Android呼叫相簿、相機(相容6.0、7.0、8.0) 文

Android系統的camera預設使用後置攝像頭

 a/packages/apps/Camera/src/com/android/camera/CameraSettings.java +++ b/packages/apps/Camera/src/com/android/camera/CameraSettings.java @@ -420,

Android程式隱藏系統的標題欄

在編寫程式的時候,系統預設的標題欄是你的專案名稱。 比如專案名稱是:Talk 會出現Talk的標題欄。 在xml檔案預覽的時候可以去掉。方法如下: 1點選AppTheme 2 如圖選擇NoActionBar。 點選確定,效果如下圖。

Android系統中實現AIDL 定義物件傳遞

  今天要在《在Android系統中實現AIDL介面回撥》這篇文章的基礎上實現AIDL自定義物件的傳遞功能。還是上一篇說到的三個專案: ├── SimpleJar ├── SimpleJarClient └── SimpleJarService 一、在SimpleJar專

Android定義通知欄Notification字型適配問題

前言 專案之前自定義的通知欄是一直有一個問題,就是不同的手機廠商生產的手機狀態列的背景顏色是不同的,所以自定義佈局的字型顏色就會出現衝突,看了幾種適配方案都不是特別完美,把官方文件看了個遍,最後在前輩的總結下新增自己的方案終於得到了一個比較完美的解決方案。文章

Android如何實現對系統對話方塊標題欄進行修改

先上一張圖可以看到,對話方塊的標題顏色是藍色的,下面還有一根線也是藍色的,在某些情況下,我們想改變程式的主題風格,顏色必須做修改,但又懶得去定製Dialog,下面的方法就誕生了: public static final void dialogTitleLineColor(

解除安裝Android系統的應用

一、系統應用的分類 1.系統開源應用 系統開源應用指我們可以看到原始碼的系統應用。 (1)在原始碼中的位置 2.系統未開源應用 系統未開源應用指系統中沒有該應用原始碼; (1)在原始

Android開發——去掉系統標題欄的幾種方式

    今天在練習自定義標題欄(Android初級開發(四)——補充3)的過程中遇到了隱藏系統自帶標題欄的問題,現將幾種去掉系統自帶標題欄的方式做一總結。大體上可以分為兩種方式,一種是修改xml檔案(這種方式產生的效果作用於所有Activity),一種是編碼實現(這種方式產

android藍芽4.0BLE及2.0 2.1 apk 串列埠助手16個定義按鍵和定義指令 字元接收 十六進位制或字元傳送

android藍芽4.0BLE apk 帶16個自定義按鍵和自定義指令 字元接收 https://pan.baidu.com/s/1eRSfprO android藍芽2.0 2.1 apk 帶16個自定義按鍵和自定義指令 字元接收  帶自動連線 https://pan.b

Android系統Spinner+Adapter基礎

屬性:android:spinnerMode="" 下拉選單的展示方式 屬性值:dropdown-下拉方式(預設的) dialog-對話方塊方式 如果下拉方式是dialog,那麼可以指定標題android:prompt="@string/title"(只能引用,不能直接寫文字) androi