Android消息通知

Android支持Toast和NotificationManager两种通知方式,前者相当于一个定时关闭的对话框,后者是在状态栏上显示一条消息。Toast和Notification都可以随时取消。

 

Toast

A toast is a view containing a quick little message for the user. The toast class helps you create and show those. Toast的使用很简单:

Toast.makeText(this, "Service destroyed…", Toast.LENGTH_LONG).show();

NotificationManager

NotificationManager负责通知用户事件的发生。

NotificationManager有三个公共方法:

1.    cancel(int id)    取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.

2.    cancelAll()    取消以前显示的所有通知。

3.    notify(int id, Notification notification)     把通知持久的发送到状态条上.

 

//初始化NotificationManager:

NotificationManager nm =

(NotificationManager)getSystemService(NOTIFICATION_SERVICE);

 

 

Notification代表着一个通知.

Notification的属性:

audioStreamType     当声音响起时,所用的音频流的类型

contentIntent     当通知条目被点击,就执行这个被设置的Intent.

contentView     当通知被显示在状态条上的时候,同时这个被设置的视图被显示.

defaults     指定哪个值要被设置成默认的.

deleteIntent     当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.

icon     状态条所用的图片.

iconLevel     假如状态条的图片有几个级别,就设置这里.

ledARGB    LED灯的颜色.

ledOffMS    LED关闭时的闪光时间(以毫秒计算)

ledOnMS     LED开始时的闪光时间(以毫秒计算)

number     这个通知代表事件的号码

sound     通知的声音

tickerText    通知被显示在状态条时,所显示的信息

vibrate     振动模式.

when     通知的时间戳.    

 

Notification的公共方法:

describeContents()    Describe the kinds of special objects contained in this Parcelable's marshalled representation.

setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) 设置Notification留言条的参数

writeToParcel(Parcel parcel, int flags)    Flatten this notification from a parcel.

toString() …………….

 

将Notification发送到状态条上:

 

Notification notification = new Notification(R.drawable.icon,

        "Service started", System.currentTimeMillis());

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,

        new Intent(this, Main.class), 0);

// must set this for content view, or will throw a exception

notification.setLatestEventInfo(this"Test Service",

        "Service started", contentIntent);

nm.notify(R.string.hello, notification);

 

Notification的取消

 

nm.cancel(R.string.hello);

 

本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2010/06/14/1758358.html,如需转载请自行联系原作者



Android中,消息通知权限(Notification Access)是指应用请求访问用户设备的通知渠道,以便发送状态更、提醒等信息。这是Android系统中的一个重要权限,因为它涉及到用户的隐私和设备管理。 配置步骤如下: 1. **添加权限声明**: 在`AndroidManifest.xml`文件中,需要在`<uses-permission>`标签内添加对`android.permission.GET_NOTIFICATIONS`或`android.permission.WAKE_LOCK`的声明,这两个权限通常一起用于获取通知权限: ```xml <uses-permission android:name="android.permission.GET_NOTIFICATIONS" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> ``` 2. **动态请求权限** (从API 23(Marshmallow)开始): 如果你的应用运行在M版本及以上,你需要在运行时请求权限。可以在`onCreate()`或需要使用通知功能的地方请求: ```java if (ContextCompat.checkSelfPermission(this, Manifest.permission.GET_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.GET_NOTIFICATIONS}, MY_PERMISSIONS_REQUEST_NOTIFICATION); } ``` 3. **处理权限结果**: 使用`ActivityCompat.onRequestPermissionsResult()`处理用户授予或拒绝的权限结果: ```java @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == MY_PERMISSIONS_REQUEST_NOTIFICATION && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // 用户同意,可以使用通知功能 } else { // 用户拒绝,提示无法使用 } } ``` 4. **创建通知**: 使用`NotificationManager`类创建并显示通知。确保在获得了通知权限之后操作。 记得在实际项目中根据需求调整,同时考虑用户体验和隐私策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值