Notification的使用以及Intent開啟各種檔案
阿新 • • 發佈:2019-02-14
一:Notification的使用
使用步驟:
流程模組:
第一步:
建立一個通知欄的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;
}
}