Notification的使用以及Intent打开各种文件
来源:互联网 发布:qq输入法mac版 编辑:程序博客网 时间:2024/06/14 22:21
一:Notification的使用
详细链接http://blog.csdn.net/vipzjyno1/article/details/25248021
使用步骤:
流程模块:
第一步:
创建一个通知栏的Builder构造类 (Create a Notification Builder)
第二步:
定义通知栏的Action (Define the Notification’s Action)
第三步:
设置通知栏点击事件 (Set the Notification’s Click Behavior)
第四步:
通知 (Issue the Notification)
notification案例:
//1,获取notificationManager myManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); //3.定义一个PendingIntent,点击Notification后启动一个Broadcast// PendingIntent intent = PendingIntent.getBroadcast(// context,// 100,// new Intent(context, NotificationClickReceiver.class),// PendingIntent.FLAG_CANCEL_CURRENT// ); //3.定义一个PendingIntent,点击Notification后启动一个Activity PendingIntent intent = PendingIntent.getActivity(context, 100, new Intent(context, NotificationActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); //2.通过Notification.Builder来创建通知 Notification.Builder myBuilder = new Notification.Builder(context); myBuilder.setContentTitle("文件下载") .setContentText("下载的文件是:" + keyName + sky) .setTicker("您收到新的消息") //设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示 .setSmallIcon(R.drawable.ic_launcher) //设置默认声音和震动 .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setAutoCancel(true)//点击后取消 .setWhen(System.currentTimeMillis())//设置通知时间 //android5.0加入了一种新的模式Notification的显示等级,共有三种: //VISIBILITY_PUBLIC 只有在没有锁屏时会显示通知 //VISIBILITY_PRIVATE 任何情况都会显示通知 //VISIBILITY_SECRET 在安全锁和没有锁屏的情况下显示通知 .setContentIntent(intent); //3.关联PendingIntent myNotification = myBuilder.build(); //4.通过通知管理器来发起通知,ID区分通知 myManager.notify(0, myNotification);
接受到通知,响应点击事件
二:Intent打开各种文件:
直接看代码:
package com.zte.meeting.ui.notification;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.FragmentActivity;import android.view.View;import android.widget.ImageButton;import android.widget.TextView;import com.zte.meeting.R;import com.zte.meeting.util.SharePreferenceUtil;import java.io.File;/** * Created by lovelin on 2017/4/17. */public class NotificationActivity extends FragmentActivity { private final String[][] MIME_MapTable = { //{后缀名,MIME类型} {".3gp", "video/3gpp"}, {".apk", "application/vnd.android.package-archive"}, {".asf", "video/x-ms-asf"}, {".avi", "video/x-msvideo"}, {".bin", "application/octet-stream"}, {".bmp", "image/bmp"}, {".c", "text/plain"}, {".class", "application/octet-stream"}, {".conf", "text/plain"}, {".cpp", "text/plain"}, {".doc", "application/msword"}, {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, {".xls", "application/vnd.ms-excel"}, {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, {".exe", "application/octet-stream"}, {".gif", "image/gif"}, {".gtar", "application/x-gtar"}, {".gz", "application/x-gzip"}, {".h", "text/plain"}, {".htm", "text/html"}, {".html", "text/html"}, {".jar", "application/java-archive"}, {".java", "text/plain"}, {".jpeg", "image/jpeg"}, {".jpg", "image/jpeg"}, {".js", "application/x-JavaScript"}, {".log", "text/plain"}, {".m3u", "audio/x-mpegurl"}, {".m4a", "audio/mp4a-latm"}, {".m4b", "audio/mp4a-latm"}, {".m4p", "audio/mp4a-latm"}, {".m4u", "video/vnd.mpegurl"}, {".m4v", "video/x-m4v"}, {".mov", "video/quicktime"}, {".mp2", "audio/x-mpeg"}, {".mp3", "audio/x-mpeg"}, {".mp4", "video/mp4"}, {".mpc", "application/vnd.mpohun.certificate"}, {".mpe", "video/mpeg"}, {".mpeg", "video/mpeg"}, {".mpg", "video/mpeg"}, {".mpg4", "video/mp4"}, {".mpga", "audio/mpeg"}, {".msg", "application/vnd.ms-outlook"}, {".ogg", "audio/ogg"}, {".pdf", "application/pdf"}, {".png", "image/png"}, {".pps", "application/vnd.ms-powerpoint"}, {".ppt", "application/vnd.ms-powerpoint"}, {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, {".prop", "text/plain"}, {".rc", "text/plain"}, {".rmvb", "audio/x-pn-realaudio"}, {".rtf", "application/rtf"}, {".sh", "text/plain"}, {".tar", "application/x-tar"}, {".tgz", "application/x-compressed"}, {".txt", "text/plain"}, {".wav", "audio/x-wav"}, {".wma", "audio/x-ms-wma"}, {".wmv", "audio/x-ms-wmv"}, {".wps", "application/vnd.ms-works"}, {".xml", "text/plain"}, {".z", "application/x-compress"}, {".zip", "application/x-zip-compressed"}, {"", "*/*"} }; private String path; private TextView tv_notii_file; private ImageButton break_; private TextView tvTitle; private String type; private String currentType; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); path = (String) SharePreferenceUtil.get(this, SharePreferenceUtil.NOTIFICATION_PATCH, SharePreferenceUtil.NOTIFICATION_DEFFOUR); initView(); responseListener(); } private void initView() { tv_notii_file = (TextView) findViewById(R.id.tv_notii_file); break_ = (ImageButton) findViewById(R.id.break_); tvTitle = (TextView) findViewById(R.id.tvTitle); initDate(); } private void initDate() { setDownFileName(); break_.setVisibility(View.VISIBLE); tvTitle.setText("当前下载文件"); } private void responseListener() { tv_notii_file.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { openFile(new File(path).getAbsoluteFile()); } }); break_.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } /** * 设置大部分文件的通俗名 */ private void setDownFileName() { int i = path.lastIndexOf("/"); String substring = path.substring(i, path.length()); int i1 = substring.lastIndexOf("."); currentType = substring.substring(i1, substring.length()); switch (currentType) { case ".doc": //文档 tv_notii_file.setText("当前下载是:" + "(" + "World文档" + ")" + "文件"); break; case ".docx": tv_notii_file.setText("当前下载是:" + " (" + "World文档" + ")" + " 文件"); break; case ".xls": //表格 tv_notii_file.setText("当前下载是:" + " (" + "Excel表格" + ")" + " 文件"); break; case ".xlsx": tv_notii_file.setText("当前下载是:" + " (" + "Excel表格" + ")" + " 文件"); break; case ".ppt": //文档 tv_notii_file.setText("当前下载是:" + "(" + "PPT" + ")" + "文件"); break; case ".pptx": //文档 tv_notii_file.setText("当前下载是:" + "(" + "PPT" + ")" + "文件"); break; case ".png": //图片 tv_notii_file.setText("当前下载是:" + " (" + ".png图片" + ")" + " 文件"); break; case ".jpg": tv_notii_file.setText("当前下载是:" + " (" + ".jpg图片" + ")" + " 文件"); break; case ".mp3": //音乐 tv_notii_file.setText("当前下载是:" + " (" + ".mp3音乐" + ")" + " 文件"); break; case ".mp4": tv_notii_file.setText("当前下载是:" + " (" + ".mp4音乐" + ")" + " 文件"); break; case ".avi": //视频 tv_notii_file.setText("当前下载是:" + " (" + ".avi视频" + ")" + " 文件"); break; default: tv_notii_file.setText("当前下载是:" + " (" + currentType + ")" + " 文件"); break; } } /** * 打开文件 * * @param file */ private void openFile(File file) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //设置intent的Action属性 intent.setAction(Intent.ACTION_VIEW); //获取文件file的MIME类型 type = getMIMEType(file); //设置intent的data和Type属性。 intent.setDataAndType(/*uri*/Uri.fromFile(file), type); //跳转 startActivity(intent); //这里最好try一下,有可能会报错。 //比如说你的MIME类型是打开邮箱,但是你手机里面没装邮箱客户端,就会报错。 } /** * 根据文件后缀名获得对应的MIME类型。 * * @param file */ private String getMIMEType(File file) { String type = "*/*"; String fName = file.getName(); //获取后缀名前的分隔符"."在fName中的位置。 int dotIndex = fName.lastIndexOf("."); if (dotIndex < 0) { return type; } /* 获取文件的后缀名*/ String end = fName.substring(dotIndex, fName.length()).toLowerCase(); if (end == "") return type; //在MIME和文件类型的匹配表中找到对应的MIME类型。 for (int i = 0; i < MIME_MapTable.length; i++) { //MIME_MapTable??在这里你一定有疑问,这个MIME_MapTable是什么? if (end.equals(MIME_MapTable[i][0])) type = MIME_MapTable[i][1]; } return type; }}
最后:注释代码都有自己看
0 0
- Notification的使用以及Intent打开各种文件
- 打开各种文件的intent
- android 使用intent 打开各种文件方式
- Android--使用Intent打开各种文件
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- Android用于打开各种文件的intent
- 【Android】用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- android用于打开各种文件的intent
- [[DobbyBlok alloc] init];
- JavaFX8 modena样式(css)源码
- APP安全报告第四期:如果共享单车APP会泄漏敏感信息,你还会用吗?
- 链表排序——插入排序(纯C语言版)
- spring集成redis及简单使用
- Notification的使用以及Intent打开各种文件
- Myeclipse2015下载github上项目到本地(图解)
- 贴花编辑器实现细节
- 电力系统可视化图形建模系统V1.4
- OpenGL之矩阵变换Matrix
- AES五种加密模式(CBC、ECB、CTR、OCF、CFB)
- Html Input表单校验
- 大数据云计算学习路线图
- APP安全报告第五期:盗版天气应用程序肆意窃取银行用户信息,你中招了吗?