您的位置:首页 > 其它

自定义状态栏notification布局

2016-05-25 15:54 417 查看
布局定义custom_notification.xml
<?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/image"
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/image"
style="@style/NotificationTitle" />

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_below="@id/title"
style="@style/NotificationText" />

</RelativeLayout>
布居中引用的样式文件styles.xml
<?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>
代码
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.image, 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);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息