<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zhoukaol1019">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name=".ContentProvider.MyContentProvider2"
android:authorities="com.bawei"
android:enabled="true"
android:exported="true"></provider>
</application>
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>
package com.example.zhoukaol1019.ContentProvider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import com.example.zhoukaol1019.Sql.SqliteHelp;
public class MyContentProvider2 extends ContentProvider {
private SQLiteDatabase db;
public MyContentProvider2() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return db.delete("person",selection,selectionArgs);
}
@Override
public String getType(Uri uri) {
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
db.insert("person", null, values);
return uri;
}
@Override
public boolean onCreate() {
SqliteHelp sqliteHelp = new SqliteHelp(getContext());
db = sqliteHelp.getReadableDatabase();
if (db==null){
return false;
}
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
return db.query("person",projection,selection,selectionArgs,null,null,sortOrder);
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return db.update("person",values,selection,selectionArgs);
}
}
package com.example.zhoukaol1019;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.example.zhoukaol1019.Sql.SqliteHelp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button lxrBt;
private Uri uri;
private ContentResolver contentResolver;
private SqliteHelp sqliteHelp;
private SQLiteDatabase db;
private List<String> list = new ArrayList<>();
private List<String> listlite = new ArrayList<>();
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqliteHelp = new SqliteHelp(this);
db = sqliteHelp.getReadableDatabase();
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS},110);
lxrBt = (Button) findViewById(R.id.lxr_bt);
lxrBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uri = Uri.parse(String.valueOf(ContactsContract.CommonDataKinds.Phone.CONTENT_URI));
contentResolver = getContentResolver();
Cursor query = contentResolver.query(uri, null, null, null, null);
if (query!=null){
while(query.moveToNext()){
String name = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number = query.getString(query.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
ContentValues contentValues = new ContentValues();
contentValues.put("name",name);
contentValues.put("phone",number);
list.add(name);
Cursor person = db.query("person", null, null, null, null, null, null);
if (person!=null){
while(person.moveToNext()){
String name1 = person.getString(person.getColumnIndex("name"));
listlite.add(name1);
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < listlite.size(); j++) {
if (list.get(i).equals(listlite.get(j))){
}else{
db.insert("person",null,contentValues);
}
}
}
}
}
}
});
}
}
package com.example.zhoukaol1019.Sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SqliteHelp extends SQLiteOpenHelper {
public SqliteHelp(@Nullable Context context) {
super(context,"Duo.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
SimpleDateFormat datesp = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
String str = datesp.format(date);
sqLiteDatabase.execSQL("create table person(id varchar(20),name varchar(20),phone varchar(20),str varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/lxr_bt"
android:text="导入联系人"
/>
</LinearLayout>
package com.example.app2.fragment;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.example.app2.R;
import java.util.ArrayList;
import java.util.List;
public class LianXiRenFragment extends Fragment {
private ListView listView;
private ContentResolver contentResolver;
private List<String> list = new ArrayList<>();
private ImageView imageView;
private Button button;
public LianXiRenFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View inflate = inflater.inflate(R.layout.fragment_lian_xi_ren, container, false);
listView = inflate.findViewById(R.id.lv);
FragmentActivity activity = getActivity();
contentResolver = activity.getContentResolver();
final Uri uri = Uri.parse("com.bawei");
Cursor query = contentResolver.query(uri, null, null, null, null);
if (query!=null){
while(query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
String address = query.getString(query.getColumnIndex("address"));
list.add(name);
list.add(address);
LianXiRenFragment lianXiRenFragment = new LianXiRenFragment();
ZhuyeFragment zhuyeFragment = new ZhuyeFragment();
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.show(lianXiRenFragment);
fragmentTransaction.hide(zhuyeFragment);
}
}
listView.setAdapter(new BaseAdapter() {
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
if (view!=null){
viewHolder = new ViewHolder();
view = layoutInflater.inflate(R.layout.lxritem,null);
viewHolder.lxrTvone = view.findViewById(R.id.lxr_tvone);
viewHolder.lxrTvtwo = view.findViewById(R.id.lxr_tvtwo);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.lxrTvone.setText(list.get(i));
viewHolder.lxrTvtwo.setText(list.get(i));
return view;
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> adapterView, View view, final int i, long l) {
PopupWindow popupWindow = new PopupWindow(getActivity());
View inflate1 = LayoutInflater.from(getActivity()).inflate(R.layout.popwindowitem, null);
imageView = inflate1.findViewById(R.id.popiv);
button = inflate1.findViewById(R.id.popsc);
popupWindow.setContentView(inflate1);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setWidth(600);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(listView,0,0);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
list.remove(this);
listView.deferNotifyDataSetChanged();
ContentResolver contentResolver = getActivity().getContentResolver();
Uri uri1 = Uri.parse("com.bawei");
ContentValues contentValues = new ContentValues();
contentValues.put("name",list.get(i));
contentValues.put("address",list.get(i));
contentResolver.delete(uri,"id=?",new String[]{"xiaoming"});
Toast.makeText(getActivity(), "删除成功", Toast.LENGTH_SHORT).show();
}
});
return true;
}
});
return inflate;
}
private class ViewHolder {
private TextView lxrTvone;
private TextView lxrTvtwo;
}
}
package com.example.app2.fragment;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.example.app2.R;
public class ZhuyeFragment extends Fragment {
private Button button;
private ContentResolver contentResolver;
public ZhuyeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_zhuye, container, false);
button = inflate.findViewById(R.id.zhuye_bt);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentActivity activity = getActivity();
contentResolver = activity.getContentResolver();
Uri uri = Uri.parse("com.bawei");
Cursor query = contentResolver.query(uri, null, null, null, null);
if (query!=null){
while(query.moveToNext()){
String name = query.getString(query.getColumnIndex("name"));
String address = query.getString(query.getColumnIndex("address"));
LianXiRenFragment lianXiRenFragment = new LianXiRenFragment();
ZhuyeFragment zhuyeFragment = new ZhuyeFragment();
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.show(lianXiRenFragment);
fragmentTransaction.hide(zhuyeFragment);
}
}
}
});
return inflate;
}
}
package com.example.app2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import com.example.app2.fragment.LianXiRenFragment;
import com.example.app2.fragment.ZhuyeFragment;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Fragment> list = new ArrayList<>();
private LinearLayout ll;
private RadioGroup rg;
private RadioButton bt1;
private RadioButton bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.ll);
rg = (RadioGroup) findViewById(R.id.rg);
bt1 = (RadioButton) findViewById(R.id.bt1);
bt2 = (RadioButton) findViewById(R.id.bt2);
if (Build.VERSION.SDK_INT>Build.VERSION_CODES.M){
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS,Manifest.permission.WRITE_CONTACTS},110);
}
final ZhuyeFragment zhuyeFragment = new ZhuyeFragment();
final LianXiRenFragment lianXiRenFragment = new LianXiRenFragment();
list.add(zhuyeFragment);
list.add(lianXiRenFragment);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction ft = supportFragmentManager.beginTransaction();
ft.add(R.id.ll,zhuyeFragment);
ft.add(R.id.ll,lianXiRenFragment);
ft.commit();
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction ft = supportFragmentManager.beginTransaction();
switch (i){
case R.id.bt1:
ft.show(zhuyeFragment);
ft.hide(lianXiRenFragment);
break;
case R.id.bt2:
ft.show(lianXiRenFragment);
ft.hide(zhuyeFragment);
break;
}
ft.commit();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:orientation="vertical"
android:id="@+id/ll">
</LinearLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:id="@+id/rg"
android:layout_weight="1">
<RadioButton
android:id="@+id/bt1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="主页"
android:button="@null"
android:gravity="center"
/>
<RadioButton
android:id="@+id/bt2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:text="联系人"
android:gravity="center"
/>
</RadioGroup>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment.LianXiRenFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/popiv"
/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/popsc"
android:text="删除"
/>
</LinearLayout>