接续上一篇博客实现通知:
sendNotification(); 截图完发送通知,并且点击通知可以查看该截图
不过下面这些字段是全局的,把上一篇博客《Android保存图片到系统图库和指定的文件夹》中的变量换成下面的即可:
private Button bt, bt_image, bt_noti;
private Context context;
Bitmap bitmap, bmp;
private Notification notification = null;
private ImageView imgShow;
File appDir;
File file;
String fileName;
String TAG="ViewActivity";
添加权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
sendNotification()方法实现:
private void sendNotification() {
NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(ViewActivity.this);
Intent intent = new Intent(Intent.ACTION_VIEW);
//intent.setData(Uri.fromFile(file));
//intent.setType("image/*");
intent.setDataAndType(Uri.fromFile(file),"image/*");//试了下上面分开写setData和setType不能实现相同效果
Log.i(TAG,(file==null)+ "");
Log.i(TAG, "uri============"+Uri.fromFile(file));
// startActivityForResult(intent,0);填充imageview
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setTicker("正在截屏...");
builder.setContentTitle("已捕捉屏幕截图");
builder.setContentText("点击以查看截屏");
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeFile(file.toString()));
builder.setContentIntent(contentIntent);
builder.setAutoCancel(true);
notification = builder.build();
mManager.notify(0, notification);
}