佈局定義custom_notification.xmlandroid
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/p_w_picpath" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_marginRight="10dp" android:contentDescription="@string/Image" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/p_w_picpath" style="@style/NotificationTitle" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/p_w_picpath" android:layout_below="@id/title" style="@style/NotificationText" /> </RelativeLayout>
布居中引用的樣式文件styles.xmlapp
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" /> <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" /> </resources>
代碼ide
package cn.itcast.tabhost; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.Color; import android.widget.RemoteViews; public class FirstActivity extends Activity { //默認點擊返回鍵(back)會finish當前activity //activity棧中的全部activity都彈出後會退出當前應用 @Override public void onBackPressed() { /* * 按照通常的邏輯,當Activity棧中有且只有一個Activity時,當按下Back鍵此 * 那麼下次點擊此應用程序圖標將從從新啓動,當前很多應用程序都是採起如Home鍵的效果, * 當點擊了Back鍵,系統返回到桌面,而後點擊應用程序圖標 * 直接回到以前的Activity界面,這種效果是怎麼實現的呢?經過重寫按下Back鍵的回調函數,轉成Home鍵的效果便可。 */ // 改成使用intent啓動HOME桌面 Intent home = new Intent(Intent.ACTION_MAIN); home.addCategory(Intent.CATEGORY_HOME); startActivity(home); // 或者,爲達到此類效果,Activity實際上提供了直接的方法。 // 將當前Activity所在的Task移到後臺,同時保留activity順序和狀態。 moveTaskToBack(true);// true表示無論是否是根都有效 } /** * 當此Activity處於後臺工做時, 在狀態欄顯示通知 */ @Override protected void onStop() { showNotification(); super.onStop(); } //當程序再次進入運行界面時,Activity處於onResume狀態,在onResume方法中去掉狀態欄的程序運行信息便可 /** * 此Activity啓動後關閉狀態欄的通知 */ @Override protected void onResume() { // 啓動後刪除以前咱們定義的通知 NotificationManager notificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); notificationManager.cancel(CUSTOM_VIEW_ID); super.onResume(); } private static final int CUSTOM_VIEW_ID = 1; //在狀態欄顯示程序通知 private void showNotification() { // 建立一個NotificationManager的引用 NotificationManager notificationManager = (NotificationManager) this .getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定義Notification的各類屬性 Notification notification = new Notification(R.drawable.bg_normal, "superGao", System.currentTimeMillis()); RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); contentView.setImageViewResource(R.id.p_w_picpath, R.drawable.i1); contentView.setTextViewText(R.id.title, "自定義佈局通知標題"); contentView.setTextViewText(R.id.text, "自定義佈局通知內容"); //給view設置點擊事件 /* contentView.setOnClickPendingIntent(viewId, pendingIntent); */ notification.contentView = contentView; notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運行"組中 notification.flags |= Notification.FLAG_NO_CLEAR; // 代表在點擊了通知欄中的"清除通知"後,此通知不清除,常常與FLAG_ONGOING_EVENT一塊兒使用 notification.flags |= Notification.FLAG_SHOW_LIGHTS;//使用LED燈 notification.defaults = Notification.DEFAULT_LIGHTS; notification.ledARGB = Color.BLUE;//LED燈顏色 notification.ledOnMS = 5000;//led燈持續時間 // 設置通知的事件消息 /* * CharSequence contentTitle = "superGao"; // 通知欄標題 CharSequence contentText = "love"; // 通知欄內容 */ Intent notificationIntent = new Intent(this, FirstActivity.class); // 點擊該通知後要跳轉的Activity PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.contentIntent=contentItent; /* notification.setLatestEventInfo(this, contentTitle, contentText, contentItent);*/ // 把Notification傳遞給NotificationManager notificationManager.notify(CUSTOM_VIEW_ID , notification); } }