|
@@ -1,74 +1,121 @@
|
|
package me.yoqi.deleteallcontacts;
|
|
package me.yoqi.deleteallcontacts;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-
|
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
|
+import android.content.pm.PackageManager;
|
|
|
|
+import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.os.Message;
|
|
-import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import android.view.View;
|
|
import android.view.View;
|
|
import android.view.View.OnClickListener;
|
|
import android.view.View.OnClickListener;
|
|
import android.widget.Button;
|
|
import android.widget.Button;
|
|
import android.widget.TextView;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
import android.widget.Toast;
|
|
|
|
+
|
|
|
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
|
+
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+
|
|
import me.yoqi.deleteallcontacts.utils.ContactManager;
|
|
import me.yoqi.deleteallcontacts.utils.ContactManager;
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * @Description: main activity
|
|
|
|
+ * @Date: 2023年03月28日 Tuesday
|
|
|
|
+ * @Author liuyuqi.gov@msn.
|
|
|
|
+ */
|
|
public class MainActivity extends AppCompatActivity {
|
|
public class MainActivity extends AppCompatActivity {
|
|
- Button btnDelUser;
|
|
|
|
- TextView tvContact;
|
|
|
|
- Context mContext;
|
|
|
|
- static String TAG = "me.yoqi.deleteallcontacts.MainActivity";
|
|
|
|
- ContactManager contactManager;
|
|
|
|
- MyHandler mHandler ;
|
|
|
|
-
|
|
|
|
- private static class MyHandler extends Handler {
|
|
|
|
-
|
|
|
|
- public MyHandler(Context mContext) {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void handleMessage(Message msg) {
|
|
|
|
- super.handleMessage(msg);
|
|
|
|
- switch (msg.what) {
|
|
|
|
- case 0:
|
|
|
|
- // 更新UI
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- super.handleMessage(msg);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
- super.onCreate(savedInstanceState);
|
|
|
|
- setContentView(R.layout.activity_main);
|
|
|
|
- mContext = this;
|
|
|
|
- contactManager = new ContactManager(mContext);
|
|
|
|
- initView();
|
|
|
|
- initData();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- void initView() {
|
|
|
|
- btnDelUser = (Button) findViewById(R.id.btn_del_contact);
|
|
|
|
- tvContact = (TextView) findViewById(R.id.tv_contact);
|
|
|
|
- btnDelUser.setOnClickListener(new OnClickListener() {
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void onClick(View v) {
|
|
|
|
- HashMap<String, Object> contactNum = contactManager.delAllContacts();
|
|
|
|
- Toast.makeText(mContext, "已经删除 " + contactNum.size() + " 个联系人", Toast.LENGTH_SHORT).show();
|
|
|
|
- initData();
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- void initData() {
|
|
|
|
- mHandler = new MyHandler(this);
|
|
|
|
-
|
|
|
|
- ArrayList<String> allContact = contactManager.getAllContact();
|
|
|
|
- tvContact.setText(allContact.toString());
|
|
|
|
- }
|
|
|
|
|
|
+ Button btnDelUser;
|
|
|
|
+ TextView tvContact;
|
|
|
|
+ RecyclerView rvContact;
|
|
|
|
+ Context mContext;
|
|
|
|
+ static String TAG = "me.yoqi.deleteallcontacts.MainActivity";
|
|
|
|
+ ContactManager contactManager;
|
|
|
|
+ MyHandler mHandler;
|
|
|
|
+
|
|
|
|
+ private static class MyHandler extends Handler {
|
|
|
|
+
|
|
|
|
+ public MyHandler(Context mContext) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
|
+ super.handleMessage(msg);
|
|
|
|
+ switch (msg.what) {
|
|
|
|
+ case 0:
|
|
|
|
+ // 更新UI
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ super.handleMessage(msg);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
|
+ setContentView(R.layout.activity_main);
|
|
|
|
+ mContext = this;
|
|
|
|
+ contactManager = new ContactManager(mContext);
|
|
|
|
+ initView();
|
|
|
|
+ initData();
|
|
|
|
+ checkUpdate();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void initView() {
|
|
|
|
+ btnDelUser = (Button) findViewById(R.id.btn_del_contact);
|
|
|
|
+ tvContact = (TextView) findViewById(R.id.tv_contact);
|
|
|
|
+ rvContact = (RecyclerView) findViewById(R.id.rv_contact);
|
|
|
|
+ btnDelUser.setOnClickListener(new OnClickListener() {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onClick(View v) {
|
|
|
|
+ HashMap<String, Object> contactNum = contactManager.delAllContacts();
|
|
|
|
+ Toast.makeText(mContext, "已经删除 " + contactNum.size() + " 个联系人", Toast.LENGTH_SHORT).show();
|
|
|
|
+ initData();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ // 请求删除联系人权限 sdk>23
|
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
+ if (!hasPermission("android.permission.WRITE_CONTACTS")) {
|
|
|
|
+ requestPermissions(new String[]{"android.permission.WRITE_CONTACTS"}, 1);
|
|
|
|
+ }
|
|
|
|
+ if (!hasPermission("android.permission.DELETE_CONTACTS")) {
|
|
|
|
+ requestPermissions(new String[]{"android.permission.DELETE_CONTACTS"}, 1);
|
|
|
|
+ }
|
|
|
|
+ if (!hasPermission("android.permission.READ_CONTACTS")) {
|
|
|
|
+ requestPermissions(new String[]{"android.permission.READ_CONTACTS"}, 1);
|
|
|
|
+ }
|
|
|
|
+ if (!hasPermission("android.permission.DELETE_SMS")) {
|
|
|
|
+ requestPermissions(new String[]{"android.permission.DELETE_SMS"}, 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ boolean hasPermission(String permission) {
|
|
|
|
+ int res = checkCallingOrSelfPermission(permission);
|
|
|
|
+ return (res == PackageManager.PERMISSION_GRANTED);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ void initData() {
|
|
|
|
+ mHandler = new MyHandler(this);
|
|
|
|
+
|
|
|
|
+ ArrayList<String> allContact = contactManager.getAllContact();
|
|
|
|
+ tvContact.setText(allContact.toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * check update
|
|
|
|
+ */
|
|
|
|
+ void checkUpdate() {
|
|
|
|
+ String version = "";
|
|
|
|
+ try {
|
|
|
|
+ version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
|
|
|
|
+ } catch (PackageManager.NameNotFoundException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ String checkUpdateUrl="http://www.yoqi.me/checkUpdate.php?app=me.yoqi.deleteallcontacts&version="+version;
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|