通知的基本用法
通过NotificationManager 来管理通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentTitle("This is a Title");
//builder.setStyle(new NotificationCompat.BigTextStyle().bigText("fdsfsfsdfsfsafsafsafs" +
// "sdfsdfsdfsdfsfsfafsfsafsdfsdfsfsfsfsfsffwfwfwfsdfsdfsafsafasf"));
//设置长文字
builder.setStyle(new NotificationCompat.BigPictureStyle().bigLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)));
builder.setContentText("THis is a text");
builder.setSmallIcon(R.mipmap.ic_launcher);
//builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
Intent intent = new Intent(MainActivity.this,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
builder.setPriority(NotificationCompat.PRIORITY_MAX);//设置通知的重要程度
builder.setContentIntent(pi);
builder.setAutoCancel(true);
//builder.setSound(Uri.fromFile(new File("")));
builder.setVibrate(new long[]{0,1000,1000,1000});//第一个和第3个参数代表静止时长,另外俩个代表震动时间
builder.setLights(Color.GREEN,1000,1000);
//builder.setDefaults(NotificationCompat.DEFAULT_ALL);//设置默认
Notification notification = builder.build();
manager.notify(1,notification);
调用照相机拍照
private void cameraPhoto() {
File outputImage = new File(getExternalCacheDir(),"output_image.jpg");
try{
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
if(Build.VERSION.SDK_INT >= 24){
imageUri = FileProvider.getUriForFile(MainActivity.this,
"com.example.cameraalbumtest.fileprovider",outputImage);
}else {
imageUri = Uri.fromFile(outputImage);
}
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,TAKE_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case TAKE_PHOTO:
if(resultCode == RESULT_OK){
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
imageView.setImageBitmap(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
}
<provider
android:authorities="com.example.cameraalbumtest.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:name="android.support.v4.content.FileProvider">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"/>
</provider>
res目录下新建xml文件夹
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name = "my_images" path = ""/>
</paths>
内存卡中读取图片
private void chooseFromAlbum() {
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}else {
openAlbum();
}
}
private void openAlbum() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent,CHOOSE_ALBUM);
}
case CHOOSE_ALBUM:
if(resultCode == RESULT_OK){
if(Build.VERSION.SDK_INT>=19){
handleImageOnKitKat(data);
}else {
handleImageBeforeKitKat(data);
}
}
}
}
private void handleImageBeforeKitKat(Intent data) {
Uri uri = data.getData();
String imagePath = getImagePath(uri,null);
displayImage(imagePath);
}
@TargetApi(19)
private void handleImageOnKitKat(Intent data) {
String imagePath = null;
Uri uri = data.getData();
if(DocumentsContract.isDocumentUri(this,uri)){
//如果是document类型的uri,则通过documentId处理
String docId = DocumentsContract.getDocumentId(uri);
if("com.android.providers.media.documents".equals(uri.getAuthority())){
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
imagePath = getImagePath(contentUri,null);
}
}else if("content".equalsIgnoreCase(uri.getScheme())){
//如果是content了类型的Uri,则使用普通方法处理
imagePath = getImagePath(uri,null);
}else if("file".equalsIgnoreCase(uri.getScheme())){
//如果是file 类型的uri 直接获取图片路径即可
imagePath = uri.getPath();
}
displayImage(imagePath);
}
private void displayImage(String imagePath) {
if(imagePath!=null){
imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
}
}
private String getImagePath(Uri externalContentUri, String selection) {
String path = null;
Cursor cursor = getContentResolver().query(externalContentUri,null,selection,null,null);
if(cursor!=null){
while (cursor.moveToNext()){
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
}
通过NotificationManager 来管理通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
builder.setContentTitle("This is a Title");
//builder.setStyle(new NotificationCompat.BigTextStyle().bigText("fdsfsfsdfsfsafsafsafs" +
// "sdfsdfsdfsdfsfsfafsfsafsdfsdfsfsfsfsfsffwfwfwfsdfsdfsafsafasf"));
//设置长文字
builder.setStyle(new NotificationCompat.BigPictureStyle().bigLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)));
builder.setContentText("THis is a text");
builder.setSmallIcon(R.mipmap.ic_launcher);
//builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
Intent intent = new Intent(MainActivity.this,MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,0,intent,0);
builder.setPriority(NotificationCompat.PRIORITY_MAX);//设置通知的重要程度
builder.setContentIntent(pi);
builder.setAutoCancel(true);
//builder.setSound(Uri.fromFile(new File("")));
builder.setVibrate(new long[]{0,1000,1000,1000});//第一个和第3个参数代表静止时长,另外俩个代表震动时间
builder.setLights(Color.GREEN,1000,1000);
//builder.setDefaults(NotificationCompat.DEFAULT_ALL);//设置默认
Notification notification = builder.build();
manager.notify(1,notification);
调用照相机拍照
private void cameraPhoto() {
File outputImage = new File(getExternalCacheDir(),"output_image.jpg");
try{
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
if(Build.VERSION.SDK_INT >= 24){
imageUri = FileProvider.getUriForFile(MainActivity.this,
"com.example.cameraalbumtest.fileprovider",outputImage);
}else {
imageUri = Uri.fromFile(outputImage);
}
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,TAKE_PHOTO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case TAKE_PHOTO:
if(resultCode == RESULT_OK){
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
imageView.setImageBitmap(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
}
}
<provider
android:authorities="com.example.cameraalbumtest.fileprovider"
android:exported="false"
android:grantUriPermissions="true"
android:name="android.support.v4.content.FileProvider">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path"/>
</provider>
res目录下新建xml文件夹
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name = "my_images" path = ""/>
</paths>
内存卡中读取图片
private void chooseFromAlbum() {
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
}else {
openAlbum();
}
}
private void openAlbum() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent,CHOOSE_ALBUM);
}
case CHOOSE_ALBUM:
if(resultCode == RESULT_OK){
if(Build.VERSION.SDK_INT>=19){
handleImageOnKitKat(data);
}else {
handleImageBeforeKitKat(data);
}
}
}
}
private void handleImageBeforeKitKat(Intent data) {
Uri uri = data.getData();
String imagePath = getImagePath(uri,null);
displayImage(imagePath);
}
@TargetApi(19)
private void handleImageOnKitKat(Intent data) {
String imagePath = null;
Uri uri = data.getData();
if(DocumentsContract.isDocumentUri(this,uri)){
//如果是document类型的uri,则通过documentId处理
String docId = DocumentsContract.getDocumentId(uri);
if("com.android.providers.media.documents".equals(uri.getAuthority())){
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
}else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){
Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
imagePath = getImagePath(contentUri,null);
}
}else if("content".equalsIgnoreCase(uri.getScheme())){
//如果是content了类型的Uri,则使用普通方法处理
imagePath = getImagePath(uri,null);
}else if("file".equalsIgnoreCase(uri.getScheme())){
//如果是file 类型的uri 直接获取图片路径即可
imagePath = uri.getPath();
}
displayImage(imagePath);
}
private void displayImage(String imagePath) {
if(imagePath!=null){
imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath));
}
}
private String getImagePath(Uri externalContentUri, String selection) {
String path = null;
Cursor cursor = getContentResolver().query(externalContentUri,null,selection,null,null);
if(cursor!=null){
while (cursor.moveToNext()){
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
}