Day03
提示信息代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("提示收到了一天消息");
builder.setContentTitle("天气预报");
builder.setContentText("正文:今天天气晴,东南方转西北风5~6级");
builder.setContentInfo("附加消息");
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
Notification build = builder.build();
manager.notify(200,build);
}
}
效果图
aa.gif大图提示信息代码
public void big(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("大图显示");
Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
builder.setStyle(bigPictureStyle);
manager.notify(4,builder.build());
}
效果图
bb.gif提示信息跳转
public void click(View view) {
//通知管理者,发送通知。取消通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建构造者
Notification.Builder builder = new Notification.Builder(this);
//设置属性 setSamllIcon该属性必须设置
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("提示收到了一天消息");
builder.setContentTitle("天气预报");
builder.setContentText("正文:今天天气晴,东南方转西北风5~6级");
builder.setContentInfo("附加消息");
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setAutoCancel(true);
//跳转到其他页面
//创建意图对象
Intent intent = new Intent(this, MainActivity.class);
//intent对象转成pendingintent
PendingIntent pendingIntent=PendingIntent.getActivity(this,100,intent,PendingIntent.FLAG_ONE_SHOT);
//设置跳转
builder.setContentIntent(pendingIntent);
//发送通知
Notification build = builder.build();
manager.notify(200,build);
}
效果图
cc.gif列表消息
public void list(View view) {
//获得通知的管理对象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//通知的穿件对象
Notification.Builder builder = new Notification.Builder(this);
//设置小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置标题
builder.setContentTitle("标题");
//设置内容
builder.setContentText("内容");
builder.setAutoCancel(true);
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
inboxStyle.addLine("1");
inboxStyle.addLine("2");
inboxStyle.addLine("3");
inboxStyle.addLine("4");
inboxStyle.addLine("5");
inboxStyle.addLine("6");
inboxStyle.addLine("7");
inboxStyle.addLine("8");
inboxStyle.addLine("9");
builder.setStyle(inboxStyle);
Intent intent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_ONE_SHOT);
builder.setFullScreenIntent(pendingIntent,true);
builder.setContentIntent(pendingIntent);
manager.notify(9,builder.build());
}
效果图
dd.gif自定义消息
<?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">
<TextView
android:id="@+id/text_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是自定义"
android:textSize="25dp"/>
<ImageView
android:id="@+id/image_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_toRightOf="@id/text_one"/>
</RelativeLayout>
public void zidingyi(View view) {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("标题");
builder.setContentText("自定义");
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_zidingyi);
remoteViews.setTextViewText(R.id.text_one,"这是自定义");
remoteViews.setImageViewResource(R.mipmap.ic_launcher,R.mipmap.ic_launcher);
builder.setContent(remoteViews);
manager.notify(4,builder.build());
}
网友评论