19_toast通知和notify通知 onTouch事件响应
1. 响应ontouch事件 启动 toast 和 notify提醒
两个成员变量
private TextView curTextView;
static final int NOTIFICATION_ID = 0x1123;
oncreate中添加
curTextView = (TextView)this.findViewById(R.id.Tv_Hello);
OnTouchListener tempListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(MainActivity.this, "aaa", 5000).show();
//创建一个启动其他Activity的Intent
Intent intent = new Intent(MainActivity.this
, OtherActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this
, 0, intent , 0);
//创建一个Notification
Notification notify = new Notification();
//为Notification设置图标,该图标显示在状态栏
notify.icon = R.drawable.ic_launcher;
//为Notification设置文本内容,该文本会显示在状态栏
notify.tickerText = "启动其他Activity的通知";
//为Notification设置发送时间
notify.when = System.currentTimeMillis();
//为Notification设置声音
notify.defaults = Notification.DEFAULT_SOUND;
//为Notification设置默认声音、默认振动、默认闪光灯
notify.defaults = Notification.DEFAULT_ALL;
//设置事件信息
notify.setLatestEventInfo(MainActivity.this, "普通通知",
"点击查看", pi);
//获取系统的NotificationManager服务
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//发送通知
notificationManager.notify(NOTIFICATION_ID, notify);
return false;
}
};
curTextView.setOnTouchListener(tempListener);
2. 在 OtherActivity 中关闭提醒
//获取系统的NotificationManager服务
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
//取消通知
notificationManager.cancel(MainActivity.NOTIFICATION_ID);
3.添加权限
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 添加操作振动器的权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>