編輯:關於android開發
android中,當app需要向發送一些通知,讓使用者注意到你想要告知的信息時,可以用Notification.下面,就來討論一下,Notification的用法,我們從實際的小例子來進行學習。
1.新建一個項目,在layout布局裡寫兩個按鈕,一個用來開啟通知,一個用來關閉通知。下面直接上布局代碼。
<RelativeLayout 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:gravity="center"
>
<Button
android:id="@+id/bt_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="openNotify"
android:text="open" />
<Button
android:id="@+id/bt_down"
android:layout_below="@id/bt_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="closeNotify"
android:text="close" />
</RelativeLayout>
然後就是代碼的實現了,還是直接上代碼,很簡單,相信大家一看就明白。
package com.example.demo;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
/**
*
* @author jianww
* 2015-12-16
*
*/
public class MainActivity extends Activity {
/**
* 1.先建立通知管理器,得到通知服務
* 2.創建通知對象,發出一個通知。
* 3.
*/
//1.建立通知管理器,
private NotificationManager notifyManager;
//2.聲明通知對象變量。
private Notification notify;
//3.創建意圖,當點擊通知時,打開相應的意思對象,跳轉到對應的類。
private Intent intent;
/*
* Intent 是及時啟動,intent 隨所在的activity 消失而消失。
PendingIntent 可以看作是對intent的包裝,通常通過getActivity,getBroadcast ,
getService來得到pendingintent的實例,當前activity並不能馬上啟動它所包含的intent,
而是在外部執行 pendingintent時,調用intent的。正由於pendingintent中 保存有當前
App的Context,使它賦予外部App一種能力,使得外部App可以如同當前App一樣的執行pendingintent裡
的 Intent, 就算在執行時當前App已經不存在了,也能通過存在pendingintent裡的Context照樣執行Intent。
另外還可以處理intent執行後的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之間傳遞數據,
而Pendingintent,一般用在 Notification上,可以理解為延遲執行的intent,
PendingIntent是對Intent一個包裝。本例用pendingIntent可以從通知中打開要打開的app中的對象。
*/
private PendingIntent pendIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
//打開通知
public void openNotify(View v) {
//創建通知對象實例,傳入默認的通知對象圖片,通知標題,通知發出時間。
notify = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
//創建意圖。此意圖不會立刻執行,只有當pendingIntent執行時,才會執行傳入裡面的意圖。
intent = new Intent(getApplicationContext(),MainActivity.class);
//得到pendintent對象實例。設置此延時意圖的標記。
pendIntent = PendingIntent.getActivity(getApplicationContext(), 100, intent, 0);
//設置通知的標題與內容。
notify.setLatestEventInfo(getApplicationContext(), "通知", "通知的內容", pendIntent);
//設置通知的標記為默認。
notify.flags = Notification.FLAG_AUTO_CANCEL;
//開始通知。
notifyManager.notify(100, notify);
}
//關閉通知。
public void closeNotify(View v) {
//關閉通知。
pendIntent.cancel();
}
}
就是這麼簡單,只是用來復習一下基礎知識。^_^
Android開發學習之路-EventBus使用,android-eventbus
Android開發學習之路-EventBus使用,android-eventbusEventBus是一個通過發布、訂閱事件實現組件間消息傳遞的工具。 它存在的目的,就是為
(轉)根據ImageView的大小來壓縮Bitmap,避免OOM,imageviewoom
(轉)根據ImageView的大小來壓縮Bitmap,避免OOM,imageviewoom本文轉載於:http://www.cnblogs.com/tianzhijiex
Building Apps with Over 65K Methods(解決APP引用方法總數超過65536),appsapp
Building Apps with Over 65K Methods(解決APP引用方法總數超過65536),appsapp 本
android下面res目錄,androidres目錄
android下面res目錄,androidres目錄1. 相關文件夾介紹 在Android項目文件夾裡面