Hanlder机制
andorid 异步机制 俗称handler机制
handler机制包括
message 消息 handler 句柄 messageQueue 消息队列 looper 轮询器
message message
handler -------------->messageQueue-------------->looper(looper是主线程中的死循环)
主线程中存在一个死循环looper
主线程为什么不会卡死?
Android内核是linux系统,主线程在运行的时候是一种沉睡唤醒机制。即当消息队列中没有消息的时候主线程会休眠,当消息队列存在消息的时候主线程就会被唤醒(IBinder机制);
Loop 一个loop维护一个线程
多线程 线程池 等等
线程池最佳线程数:核心数x2+1
通知:
Notification 手机最上面的一行黑色的状态栏 显示有联网状态等
类似于推送 不过这个是本地做的
可以理解为本地推送
使用步骤
1.获取通知服务对象NotificationManager
2.建立Notification对象
3.关联intent
4.执行通知
使用比较简单
也可能会遇到未 比如兼容问题,上述已经解决使用support提供得方法
new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")
.setContentText("具体内容")
.setSmallIcon(R.mipmap.ic_launcher)
//.setLargeIcon(aBitmap)
.build();
//震动
notification.defaults = Notification.DEFAULT_VIBRATE;
//一直到用户响应结束通知 如铃声或者震动等 开启震动后配合使用
notification.flags = Notification.FLAG_INSISTENT;
//通知可以清除
notification.flags=Notification.FLAG_AUTO_CANCEL;
示例:
notification = new NotificationCompat.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("推送单条").setContentText("推送单条内容,点击后可以跳转页面").build();
//通知可以清除
notification.flags=Notification.FLAG_AUTO_CANCEL;
//震动
notification.defaults = Notification.DEFAULT_VIBRATE;
//一直到用户响应结束通知 如铃声或者震动等 开启震动后配合使用
notification.flags = Notification.FLAG_INSISTENT;
Intent notificationIntent = new Intent(MainActivity.this, Main2Activity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
notification.contentIntent=contentIntent;
notificationManager.notify(1,notification);
一般使用 大多数还是使用类似极光推送的东西
接受 发送 拦截 短信 等
调用摄像头和相册
调用摄像头 拍照
File outputImage = new File(Environment.
getExternalStorageDirectory(), "tempImage.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.media.action. IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
上面是调用摄像头拍照前的部门 下面还有拍照之后的部分 卸载onActivityResult里面
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO); // 启动裁剪程序
}
break;
case CROP_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream
(getContentResolver()
.openInputStream(imageUri));
picture.setImageBitmap(bitmap); // 将裁剪后的照片显示出来
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
播放音频
try {
File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");
mediaPlayer.setDataSource(file.getPath()); // 指定音频文件的路径
mediaPlayer.prepare(); // 让MediaPlayer进入到准备状态
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.start(); // 开始播放
mediaPlayer.pause(); // 暂停播放
mediaPlayer.reset(); // 停止播放
mediaPlayer.stop();
mediaPlayer.release();
播放视频
File file = new File(Environment.getExternalStorageDirectory(),"movie.3gp");
videoView.setVideoPath(file.getPath()); // 指定视频文件的路径
videoView.start(); // 开始播放
videoView.pause(); // 暂时播放
videoView.resume(); // 重新播放
videoView.suspend();
AsyncTask 异步 用的不多了 可以使用RxAndroid eventBus代替 是官方的Handler封装
webView 加载网页的控件 使用较为简单
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, Stringurl) {
view.loadUrl(url); // 根据传入的参数再去加载新的网页
return true; // 表示当前WebView可以处理打开新网页的请求,不用借助系统浏览器
}
});
webView.loadUrl("http://www.baidu.com");
通过 webView.setWebViewClient(new WebViewClient()) 的方式可以调用js代码
通过 webView.addJavascriptInterface(对象,“名称”); 的方式向 js端注入一个含有本地方法的对象,js端可以根据我们传入的名称直接调用方法名,通过回调的方式来调用我们的代码(具体实现是js中封装好的一个api)
网络请求
HttpURLConnection Httpclient 阿帕奇(号称被移除了,如果想要使用的话需要手动导入了不再是内置支持) 在新版本中使用okHttp
传感器 SensorManager
光照 (SensorManager)getSystemService(Context.SENSOR_SERVICE);
加速度 sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
方向 sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
手势 GustureDecetor
MediaPlayer 播放音乐
SoundPool 播放较短的声音,如碰撞音 战斗 音 占用资源小 使用方便
VideoView 播放视频
andorid 异步机制 俗称handler机制
handler机制包括
message 消息 handler 句柄 messageQueue 消息队列 looper 轮询器
message message
handler -------------->messageQueue-------------->looper(looper是主线程中的死循环)
主线程中存在一个死循环looper
主线程为什么不会卡死?
Android内核是linux系统,主线程在运行的时候是一种沉睡唤醒机制。即当消息队列中没有消息的时候主线程会休眠,当消息队列存在消息的时候主线程就会被唤醒(IBinder机制);
Loop 一个loop维护一个线程
多线程 线程池 等等
线程池最佳线程数:核心数x2+1
通知:
Notification 手机最上面的一行黑色的状态栏 显示有联网状态等
类似于推送 不过这个是本地做的
可以理解为本地推送
使用步骤
1.获取通知服务对象NotificationManager
2.建立Notification对象
3.关联intent
4.执行通知
使用比较简单
也可能会遇到未 比如兼容问题,上述已经解决使用support提供得方法
new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("标题")
.setContentText("具体内容")
.setSmallIcon(R.mipmap.ic_launcher)
//.setLargeIcon(aBitmap)
.build();
//震动
notification.defaults = Notification.DEFAULT_VIBRATE;
//一直到用户响应结束通知 如铃声或者震动等 开启震动后配合使用
notification.flags = Notification.FLAG_INSISTENT;
//通知可以清除
notification.flags=Notification.FLAG_AUTO_CANCEL;
示例:
notification = new NotificationCompat.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("推送单条").setContentText("推送单条内容,点击后可以跳转页面").build();
//通知可以清除
notification.flags=Notification.FLAG_AUTO_CANCEL;
//震动
notification.defaults = Notification.DEFAULT_VIBRATE;
//一直到用户响应结束通知 如铃声或者震动等 开启震动后配合使用
notification.flags = Notification.FLAG_INSISTENT;
Intent notificationIntent = new Intent(MainActivity.this, Main2Activity.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
notification.contentIntent=contentIntent;
notificationManager.notify(1,notification);
一般使用 大多数还是使用类似极光推送的东西
接受 发送 拦截 短信 等
调用摄像头和相册
调用摄像头 拍照
File outputImage = new File(Environment.
getExternalStorageDirectory(), "tempImage.jpg");
try {
if (outputImage.exists()) {
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = Uri.fromFile(outputImage);
Intent intent = new Intent("android.media.action. IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PHOTO);
上面是调用摄像头拍照前的部门 下面还有拍照之后的部分 卸载onActivityResult里面
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PHOTO:
if (resultCode == RESULT_OK) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imageUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, CROP_PHOTO); // 启动裁剪程序
}
break;
case CROP_PHOTO:
if (resultCode == RESULT_OK) {
try {
Bitmap bitmap = BitmapFactory.decodeStream
(getContentResolver()
.openInputStream(imageUri));
picture.setImageBitmap(bitmap); // 将裁剪后的照片显示出来
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
default:
break;
}
}
}
播放音频
try {
File file = new File(Environment.getExternalStorageDirectory(),"music.mp3");
mediaPlayer.setDataSource(file.getPath()); // 指定音频文件的路径
mediaPlayer.prepare(); // 让MediaPlayer进入到准备状态
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.start(); // 开始播放
mediaPlayer.pause(); // 暂停播放
mediaPlayer.reset(); // 停止播放
mediaPlayer.stop();
mediaPlayer.release();
播放视频
File file = new File(Environment.getExternalStorageDirectory(),"movie.3gp");
videoView.setVideoPath(file.getPath()); // 指定视频文件的路径
videoView.start(); // 开始播放
videoView.pause(); // 暂时播放
videoView.resume(); // 重新播放
videoView.suspend();
AsyncTask 异步 用的不多了 可以使用RxAndroid eventBus代替 是官方的Handler封装
webView 加载网页的控件 使用较为简单
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, Stringurl) {
view.loadUrl(url); // 根据传入的参数再去加载新的网页
return true; // 表示当前WebView可以处理打开新网页的请求,不用借助系统浏览器
}
});
webView.loadUrl("http://www.baidu.com");
通过 webView.setWebViewClient(new WebViewClient()) 的方式可以调用js代码
通过 webView.addJavascriptInterface(对象,“名称”); 的方式向 js端注入一个含有本地方法的对象,js端可以根据我们传入的名称直接调用方法名,通过回调的方式来调用我们的代码(具体实现是js中封装好的一个api)
网络请求
HttpURLConnection Httpclient 阿帕奇(号称被移除了,如果想要使用的话需要手动导入了不再是内置支持) 在新版本中使用okHttp
解析xml 解析json 使用Gson解析json xml基本不怎么用
传感器 SensorManager
光照 (SensorManager)getSystemService(Context.SENSOR_SERVICE);
加速度 sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
方向 sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
手势 GustureDecetor
MediaPlayer 播放音乐
SoundPool 播放较短的声音,如碰撞音 战斗 音 占用资源小 使用方便
VideoView 播放视频
MediaPalyer 和SurfaceView 播放视频 SurfaceView播放动画 MediaPlayer播放声音
内容提供者:
这个和数据库很类似 不同之处在于增删改查的时候需要一个uri 而不是表名,他的用处在于提供接口让其他的app来放我你的app,实际用的时候是在做第三方 ,且有较多人使用你提供的数据时才会用到。