刚刚学习了Android中的信息提示方法,之前的AlertDialog和Toast、Notification都是Android中存在的信息提示方法,本文总结一下Toast和Notification的基本使用方法。
1.Toast
首先来看Toast,这个是我最早接触的信息提示方法,也是很常见的一种信息提示方法,例如手机连接usb设备,会弹出一个小的提示“设备已连接!”,这在利用usb通讯或者蓝牙通讯的开发中很是常见,前段时间在研究这两中通讯的时候,就用到了该方法。
Toast相对于AlertDialog和Notification而言,是最简单的一种提示方式,如果想实现最简单的文本提示,只需要一句代码就可以实现。
Toast.makeText(this, "今天天气真好!", Toast.LENGTH_SHORT).show();
这样一句代码就可以实现文本的提示。但是有些时候,我们也需要在Toast上加上一些自定义的东西,比如一个图像,一个表情,这样我们就需要自定义一个Toast,查阅Android API发现:
有一个setView方法,该方法可以传入一个View类型的参数,那么这些问题就不难解决了,我们可以重写一个Layout布局,将我们需要实现的功能写进去,然后实现需要的功能,例如下面的代码就是利用toast.xml来实现自定义布局的。
View view = getLayoutInflater().inflate(R.layout.toast,null);
TextView textView = (TextView) findViewById(R.id.textview);
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
2.Notification
相比于前两者,Notification这种信息提示就显得更为亲切了,也是Android中最为理想的提示方式,就像你在看着优酷视频,一个短信过来,它不会中断你的视频播放,而是在状态栏显示一个短信的信息提示,这就是一种Notification,这种的信息提示的方式不仅可以显示文本,而且还可以显示图片。
首先来看看该如何创建一个Notification,通过学习得知,Android中的Notification经历过一次比较大的变化,即在API11之前,Notification可以直接创建然后使用相关的set方法来设置基本属性,而在之后的API中,这种方法已经行不通了,在编译时就会不通过,目前,创建一个Notification,需要先利用Builder方法创建一个notificationnBuilder,然后使用Builder的方法来设置,最后通过build()方法来创建Notificatio,然年再通过NotificationManager来响应,代码如下:
//创建Notification.Builder对象
Notification.Builder notification = new Notification.Builder(MainActivity.this);
//创建一个PendingIntent对象,原因是Notification可以与应用程序脱离,即应用程序关闭,Notification仍然会显示在状态栏中
Intent intent = new Intent(this,MainActivity.class);
intent.putExtra("msg","在干嘛呢?");
//当应用程序重新启动,又可以控制这些Notification
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
notification.setDeleteIntent(pendingIntent);
//使用Notification类的setLatestEventInfo方法显示Notification消息
notification.setSmallIcon(resId);//显示图标
notification.setTicker(tickerText);
notification.setWhen(System.currentTimeMillis());//显示时间
notification.setContentText(contentText);//显示标题
notification.setContentTitle(contentTitle);//显示文本内容
notification.setContentIntent(pendingIntent);//点击Notification事件
notification.setDefaults(Notification.DEFAULT_SOUND);//提示方式(响铃、震动等)
Notification notification1 = notification.build();
//设置永久存在
notification1.flags = Notification.FLAG_NO_CLEAR;
notificationManager.notify(id,notification1);
对于Notification,我们也可以自定义设置,通过加载一个notification.xml文件来实现,代码如下:
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.kuchipatchi);
builder.setTicker("自定义Notification");
builder.setWhen(System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.notification);
remoteViews.setTextViewText(R.id.textview,"更新自定义内容");
builder.setContent(remoteViews);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
notificationManager.notify(R.drawable.kuchipatchi,notification);
注:在使用Notification时,关于设置Icon,我在小米2s上设置不起作用,在谷歌nexus上只能显示轮廓,查过度娘后,发现在华为上也会有同样的问题,可能跟图像有关,也有可能和手机系统有关。