package com.example.admin.rxjava.ui.activity;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.blankj.utilcode.util.PermissionUtils;
import com.example.admin.rxjava.R;
import com.example.admin.rxjava.ui.net.ApiService;
import com.example.admin.rxjava.ui.net.HttpRetrofitClient;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class PublishMainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tvDownload;
private TextView tvSinglePermission = null;
private TextView tvMuticePermission = null;
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
private static final int MY_PERMISSIONS_REQUEST_CALL_CAMERA = 2;
//运行时候需要的所有权限
String[] permissions = new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE
};
//声明一个集合 装用户手动拒绝的权限
List<String> permissionList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_publish_main);
tvDownload = findViewById(R.id.tv_download_file);
tvSinglePermission = this.findViewById(R.id.tv_single_permission);
tvMuticePermission = this.findViewById(R.id.tv_multice_permission);
tvSinglePermission.setOnClickListener(this);
tvMuticePermission.setOnClickListener(this);
tvDownload.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_single_permission: //单个授权
int sdk_int = Build.VERSION.SDK_INT;
int android_m = Build.VERSION_CODES.M;
Toast.makeText(PublishMainActivity.this, "sdk_int" + sdk_int + "," + android_m, Toast.LENGTH_LONG).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//如果当前系统是大于23的话
if (PermissionUtils.isGranted(Manifest.permission.CALL_PHONE)) {
//检查用户是否授权
Toast.makeText(PublishMainActivity.this, "用户已经授权", Toast.LENGTH_LONG).show();
} else {
//请求授权 系统会打开弹窗
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
}
}
break;
case R.id.tv_multice_permission:
break;
case R.id.tv_download_file:
ApiService apiService = HttpRetrofitClient.getInstance().create(ApiService.class);
Observable<ResponseBody> observable = apiService.downLoadFile("");
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<ResponseBody>() {
@Override
public void accept(ResponseBody responseBody) throws Exception {
InputStream inputStream = responseBody.byteStream();
//new RandomAccessFile()
}
});
break;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//打电话权限已经申请
Log.i("gdchent", "result");
} else {
Log.i("gdchent", "用户没有授权");
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
网友评论