Android之提醒用户

Android中有三种提醒用户的方式

Toast 提醒 从后台显示的简短消息
StatusBar提醒 从后台传来,需要用户做出响应的持续的提醒
Dialog提醒 和activity相关的提醒

Toast是一个从窗口表面弹出来的消息。只占需要显示的消息的面积,用户当前的Activity依旧是显示的,可以用的
该Notification自动的淡入淡出,并且不和用户进行互动。该提醒可以从后台Service创建出来,即使应用程序不可见,也会显示
toast用户比较短的提心消息,例如“文件已保存”。Toast不可以和用户进行交互,如果希望用户做出反应并且采取动作,可以考虑用StatusBar进行代替

下面示例一个自定义Toast布局toast_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              android:background="#DAAA"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

在程序中需要用ID对Toast的布局进行填充

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello! This is a custom toast!");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

注意: 不要使用公共的构造方法来创建Toast,除非需要调用函数setView(View)来设置自定义的View,否则的话,必须使用makeText(Context,int,int)来创建一个Toast

Notification

Activity或者是Service可以初始化一个状态栏的notification,由于一个Activity如果想要在状态栏创建Notification的话,必须处于活动状态,因此只能从Service创建一个notification。必须使用Notification and NotificationManager这两个类来创建一个Notification
Notification类可以用来定义状态栏提醒的属性,例如状态栏的图标,扩展的消息,和额外其他的设置例如播放声音。
NotificationManager是Android系统的一个服务,用来执行和管理所有的Notification,不用实例化,使用的时候只需要 getSystemService()获得系统中的一个引用机就可以了,当通知用户的时候,将Notification对象传递到notify就可以了

1、首先获得一个NotificationManager的引用
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
2、初始化Notification
int icon = R.drawable.notification_icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();


Notification notification = new Notification(icon, tickerText, when);
3、定义Notification的一些扩展信息和Intent


Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);


notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
4、将Notification传递给NotificationManager
private static final int HELLO_ID = 1;


注意:如果是要点击之后就清除掉该提醒的话,需要设置notification.flags = Notification.FLAG_AUTO_CANCEL;
也可以用cancel(int)手动清除,或者用cancelAll()将所有的都清楚掉。
还可以设置声音,震动,闪光灯
添加声音:

notification.defaults |= Notification.DEFAULT_SOUND;
利用SD卡中已知的一个音乐文件
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");

notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");


添加震动:

notification.defaults |= Notification.DEFAULT_VIBRATE;

long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;
闪光灯

notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

Notification扩展的自定义布局

1、创建自定义布局文件

	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="3dp"
              >
	    <ImageView android:id="@+id/image"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:layout_marginRight="10dp"
              />
	    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#000"
              />
	</LinearLayout>

2、利用RemoveViews 来定义image和text,然后将RemoveViews对象给Notification的contentView
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.notification_image);
contentView.setTextViewText(R.id.text, "Hello, this message is in a custom expanded view");
notification.contentView = contentView;
3、设置一个自定义VIEW的时候,不再需要setLatestEventInfo() 方法,但是必须定义Intent
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
4、同上面一样  

mNotificationManager.notify(CUSTOM_VIEW_ID, notification);


FLAG_AUTO_CANCEL 当选择了该Notification之后,自动取消该提醒
FLAG_INSISTENT 重复audio,直到用户作出响应
FLAG_ONGOING_EVENT 可以设置例如电话或者是音乐正在运行过程中,用来表明该程序是在运行的
FLAG_NO_CLEAR

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值