首页 > 代码库 > android GB2/GB3版本预置联系人且不可编辑或删除
android GB2/GB3版本预置联系人且不可编辑或删除
一、针对GB2、GB3等版本,预置联系人可以 中Part one这部分即可。
例如:在AbstractStartSIMService类的开始增加的定义如下:
public static final int INSERT_NUMBER_COUNT = 4; //定义要预置的联系人的个数
public static final String defaultNumbers[] = {"xxxx", "xxxx", "xxxx" , "xxxx"}; //定义要预置的联系人的号码
public static final String defaultNames[] = {"xxxxx", "xxxxx", "xxxxx", "xxxxx"}; //定义要预置的联系人的姓名
二、预置后不希望对这些联系人进行删除或编辑操作。
1、请参考如下方式修改ContactsListActivity.java文件:
(1) onCreateContextMenu()方法中
if(addEditAndDelete && simHasImported){
menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact);
menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact);
}
这部分之前添加如下部分,设置编辑和删除选项不可见:
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(header.equals(AbstractStartSIMService.defaultNames[i])){
addEditAndDelete = false;
}
}
(2) onListItemClick()方法中
if((isHeader && id == 0) || (null != PhoneOwner.getInstance() && id == PhoneOwner.getInstance().getOwnerID())) {
......................
}else{
if(mSearchMode && mAdapter.isSearchAllContactsItemPosition(position)){
doSearch();
}else if(mMode == MODE_INSERT_OR_EDIT_CONTACT || mMode == MODE_QUERY_PICK_TO_EDIT){
................
}else if((mMode & MODE_MASK_CREATE_NEW) == MODE_MASK_CREATE_NEW && position == 0 ){
................
}else if(mMode == MODE_JOIN_CONTACT && id == JOIN_MODE_SHOW_ALL_CONTACTS_ID){
................
}else if(id > 0){
................
if(mMode == MODE_DEFAULT && mDeleteState){
try{
...............
CheckBox cb = rootLayout.getCheckBox();
contactId = cursor.getInt(cursor.getColumnIndexOrThrow(Contacts.ID));
//(2)步骤的第一处修改开始处
String displayName = cursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
boolean isDefaultContact = false;
Log.d(TAG, "mtk----displayName: " + displayName);
if(!TextUtils.isEmpty(displayName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(header.equals(AbstractStartSIMService.defaultNames[i])){
isDefaultContact = true;
break;
}
}
}
//(2)步骤的第一处修改结束处
...................
if(cbPrevState){
cb.setChecked(false);
//(2)步骤的第二处修改开始处
if(isDefaultContact){
cb.setVisibility(View.INVISIBLE);
}
//(2)步骤的第二处修改结束处
...............
}else{
//(2)步骤的第三处修改开始处
//cb.setChecked(true);
//indicate = cursor.getInt(cursor.getColumnIndexOrThrow(RawContacts.INDICATE_PHONE_SIM));
//mToBeDeletedContacts.put(contactId, indicate);
//index in SIM
//long indexInSim = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
//mToBeDeletedContactsIndexInSim.put(contactId, indexInSim);
if(!isDefaultContact){
cb.setChecked(true);
cb.setVisibility(View.VISIBLE);
indicate = cursor.getInt(cursor.getColumnIndexOrThrow(RawContacts.INDICATE_PHONE_SIM));
mToBeDeletedContacts.put(contactId, indicate);
//index in SIM
long indexInSim = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
mToBeDeletedContactsIndexInSim.put(contactId, indexInSim);
}else{
cb.setChecked(false);
cb.setVisibility(View.INVISIBLE);
}
//(2)步骤的第三处修改结束处
........................
if(mSelected == mAllContacts.size()){
mSelectAllToDel = true;
//(2)步骤的第四处修改开始处
//mSelectAllBox.setChecked(mSelectAllToDel);
if(!isDefaultContact){
mSelectAllBox.setChecked(mSelectAllToDel);
}else{
mSelectAllBox.setChecked(false);
mSelectAllBox.setVisibility(View.INVISIBLE);
}
//(2)步骤的第四处修改结束处
mSelectAllButton.setText(mUnSelectText);
}
} //end else
.........................
} //end try
..................
}
...........
}
............
}
........................
(3) onQueryComplete()方法中
//mtk80909 start
if(activity.mDeleteState /*&& ! activity.mAllOrNone*/){
................
if(tmpCursor.moveToFirst()){
do{
..................
long indexInSim = tmpCursor.getInt(tmpCursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
//(3)步骤的修改开始处
//mAllContacts.put(currentID, indicate);
//mAllContactsIndexInSim.put(currentID, indexInSim); // index
// in
// sim
String displayName = tmpCursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
boolean isDefaultContact = false;
Log.d(TAG, "mtk----displayName: " + displayName);
if(!TextUtils.isEmpty(displayName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(displayName.equals(AbstractStartSIMService.defaultNames[i])){
isDefaultContact = true;
}
}
}
if( ! isDefaultContact ){
mAllContacts.put(currentID, indicate);
mAllContactsIndexInSim.put(currentID, indexInSim); // index in sim
}
//(3)步骤的修改结束处
}while(tmpCursor.moveToNext());
................
}
}
(4) bindView()方法中
if(mMode == MODE_DEFAULT && mDeleteState){ // [mtk80909]
long currentID = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
//(4)步骤第一处修改的开始处
String displayName = cursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
Log.d(TAG, "displayName: " + displayName);
//(4)步骤第一处修改的结束处
CheckBox cb = view.getCheckBox();
if(cb != null){
cb.setVisibility(View.VISIBLE);
//(4)步骤第二处修改的开始处
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(displayName.equals(AbstractStartSIMService.defaultNames[i])){
cb.setVisibility(View.INVISIBLE);
break;
}
}
//(4)步骤第二处修改的结束处
.................
}
}
2、按以下方式修改ViewContactActivity.java文件
(1) 在onCreateContextMenu()方法中,去掉这几个号码的删除选项
将 menu.add(0, MENU_ITEM_REMOVE_NUMBER, 0, R.string.menu_remove_number); 语句修改为:
int count = 0;
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(entry.data.equals(AbstractStartSIMService.defaultNumbers[i])){
count++;
}
}
if(count == 0){
menu.add(0, MENU_ITEM_REMOVE_NUMBER, 0, R.string.menu_remove_number);
}
(2) 在onPrepareOptionsMenu()方法中,去掉icon_menu中的编辑和删除联系人选项
menu.findItem(R.id.menu_edit).setVisible(addEditAndDelete);
menu.findItem(R.id.menu_delete).setVisible(addEditAndDelete); 语句的 前面 添加如下语句:
if(hasRawContact){
for (Entity contactEntity : mEntities) {
for (NamedContentValues subValue : contactEntity.getSubValues()) {
final ContentValues entryValues = subValue.values;
final String mimeType = entryValues.getAsString(Data.MIMETYPE);
final String data = http://www.mamicode.com/entryValues.getAsString(Data.DATA1);
int count = 0;
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(data.equals(AbstractStartSIMService.defaultNumbers[i]) || data.equals(AbstractStartSIMService.defaultNames[i])){
count++;
}
}
if(count != 0){
addEditAndDelete = false;
}
}
}
}
3、为了防止删除或编辑时误判,修改EditContactActivity.java和EditSimContactActivity.java文件,不让用户插入与预置号码的name、number一致的联系人
(1) 修改EditContactActivity.java文件
a. 在该文件的开头,导入如下包
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import com.android.contacts.AbstractStartSIMService;
import android.widget.Toast;
b. 在该类的开始定义两个全局变量
boolean sameNumberExist = false;
boolean sameNameExist = false;
c. 在onBackPressed()方法中
将 doSaveAction(SAVE_MODE_DEFAULT); 语句修改成:
doSaveWithCheckField();
if(sameNumberExist || sameNameExist){
finish();
}
d. 在 doSaveWithCheckField()方法中
ArrayList<ValuesDelta> list = null;
//第一处修改的开始
ArrayList<ValuesDelta> listName = null;
//第一处修改的结束
if(mState != null && mState.size() > 0){
.............
//第二处修改的开始
listName = mState.get(0).getMimeEntries(StructuredName.CONTENT_ITEM_TYPE);
//第二处修改的结束
}
String num;
//第三处修改的开始
/* if(list != null){
for(ValuesDelta delta : list){...............}
} */
String name1 = null;
String name2 = null;
String name = null;
sameNumberExist = false;
sameNameExist = false;
if(list != null){
for(ValuesDelta delta : list){
num = delta.getAsString(Data.DATA1);
if("".equals(num)){
sameNumberExist = false;
}
if(!TextUtils.isEmpty(num)){
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(num.equals(AbstractStartSIMService.defaultNumbers[i])){
sameNumberExist = true;
break;
}
}
if(!sameNumberExist ){
numberList.add(num);
}
}
}
}
if(listName != null && !sameNumberExist ){
for(ValuesDelta delta : listName){
name1 = delta.getAsString(Data.DATA2);
name2 = delta.getAsString(Data.DATA3);
if(name2 == null && name1 != null){
name = name1;
}else if(name2 != null && name1 == null){
name = name2;
}else if(name2 != null && name1 != null){
name = name2 + " " + name1;
}
if("".equals(name)){
sameNameExist = false;
}
if(!TextUtils.isEmpty(name)){
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(name.equals(AbstractStartSIMService.defaultNames[i])){
sameNameExist = true;
break;
}
}
if(!sameNumberExist ){
numberList.add(num);
}
}
}
}
//第三处修改的结束
bundle.putCharSequenceArrayList(KEY_PHONE_NUMBER, numberList);
boolean hasCheckDuplicatePlugin = .........................
//第四处修改的开始
/* if( !hasCheckDuplicatePlugin ){
doSaveAction(SAVE_MODE_DEFAULT);
} */
if(!hasCheckDuplicatePlugin && !sameNumberExist && !sameNameExist){
doSaveAction(SAVE_MODE_DEFAULT);
}else{
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
}
//第四处修改的结束
(2) 修改EditSimContactActivity.java文件
a. 在该文件的开头导入如下包
import com.android.contacts.AbstractStartSIMService;
import android.widget.Toast;
b. 在该类的开始定义两个全局变量
boolean hasSameNumber = false;
boolean hasSameName = false;
c. 在该文件中添加方法hasSameNameOrNumberAsDefaultContacts()
private void hasSameNameOrNumberAsDefaultContacts(){
hasSameNumber = false;
hasSameName = false;
updateName = edit_contact_name.getText().toString();
updatephone = edit_phone_numbers.getText().toString();
if("".equals(updatephone)){
hasSameNumber = false;
}
if("".equals(updateName)){
hasSameName = false;
}
if(!TextUtils.isEmpty(updatephone)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(updatephone.equals(AbstractStartSIMService.defaultNumbers[i])){
hasSameNumber = true;
break;
}
}
}
if(!TextUtils.isEmpty(updateName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(updateName.equals(AbstractStartSIMService.defaultNames[i])){
hasSameName = true;
break;
}
}
}
}
d. 在onClick()方法中
case R.id.btn_sim1_done: {
btn1.setClickable(false);
//修改的地方开始处
// doSaveAction(mMode);
hasSameNameOrNumberAsDefaultContacts();
if(!hasSameNumber && !hasSameName){
doSaveAction(mMode);
}else{
btn1.setClickable(true);
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
}
//修改的地方结束处
break;
}
e. onBackPressed()方法中
doSaveAction(mMode); 语句 修改成
hasSameNameOrNumberAsDefaultContacts();
if(!hasSameNumber && !hasSameName){
doSaveAction(mMode);
}else{
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
finish();
}
例如:在AbstractStartSIMService类的开始增加的定义如下:
public static final int INSERT_NUMBER_COUNT = 4; //定义要预置的联系人的个数
public static final String defaultNumbers[] = {"xxxx", "xxxx", "xxxx" , "xxxx"}; //定义要预置的联系人的号码
public static final String defaultNames[] = {"xxxxx", "xxxxx", "xxxxx", "xxxxx"}; //定义要预置的联系人的姓名
二、预置后不希望对这些联系人进行删除或编辑操作。
1、请参考如下方式修改ContactsListActivity.java文件:
(1) onCreateContextMenu()方法中
if(addEditAndDelete && simHasImported){
menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_editContact);
menu.add(0, MENU_ITEM_DELETE, 0, R.string.menu_deleteContact);
}
这部分之前添加如下部分,设置编辑和删除选项不可见:
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(header.equals(AbstractStartSIMService.defaultNames[i])){
addEditAndDelete = false;
}
}
(2) onListItemClick()方法中
if((isHeader && id == 0) || (null != PhoneOwner.getInstance() && id == PhoneOwner.getInstance().getOwnerID())) {
......................
}else{
if(mSearchMode && mAdapter.isSearchAllContactsItemPosition(position)){
doSearch();
}else if(mMode == MODE_INSERT_OR_EDIT_CONTACT || mMode == MODE_QUERY_PICK_TO_EDIT){
................
}else if((mMode & MODE_MASK_CREATE_NEW) == MODE_MASK_CREATE_NEW && position == 0 ){
................
}else if(mMode == MODE_JOIN_CONTACT && id == JOIN_MODE_SHOW_ALL_CONTACTS_ID){
................
}else if(id > 0){
................
if(mMode == MODE_DEFAULT && mDeleteState){
try{
...............
CheckBox cb = rootLayout.getCheckBox();
contactId = cursor.getInt(cursor.getColumnIndexOrThrow(Contacts.ID));
//(2)步骤的第一处修改开始处
String displayName = cursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
boolean isDefaultContact = false;
Log.d(TAG, "mtk----displayName: " + displayName);
if(!TextUtils.isEmpty(displayName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(header.equals(AbstractStartSIMService.defaultNames[i])){
isDefaultContact = true;
break;
}
}
}
//(2)步骤的第一处修改结束处
...................
if(cbPrevState){
cb.setChecked(false);
//(2)步骤的第二处修改开始处
if(isDefaultContact){
cb.setVisibility(View.INVISIBLE);
}
//(2)步骤的第二处修改结束处
...............
}else{
//(2)步骤的第三处修改开始处
//cb.setChecked(true);
//indicate = cursor.getInt(cursor.getColumnIndexOrThrow(RawContacts.INDICATE_PHONE_SIM));
//mToBeDeletedContacts.put(contactId, indicate);
//index in SIM
//long indexInSim = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
//mToBeDeletedContactsIndexInSim.put(contactId, indexInSim);
if(!isDefaultContact){
cb.setChecked(true);
cb.setVisibility(View.VISIBLE);
indicate = cursor.getInt(cursor.getColumnIndexOrThrow(RawContacts.INDICATE_PHONE_SIM));
mToBeDeletedContacts.put(contactId, indicate);
//index in SIM
long indexInSim = cursor.getLong(cursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
mToBeDeletedContactsIndexInSim.put(contactId, indexInSim);
}else{
cb.setChecked(false);
cb.setVisibility(View.INVISIBLE);
}
//(2)步骤的第三处修改结束处
........................
if(mSelected == mAllContacts.size()){
mSelectAllToDel = true;
//(2)步骤的第四处修改开始处
//mSelectAllBox.setChecked(mSelectAllToDel);
if(!isDefaultContact){
mSelectAllBox.setChecked(mSelectAllToDel);
}else{
mSelectAllBox.setChecked(false);
mSelectAllBox.setVisibility(View.INVISIBLE);
}
//(2)步骤的第四处修改结束处
mSelectAllButton.setText(mUnSelectText);
}
} //end else
.........................
} //end try
..................
}
...........
}
............
}
........................
(3) onQueryComplete()方法中
//mtk80909 start
if(activity.mDeleteState /*&& ! activity.mAllOrNone*/){
................
if(tmpCursor.moveToFirst()){
do{
..................
long indexInSim = tmpCursor.getInt(tmpCursor.getColumnIndexOrThrow(Contacts.INDEX_IN_SIM));
//(3)步骤的修改开始处
//mAllContacts.put(currentID, indicate);
//mAllContactsIndexInSim.put(currentID, indexInSim); // index
// in
// sim
String displayName = tmpCursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
boolean isDefaultContact = false;
Log.d(TAG, "mtk----displayName: " + displayName);
if(!TextUtils.isEmpty(displayName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(displayName.equals(AbstractStartSIMService.defaultNames[i])){
isDefaultContact = true;
}
}
}
if( ! isDefaultContact ){
mAllContacts.put(currentID, indicate);
mAllContactsIndexInSim.put(currentID, indexInSim); // index in sim
}
//(3)步骤的修改结束处
}while(tmpCursor.moveToNext());
................
}
}
(4) bindView()方法中
if(mMode == MODE_DEFAULT && mDeleteState){ // [mtk80909]
long currentID = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
//(4)步骤第一处修改的开始处
String displayName = cursor.getString(SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX);
Log.d(TAG, "displayName: " + displayName);
//(4)步骤第一处修改的结束处
CheckBox cb = view.getCheckBox();
if(cb != null){
cb.setVisibility(View.VISIBLE);
//(4)步骤第二处修改的开始处
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(displayName.equals(AbstractStartSIMService.defaultNames[i])){
cb.setVisibility(View.INVISIBLE);
break;
}
}
//(4)步骤第二处修改的结束处
.................
}
}
2、按以下方式修改ViewContactActivity.java文件
(1) 在onCreateContextMenu()方法中,去掉这几个号码的删除选项
将 menu.add(0, MENU_ITEM_REMOVE_NUMBER, 0, R.string.menu_remove_number); 语句修改为:
int count = 0;
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(entry.data.equals(AbstractStartSIMService.defaultNumbers[i])){
count++;
}
}
if(count == 0){
menu.add(0, MENU_ITEM_REMOVE_NUMBER, 0, R.string.menu_remove_number);
}
(2) 在onPrepareOptionsMenu()方法中,去掉icon_menu中的编辑和删除联系人选项
menu.findItem(R.id.menu_edit).setVisible(addEditAndDelete);
menu.findItem(R.id.menu_delete).setVisible(addEditAndDelete); 语句的 前面 添加如下语句:
if(hasRawContact){
for (Entity contactEntity : mEntities) {
for (NamedContentValues subValue : contactEntity.getSubValues()) {
final ContentValues entryValues = subValue.values;
final String mimeType = entryValues.getAsString(Data.MIMETYPE);
final String data = http://www.mamicode.com/entryValues.getAsString(Data.DATA1);
int count = 0;
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(data.equals(AbstractStartSIMService.defaultNumbers[i]) || data.equals(AbstractStartSIMService.defaultNames[i])){
count++;
}
}
if(count != 0){
addEditAndDelete = false;
}
}
}
}
3、为了防止删除或编辑时误判,修改EditContactActivity.java和EditSimContactActivity.java文件,不让用户插入与预置号码的name、number一致的联系人
(1) 修改EditContactActivity.java文件
a. 在该文件的开头,导入如下包
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import com.android.contacts.AbstractStartSIMService;
import android.widget.Toast;
b. 在该类的开始定义两个全局变量
boolean sameNumberExist = false;
boolean sameNameExist = false;
c. 在onBackPressed()方法中
将 doSaveAction(SAVE_MODE_DEFAULT); 语句修改成:
doSaveWithCheckField();
if(sameNumberExist || sameNameExist){
finish();
}
d. 在 doSaveWithCheckField()方法中
ArrayList<ValuesDelta> list = null;
//第一处修改的开始
ArrayList<ValuesDelta> listName = null;
//第一处修改的结束
if(mState != null && mState.size() > 0){
.............
//第二处修改的开始
listName = mState.get(0).getMimeEntries(StructuredName.CONTENT_ITEM_TYPE);
//第二处修改的结束
}
String num;
//第三处修改的开始
/* if(list != null){
for(ValuesDelta delta : list){...............}
} */
String name1 = null;
String name2 = null;
String name = null;
sameNumberExist = false;
sameNameExist = false;
if(list != null){
for(ValuesDelta delta : list){
num = delta.getAsString(Data.DATA1);
if("".equals(num)){
sameNumberExist = false;
}
if(!TextUtils.isEmpty(num)){
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(num.equals(AbstractStartSIMService.defaultNumbers[i])){
sameNumberExist = true;
break;
}
}
if(!sameNumberExist ){
numberList.add(num);
}
}
}
}
if(listName != null && !sameNumberExist ){
for(ValuesDelta delta : listName){
name1 = delta.getAsString(Data.DATA2);
name2 = delta.getAsString(Data.DATA3);
if(name2 == null && name1 != null){
name = name1;
}else if(name2 != null && name1 == null){
name = name2;
}else if(name2 != null && name1 != null){
name = name2 + " " + name1;
}
if("".equals(name)){
sameNameExist = false;
}
if(!TextUtils.isEmpty(name)){
for(int i = 0; i< AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(name.equals(AbstractStartSIMService.defaultNames[i])){
sameNameExist = true;
break;
}
}
if(!sameNumberExist ){
numberList.add(num);
}
}
}
}
//第三处修改的结束
bundle.putCharSequenceArrayList(KEY_PHONE_NUMBER, numberList);
boolean hasCheckDuplicatePlugin = .........................
//第四处修改的开始
/* if( !hasCheckDuplicatePlugin ){
doSaveAction(SAVE_MODE_DEFAULT);
} */
if(!hasCheckDuplicatePlugin && !sameNumberExist && !sameNameExist){
doSaveAction(SAVE_MODE_DEFAULT);
}else{
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
}
//第四处修改的结束
(2) 修改EditSimContactActivity.java文件
a. 在该文件的开头导入如下包
import com.android.contacts.AbstractStartSIMService;
import android.widget.Toast;
b. 在该类的开始定义两个全局变量
boolean hasSameNumber = false;
boolean hasSameName = false;
c. 在该文件中添加方法hasSameNameOrNumberAsDefaultContacts()
private void hasSameNameOrNumberAsDefaultContacts(){
hasSameNumber = false;
hasSameName = false;
updateName = edit_contact_name.getText().toString();
updatephone = edit_phone_numbers.getText().toString();
if("".equals(updatephone)){
hasSameNumber = false;
}
if("".equals(updateName)){
hasSameName = false;
}
if(!TextUtils.isEmpty(updatephone)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(updatephone.equals(AbstractStartSIMService.defaultNumbers[i])){
hasSameNumber = true;
break;
}
}
}
if(!TextUtils.isEmpty(updateName)){
for(int i = 0; i < AbstractStartSIMService.INSERT_NUMBER_COUNT; i++){
if(updateName.equals(AbstractStartSIMService.defaultNames[i])){
hasSameName = true;
break;
}
}
}
}
d. 在onClick()方法中
case R.id.btn_sim1_done: {
btn1.setClickable(false);
//修改的地方开始处
// doSaveAction(mMode);
hasSameNameOrNumberAsDefaultContacts();
if(!hasSameNumber && !hasSameName){
doSaveAction(mMode);
}else{
btn1.setClickable(true);
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
}
//修改的地方结束处
break;
}
e. onBackPressed()方法中
doSaveAction(mMode); 语句 修改成
hasSameNameOrNumberAsDefaultContacts();
if(!hasSameNumber && !hasSameName){
doSaveAction(mMode);
}else{
Toast.makeText(this, "Can not save a number or name the same as the default contact!", Toast.LENGTH_LONG).show();
finish();
}
android GB2/GB3版本预置联系人且不可编辑或删除
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。