亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android 通知Notification詳解及實例代碼

 更新時間:2016年12月25日 15:58:20   投稿:lqh  
這篇文章主要介紹了android 通知Notification詳解及實例代碼的相關資料,需要的朋友可以參考下

android Notification實例詳解

1.使用Builder模式來創(chuàng)建

2.必須要設置一個smallIcon,還可以設置setTicker

3.可以設置 setContentTitle,setContentInfo,setContentText,setWhen

4.可以設置setDefaults(閃屏,聲音,震動),通過Notification設置flags(能不能被清除)

5.發(fā)送需要獲取一個NotificationManager(getSystemService來獲?。?notify(int id,Notification)

6.清除 manager.cancelAll(清除所有) cancal(int id); 如果需要16一下的api也能訪問,需要導入v4包,使用notificationCompat兼容類。

自定義通知

使用RemoteViews來建立一個布局,然后使用setContent()設置;

點擊事件使用PendingIntent來完成

下面是一個案例

MainActivity類

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

  }

  public void clearNotification(View v) {
    // 通過代碼來清除 NO_CLEAR
    // 清除也需要manager
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // 只能清除由本應用程序發(fā)出去的通知
    manager.cancelAll();
  }

  public void sendNotification(View v) {
    // 他是 在v4包中的通知,用于16以下版本發(fā)送通知
    // NotificationCompat
    // 通知的創(chuàng)建
    Notification.Builder builder = new Notification.Builder(this);
    // NotificationCompat.Builder builder2 = new NotificationCompat.Builder(
    // this);
    // 顯示在通知條上的圖標 必須的
    builder.setSmallIcon(R.drawable.ic_launcher);
    // 顯示通知條上的文字 不是必須的
    builder.setTicker("您有一條新的消息!!");

    // 狀態(tài)欄中的
    builder.setContentTitle("大標題");
    builder.setContentText("文本");
    builder.setWhen(System.currentTimeMillis());
    builder.setContentInfo("Info");
    builder.setDefaults(Notification.DEFAULT_ALL);
    // 點擊事件使用Pending
    Intent intent = new Intent(this, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this, 1, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pi);
    // 生成對象 16API以上,支持低版本需要使用v4包中的notificationCompat
    Notification notify = builder.build();
    // 設置不能清除
    notify.flags = Notification.FLAG_NO_CLEAR;

    // 如何將通知發(fā)送出去
    NotificationManager mananger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // 通知的唯一值,如果id重復,表明是更新一條通知,而不是新建
    mananger.notify((int) (Math.random() * 1000), notify);
  }

  public void diyNotification(View v) {
    // 展示在通知上面的視圖
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.layout);
    Notification notification = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher).setTicker("自定義通知 ")
        // 布局
        .setContent(views).build();
    // 使用RemoteViews來設置點擊事件
    views.setTextColor(R.id.tv, Color.RED);
    Intent intent = new Intent(this, OneActivity.class);
    // 音樂播放是放在Service
    PendingIntent pi = PendingIntent.getActivity(this, 2, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.tv, pi);

    Intent intent2 = new Intent(this, MainActivity.class);

    PendingIntent pi2 = PendingIntent.getActivity(this, 1, intent2,
        PendingIntent.FLAG_UPDATE_CURRENT);
    views.setOnClickPendingIntent(R.id.iv, pi2);
    // 發(fā)送
    NotificationManager notify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notify.notify(1, notification);

  }

}

OneActivity類

public class OneActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView tv = new TextView(this);
    tv.setText("跳轉界面");
    setContentView(tv);
  }
}

activity.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:orientation="vertical"
  tools:context="com.example.lesson8_notification.MainActivity" >

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="sendNotification"
    android:text="普通的通知" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="clearNotification"
    android:text="清除所有通知" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="diyNotification"
    android:text="自定義通知" />

</LinearLayout>

layout.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:orientation="vertical" >

  <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

  <ImageView
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

最后記得注冊activity

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • Android Imageloader的配置的實現代碼

    Android Imageloader的配置的實現代碼

    這篇文章主要介紹了Android Imageloader的配置的實現代碼的相關資料,需要的朋友可以參考下
    2017-07-07
  • Android端代碼量非常小的分頁加載庫

    Android端代碼量非常小的分頁加載庫

    這篇文章主要給大家介紹了關于Android端代碼量非常小的分頁加載庫的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • ListView點擊Item展開菜單實現代碼詳解

    ListView點擊Item展開菜單實現代碼詳解

    這篇文章主要介紹了ListView點擊Item展開菜單實現代碼詳解的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • Android 圖片的三級緩存機制實例分析

    Android 圖片的三級緩存機制實例分析

    這篇文章主要介紹了Android 圖片的三級緩存機制實例分析的相關資料,需要的朋友可以參考下
    2017-05-05
  • Android中用Bmob實現短信驗證碼功能的方法詳解

    Android中用Bmob實現短信驗證碼功能的方法詳解

    本文給大家分享通過第三方平臺Bmob實現發(fā)送驗證碼和校驗驗證碼的功能,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-09-09
  • android studio 4.0 新建類沒有修飾符的方法

    android studio 4.0 新建類沒有修飾符的方法

    這篇文章主要介紹了android studio 4.0 新建類沒有修飾符的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • Xposed插件開發(fā)入門詳解

    Xposed插件開發(fā)入門詳解

    這篇文章主要介紹了Xposed插件開發(fā)入門詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Android編程實現ListView頭部ViewPager廣告輪詢圖效果

    Android編程實現ListView頭部ViewPager廣告輪詢圖效果

    這篇文章主要介紹了Android編程實現ListView頭部ViewPager廣告輪詢圖效果,較為詳細的分析了自定義ListView實現ViewPager廣告圖輪詢的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android如何遍歷特定目錄下所有文件

    Android如何遍歷特定目錄下所有文件

    這篇文章主要為大家詳細介紹了Android如何遍歷特定目錄下所有文件的代碼,供大家參考,感興趣的小伙伴們可以參考一下
    2016-07-07
  • Android開發(fā)之imageView圖片按比例縮放的實現方法

    Android開發(fā)之imageView圖片按比例縮放的實現方法

    這篇文章主要介紹了Android開發(fā)之imageView圖片按比例縮放的實現方法,較為詳細的分析了Android中ImageView控件的scaleType屬性控制圖片縮放的具體用法,需要的朋友可以參考下
    2016-01-01

最新評論