这篇文章主要介绍如何自定义状态栏notification布局,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
布局定义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/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"
/>
<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"
/>
</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.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);
}
}
以上是“如何自定义状态栏notification布局”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。