美文网首页
Android 多媒体及网络技术使用

Android 多媒体及网络技术使用

作者: 风之化身呀 | 来源:发表于2019-07-10 21:35 被阅读0次

1、使用通知 Notification

1、一般在广播接收器和服务里创建
2、基本用法

// 1、创建一个通知管理器 NotificationManager
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
// 2、创建一个通知
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("This is title")  // 标题
.setContentText("This is content") // 内容
.setWhen(System.currentTimeMillis()) // 创建的时间
.setSmallIcon(R.drawable.small_icon) // 小 icon
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.large_icon)) // 大 icon
.build()
// 3、出发通知
manager.notify(1,notification)

3、可点击的通知 PendingIndent

Indent indent = new Intent(this,NotificationActivity.class)
PendingIndent pi = PendingIntent.getActivity(this,0,intent,0)
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("This is title")  // 标题
.setContentText("This is content") // 内容
.setWhen(System.currentTimeMillis()) // 创建的时间
.setSmallIcon(R.drawable.small_icon) // 小 icon
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.large_icon)) // 大 icon
.setContentIntent(pi)   // 点击后的动作
.setAutoCancel(true)  // 点击后取消
.build()

2、使用摄像头和相册

// 打开摄像头
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE")
startActivity(intent)
// 打开相册
Intent intent = new Intent("android.intent.action.GET_CONTENT")
startActivity(intent) // 注意申请权限

3、使用音视频

可通过 MediaPlayer 操控音频播放,通过 VideoView 播放视频

4、使用 WebView

// 1、布局里使用 WebView
<WebView
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>
// 2、活动里设置 webview 的一些特性
WebView webviw = findViewById(R.id.web_view);
webview.getSettings().setJavascriptEnabled(true); // 允许运行JS
webview.setWebViewClient(new WebViewClient()); // 当从一个网页跳到另一个网页时,仍在当前webview展示,而不是打开系统浏览器
webView.loadUrl("baido.com") // 加载网页或者字符串
// 3、注意得申请网络权限

5、使用 OkHttp

Android 中用于发送 get,post 等请求的开源框架

// 1、添加依赖
compile:"com.squareup.okhttp3:okhttp:3.4.1"
// 2、发送get请求
OkHttpClient client = new OkHttpClient();  // 创建一个 OkHttpClient
Request request = new Request.Builder()  //  创建一个请求对象
     .url("http://www.baidu.com")
     .build()    
Response res = client.newCall(request).execute()  // 调用 newCall 发送请求
Log.i(res.body().string())
// 3、发送 post 请求
OkHttpClient client = new OkHttpClient();  // 创建一个 OkHttpClient
RequestBody requestBody = new FormBody.Builder()  // 创建请求体
      .add("username","admin")
      .add("password","111111")
      .build()
Request request = new Request.Builder()  //  创建一个请求对象
      .url("http://www.baidu.com")
      .post(requestBody)     // 声明使用 post 方法
      .build()    
Response res = client.newCall(request).execute()  // 调用 newCall 发送请求
Log.i(res.body().string())

相关文章

网友评论

      本文标题:Android 多媒体及网络技术使用

      本文链接:https://www.haomeiwen.com/subject/gtlvwftx.html