android studio中怎么设置点击按钮响应

android studio中怎么设置点击按钮响应,第1张

Notification的用法 --- 状态栏通知发送一个状态栏通知必须的两个类:1. NotificationManager --- 状态栏通知的管理类,负责发通知,清除通知等NotificationManager : 是一个系统Service,必须通过 context.getSystemService(NOTIFICATION_SERVICE)方法获取NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE)2. Notification --- 具体的状态栏通知对象,可以设置icon,文字,提示音,震动等参数下面是设置一个通知需要的基本参数Anicon(通知的图标)Atitleanexpandedmessage(通知的标题和内容)APendingIntent(点击通知执行页面跳转)1.创建Notification通过NotificationManager的notify(int Id , Notification)方法来启动Notification参数一:Notification的唯一标识参数二:Notification对象2.更新Notification调用Notification的setLatestEventInfo()方法来更新内容,然后调用NotificationManager的notify()方法即可3.删除Notification通过NotificationManager的cancle(int id) 方法,清除通知 参数: 要清除的Notification的唯一标识4.Notification设置 -- 震动,铃声等1.基本设置:?123456789101112131415161718//新建状态栏通知baseNF=new Notification()//设置通知在状态栏显示的图标baseNF.icon=R.drawable.icon//通知时在状态栏显示的内容baseNF.tickerText="YouclickedBaseNF!"//通知的默认参数DEFAULT_SOUND,DEFAULT_VIBRATE,DEFAULT_LIGHTS.//如果要全部采用默认值,用DEFAULT_ALL.//此处采用默认声音baseNF.defaults=Notification.DEFAULT_SOUND//第二个参数:下拉状态栏时显示的消息标题expandedmessagetitle//第三个参数:下拉状态栏时显示的消息内容expandedmessagetext//第四个参数:点击该通知时执行页面跳转baseNF.setLatestEventInfo(Lesson_10.this,"Title01","Content01",pd)//发出状态栏通知//ThefirstparameteristheuniqueIDfortheNotification//andthesecondistheNotificationobject.nm.notify(Notification_ID_BASE,baseNF)2.添加声音baseNF.default=Notification.DEFAULT_SOUND-- 使用系统默认提示音notification.sound=Uri.parse("file:///sdcard/notification/ringer.mp3")--- 自定义声音使用用系统自带的铃声,可以这样:notification.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI,"6")3.添加震动notification.defaults=Notification.DEFAULT_VIBRATE使用默认震动方式4.添加闪光notification.defaults=Notification.DEFAULT_LIGHTS5.其他有用的设置:?12345678910111213141516flags:Notification.FLAG_INSISTENT//让声音、振动无限循环,直到用户响应Notification.FLAG_AUTO_CANCEL//通知被点击后,自动消失Notification.FLAG_NO_CLEAR//点击'Clear'时,不清楚该通知(QQ的通知无法清除,就是用的这个//自定义下拉视图,比如下载软件时,显示的进度条。Notificationnotification=newNotification()notification.icon=R.drawable.iconnotification.tickerText="Custom!"RemoteViewscontentView=newRemoteViews(getPackageName(),R.layout.custom)contentView.setImageViewResource(R.id.image,R.drawable.icon)contentView.setTextViewText(R.id.text,"Hello,thismessageisinacustomexpandedview")notification.contentView=contentView//使用自定义下拉视图时,不需要再调用setLatestEventInfo()方法//但是必须定义contentIntentnotification.contentIntent=pdnm.notify(3,notification)应用实例一:?123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时,把notification从通知栏里去掉。或者,只要程序在运行就一直显示通知栏图标。 下面对Notification类中的一些常量,字段,方法简单介绍一下:常量:DEFAULT_ALL使用所有默认值,比如声音,震动,闪屏等等DEFAULT_LIGHTS 使用默认闪光提示DEFAULT_SOUNDS 使用默认提示声音DEFAULT_VIBRATE 使用默认手机震动【说明】:加入手机震动,一定要在manifest.xml中加入权限:<uses-permission android:name="android.permission.VIBRATE">以上的效果常量可以叠加,即通过notification.defaults =DEFAULT_SOUNDDEFAULT_VIBRATEnotification.defaults = DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有) //设置flag位FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉FLAG_ONGOING_EVENT 通知放置在正在运行FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应 常用字段:contentIntent 设置PendingIntent对象,点击时发送该Intentdefaults 添加默认效果flags 设置flag位,例如FLAG_NO_CLEAR等icon 设置图标sound 设置声音tickerText 显示在状态栏中的文字when 发送此通知的时间戳 NotificationManager常用方法介绍:public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)public void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为idpublic void notify(int id, Notification notification) 将通知加入状态栏,标记为id package com.ljq.activity import android.app.Activityimport android.app.Notificationimport android.app.NotificationManagerimport android.app.PendingIntentimport android.content.Intentimport android.graphics.Colorimport android.os.Bundle public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.main) clearNotification() } @Overrideprotected void onStop() { showNotification() super.onStop() } @Overrideprotected void onStart() { clearNotification() super.onStart() } /** * 在状态栏显示通知 */private void showNotification(){ // 创建一个NotificationManager的引用NotificationManager notificationManager = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE) // 定义Notification的各种属性Notification notification =new Notification(R.drawable.icon, "督导系统", System.currentTimeMillis()) //FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉 //FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉 //FLAG_ONGOING_EVENT 通知放置在正在运行 //FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应 notification.flags = Notification.FLAG_ONGOING_EVENT// 将此通知放到通知栏的"Ongoing"即"正在运行"组中notification.flags = Notification.FLAG_NO_CLEAR// 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用notification.flags = Notification.FLAG_SHOW_LIGHTS //DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等 //DEFAULT_LIGHTS 使用默认闪光提示 //DEFAULT_SOUNDS 使用默认提示声音 //DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE">权限 notification.defaults = Notification.DEFAULT_LIGHTS //叠加效果常量 //notification.defaults=Notification.DEFAULT_LIGHTSNotification.DEFAULT_SOUND notification.ledARGB = Color.BLUE notification.ledOnMS =5000//闪光时间,毫秒 // 设置通知的事件消息CharSequence contentTitle ="督导系统标题"// 通知栏标题CharSequence contentText ="督导系统内容"// 通知栏内容Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class)// 点击该通知后要跳转的ActivityPendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0) notification.setLatestEventInfo(this, contentTitle, contentText, contentItent) // 把Notification传递给NotificationManagernotificationManager.notify(0, notification) }?//删除通知 private void clearNotification(){ // 启动后删除之前我们定义的通知NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE) notificationManager.cancel(0)

当某些异常情况发生时,例如:蓝屏,突然断电......这时如果你正在开发,而且恰好用的是AndroidStudio,那么当你再次重新启动电脑,启动项目时,你会发现,你的工作状态(打开的类,打开的目录等)都被归置了。以后每次打开都会要重新的打开目录,打开相关的文件......其实引起这种情况的原因是工程目录下的.idea---->libraries---->workspace.xml损坏了

解决方案:打开AndroidStudio的project视图方式,找到workspace.xml然后干掉它,重启AndroidStudio,这次再打开几个文件进行测试,关闭AndroidStudio,打开后发现工作状态已经保存。仅供参考!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11955159.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存