Android开发之Notification(通知)
来源:互联网 发布:郭德纲合作网络平台 编辑:程序博客网 时间:2024/06/11 10:31
通知(Notification)是Android系统中比较有特色的一个功能,当某个应用程序希望向用户发出一条提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。
通知的基本用法,通过一个小demo来学习一下:
1,先看activity_main.xml布局,很简单放了一个button;
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.gyq.notificationtest.MainActivity"> <Button android:id="@+id/btn_send" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="send notice"/></RelativeLayout>
2,在看MainActivity.java
package com.gyq.notificationtest;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.graphics.BitmapFactory;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button mSend = (Button)findViewById(R.id.btn_send); mSend.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_send : //下拉后跳转到相应的活动 Intent intent = new Intent(this,NotificationActivity.class); PendingIntent pi = PendingIntent.getActivity(this,0,intent,0); NotificationManager manager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this) .setContentTitle("this is content title") //指定通知的标题内容 .setContentText("this is content text") //指定通知的正文内容 .setWhen(System.currentTimeMillis()) //指定通知被创建的时间 .setSmallIcon(R.mipmap.ic_launcher) //设置通知的小图标,注意只能使用纯alpha图层的图片进行设置 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) //设置通知的大图标 .setContentIntent(pi) .setAutoCancel(true) //让通知的小图标消失 //.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luga.ogg"))) //通知到了响起音频 //.setVibrate(new long[] {0,1000,1000,1000}) //震动 //.setLights(Color.GREEN,1000,1000) //LED灯一闪一闪的效果 .setDefaults(NotificationCompat.DEFAULT_ALL) .setStyle(new NotificationCompat.BigTextStyle().bigText("this is content text this is content text this is content text this is content text\" +\n" + " \"this is content text this is content text this is content text this is content text this is content text\" +\n" + " \"this is content text this is content text this is content text this is content text")) .build(); manager.notify(1,notification); break; default: break; } }}
3,添加震动权限:
<uses-permission android:name="android.permission.VIBRATE"/>
4,上述代码中提到的NotificationActivity.java以及其布局文件。
package com.gyq.notificationtest;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class NotificationActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); /*NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(1);*/ }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.gyq.notificationtest.NotificationActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="this this notification content h" android:textSize="24sp"/></RelativeLayout>
完成。
接下来解释一下,上述代码中的内容。
1,使用PendingIntent,可以理解为延迟执行的Intent,第一个参数是Context,第二个参数一般用不到,通常设置为0;第三个参数是Intent对象;第四个参数一般情况下传入0。解释一下:来通知了,我们一般会下来看到然后要点击进入看相关的内容,就需要用到这个PendingIntent。
2,让系统状态上的通知图标消失的两个方法:
方法一:
Notification notification = new NotificationCompat.Builder(this) ... .setAutoCancel(true) .build();
方法二:在要跳转到的,写这几代码。
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); manager.cancel(1);
进阶技巧:
1,设置通知到达时,有音乐提醒;
Notification notification = new NotificationCompat.Builder(this) ... .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luga.ogg"))) //通知到了响起音频 .build();
2,设置通知到达时,有震动提醒,需要添加震动权限。
Notification notification = new NotificationCompat.Builder(this) ... .setVibrate(new long[] {0,1000,1000,1000}) //震动 .build();
3,LED灯闪烁来提示用户去查看;
Notification notification = new NotificationCompat.Builder(this) ... .setLights(Color.GREEN,1000,1000) //LED灯一闪一闪的效果 .build();
4,直接使用通知的默认效果:
Notification notification = new NotificationCompat.Builder(this) ... .setDefaults(NotificationCompat.DEFAULT_ALL) .build();
5,setStyle()方法:由于内容过多会导致通知内容显示不全,已省略代替。然而这个方法可以显示全。
....setStyle(new NotificationCompat.BigTextStyle().bigText("this is content text this is content text this is content text this is content text\" +\n" + " \"this is content text this is content text this is content text this is content text this is content text\" +\n" + " \"this is content text this is content text this is content text this is content text"))...
还可以显示一张大图片。
6,设置通知的重要程度。setPriority()方法,NotificationCompat.PRIORITY_MAX参数是一天非常重要的通知,效果类似弹出一条横幅。
0 0
- Android开发之Notification(通知)
- Android开发之通知栏Notification详解
- android开发之notification通知完全解析
- Android开发之Notification 通知栏
- Android开发之通知栏Notification详解
- android之Notification通知
- android之Notification通知
- android之Notification通知
- android之Notification通知
- android之Notification通知
- android之Notification通知
- Android之Notification通知
- android之Notification通知
- android之Notification通知
- android之Notification通知
- Android--通知之Notification
- android--通知之Notification
- android之Notification通知
- [Mapbox GL]改变地图语言
- 要做安卓开发人员需要具备的知识
- Android WebView遇到的坑
- MySQL中函数CONCAT及GROUP_CONCAT
- Cannot get https://gerrit.googlesource.com/git-repo/clone.bundle
- Android开发之Notification(通知)
- 流程表单中js如何清空SheetUser控件数据?
- windows更新一周年版本,更新失败完全解决方案(绝对成功)
- Ajax大杂烩
- Repeater嵌套绑定Repeater以及内层调用外层数据
- Fragment生命周期,使用注意事项
- linux管道pipe详解
- 性能测试高级工程师培训总结
- 编程之美(数字的魅力)