Android-系统自带通知栏and自定义通知栏
2017-02-15 11:04
169 查看
系统自带通知栏:
在布局文件中写一个button,写一个sendNotification方法
自定义通知栏:
布局文件:
Activity:
在布局文件中写一个button,写一个sendNotification方法
public void sendNotification(View view){ //實例化通知管理器 NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //實例化通知 NotificationCompat.Builder builder=new NotificationCompat.Builder(this); builder.setContentTitle("系統消息"); builder.setContentText("來自系統的消息"); builder.setDefaults(NotificationCompat.DEFAULT_ALL); builder.setSmallIcon(android.R.drawable.ic_media_play); builder.setContentIntent(PendingIntent.getActivity(this,0x102,new Intent(this,RingActivity.class),0)); Notification notification=builder.build(); //發出通知. notificationManager.notify(0x101,notification); }
自定义通知栏:
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:id="@+id/textView1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:clickable="true" /> <TextView android:id="@+id/textView2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout>
Activity:
package com.example.android_alarmandnotification; import android.os.Bundle; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.graphics.Color; import android.widget.RemoteViews;; import android.content.Context; import android.content.Intent; public class NotifictionActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Notification notification = new Notification(); //设置图标,后面的自定义布局的图片会覆盖它,但还是得设置,不然不会显示到通知栏 notification.icon = android.R.drawable.ic_media_play; notification.tickerText = "来信息啦"; notification.when = System.currentTimeMillis(); notification.flags = Notification.FLAG_AUTO_CANCEL; //传入当前项目的包名,和你通知栏上要显示的自定义布局的ID RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.activity_notifiction); //下面都是设置通知栏布局里面控件的属性 remoteViews.setImageViewResource(R.id.imageView1, android.R.drawable.ic_media_play); remoteViews.setTextColor(R.id.textView1, Color.RED); remoteViews.setTextViewText(R.id.textView1, "今日头条"); // PendingIntent有4种flag. // - FLAG_ONE_SHOT 只执行一次 // - FLAG_NO_CREATE 若描述的Intent不存在则返回NULL值 // - FLAG_CANCEL_CURRENT 如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的 // - FLAG_UPDATE_CURRENT 总是执行,这个flag用的最多 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.textView1, pendingIntent); notification.contentView = remoteViews; notification.contentIntent = pendingIntent; NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(1, notification); } }
相关文章推荐
- android 中系统自带的主题与样式(theme and style)
- Android应用开发——系统自带样式And…
- android 中系统自带的主题与样式(theme and style)
- Android的Notification的自定义布局按钮点击+点击按钮收回系统通知栏
- Android进阶——自定义View之继承系统控件实现自带删除按钮动画效果和软键盘自动悬浮于文本框下方
- android系统自带的主题与样式(theme and style)
- android 系统自带主题样式及自定义主题样式
- android通知使用系统默认声音或者自定义声音
- [转]android 中系统自带的主题与样式(theme and style)
- 【Android】系统自带的主题与样式(theme and style)
- 【转】android 中系统自带的主题与样式(theme and style)
- android系统自带的主题与样式(theme and style)
- Android xml 自定义View需要写全路径,而系统自带的却不用?原因就在此
- Android 自定义系统通知栏的颜色
- android 一些系统自带的通知,服务,活动和数据
- AndrowListView实现(自定义游戏列表)防止屏幕闪烁,设置分割线android中不推荐的方法,要考虑向下兼容,用了推荐的新方法,可能不兼容旧版本系统的手机
- android系统自带的主题与样式(theme and style)
- android系统自带的主题与样式(theme and style)
- Android闹钟AlarmManager-通知NotificationManager-自定义通知栏
- android 中系统自带的主题与样式(theme and style)