1. Notification 通知
(1)notification的使用
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification= new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("This is a Notification")
.setContentText("It's time to sleep")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(MainActivity.this.getResources(),R.mipmap.ic_launcher))
.build();
(2)paddingIntent
Intent intent = new Intent(MainActivity.this,HomeActivity.class);
paddingIntent paddingIntent = PendingIntent.getActivity(MainActivity.class,0,intent,0);
NotificationManager manager = (NotificationManager )getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(MainActivity.this)
.setContentIntent(paddingIntent)
(3) 各种各样的set
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))
.setVibrate(new long[]{0,1000,1000,1000})
.setLights(Color.GREEN,1000,1000)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setStyle(new NotificationCompat.BigTextStyle().bigText("asdasdasd" +
"asdasdasdasdasdzxczxc" +
"asdasdasewrewfvsdvxv" +
"asdasdasd"))
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(MainActivity.this.getResources(), R.mipmap.ic_launcher_round)))
.setPriority(NotificationCompat.PRIORITY_MIN)
2.调用摄像头和相册
public class MainActivity extends AppCompatActivity {
public static final int TAKE_PHOTO = 1;
private ImageView imageView;
private Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.btn_take_photo);
imageView = findViewById(R.id.iv_picture);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File outputImage = new File(getExternalCacheDir(), "output_image.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException 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, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case TAKE_PHOTO:
if(requestCode == RESULT_OK){
try {
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
① 至于为什么要用 getExtranalCacheDir() 这个位置在sdcard/Android/data/<package name>/cache
因为存放在sd卡任何其他位置都需要权限
② 从android7.0开始 ,直接使用本地真实路径的Uri被认为是不安全的,会抛出异常,而FileProvider是一个特殊的内容提供器,可以选择性的将封装过的Uri共享给外部,从而提高应用的安全性
3.播放音频
MediaPlayer
VideoView