Qt android获取通讯录,Android导入导出txt通讯录工具【附源码】

packagecom.zyc.contact.tool;

importjava.io.File;

importAndroid.app.Activity;

importandroid.app.AlertDialog;

importandroid.content.Context;

importandroid.content.DialogInterface;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.RadioButton;

importandroid.widget.TextView;

publicclassContactToolActivityextendsActivityimplementsOnClickListener {

privateEditText mEditText;

privateButton mHelpButton;

privateButton mInsertButton;

privateButton mOutputButton;

privateTextView mResultTextView;

privateTextView mOsTextView;

privateRadioButton[] mOsSetButtons =newRadioButton[2];

privateRadioButton[] mModeButtons =newRadioButton[2];

privateHandler mHandler =newHandler() {

@Override

publicvoidhandleMessage(Message msg) {

switch(msg.what) {

caseContactContant.INSERT_FAIL:

mResultTextView.setText(ContactContant.FAIL_INSERT);

endInsertContact();

break;

caseContactContant.INSERT_SUCCESS:

mResultTextView.setText(String.format(

ContactContant.SUCCESS_INSERT,

ContactToolInsertUtils.getSuccessCount(),

ContactToolInsertUtils.getFailCount()));

endInsertContact();

break;

caseContactContant.OUTPUT_FAIL:

mResultTextView.setText(ContactContant.FAIL_OUTPUT);

endOutputContact();

break;

caseContactContant.OUTPUT_SUCCESS:

mResultTextView.setText((String.format(

ContactContant.SUCCESS_OUTPUT,

ContactToolOutputUtils.getCount())));

endOutputContact();

break;

}

}

};

@Override

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

init();

}

/*init widgets*/

privatevoidinit() {

mEditText = (EditText) findViewById(R.id.edit_text);

mHelpButton = (Button) findViewById(R.id.help_button);

mHelpButton.setOnClickListener(this);

mInsertButton = (Button) findViewById(R.id.insert_button);

mInsertButton.setOnClickListener(this);

mOutputButton = (Button) findViewById(R.id.output_button);

mOutputButton.setOnClickListener(this);

mResultTextView = (TextView) findViewById(R.id.result_view);

mOsTextView = (TextView)findViewById(R.id.os_text);

mOsSetButtons[0] = (RadioButton) findViewById(R.id.radio_button_win);

// set gbk default

mOsSetButtons[0].setChecked(true);

mOsSetButtons[1] = (RadioButton) findViewById(R.id.radio_button_linux);

mModeButtons[0] = (RadioButton) findViewById(R.id.radio_insert);

mModeButtons[0].setOnClickListener(this);

mModeButtons[1] = (RadioButton) findViewById(R.id.radio_output);

mModeButtons[1].setOnClickListener(this);

setInsertWidgetEnabled(false);

setOutputWidgetEnabled(false);

}

@Override

publicvoidonResume() {

super.onResume();

}

@Override

publicvoidonClick(View v) {

switch(v.getId()) {

caseR.id.help_button:

createDialog(this,ContactContant.HELP_DIALOG_TITLE,ContactContant.HELP_MESSAGE,false,

ContactContant.DIALOG_TYPE_HELP);

break;

caseR.id.insert_button:

insertContact();

break;

caseR.id.output_button:

outputContact();

break;

caseR.id.radio_insert:

setOutputWidgetEnabled(false);

setInsertWidgetEnabled(true);

break;

caseR.id.radio_output:

setInsertWidgetEnabled(false);

setOutputWidgetEnabled(true);

break;

}

}

publicvoidcreateDialog(Context context, String title, String message,

booleanhasCancel,finalinttype) {

AlertDialog.Builder builder =newAlertDialog.Builder(context);

builder.setTitle(title);

builder.setMessage(message);

builder.setPositiveButton(ContactContant.DIALOG_OK,

newDialogInterface.OnClickListener() {

publicvoidonClick(DialogInterface dialog,intwhichButton) {

switch(type) {

caseContactContant.DIALOG_TYPE_HELP:

dialog.cancel();

break;

caseContactContant.DIALOG_TYPE_INSERT:

doInsertContact();

break;

caseContactContant.DIALOG_TYPE_OUTPUT:

doOutputContact();

break;

}

}

});

if(hasCancel) {

builder.setNeutralButton(ContactContant.DIALOG_CANCEL,

newDialogInterface.OnClickListener() {

publicvoidonClick(DialogInterface dialog,

intwhichButton) {

dialog.cancel();

}

});

}

builder.show();

}

privatevoidsetInsertWidgetEnabled(booleanenable) {

mOsSetButtons[0].setEnabled(enable);

mOsSetButtons[1].setEnabled(enable);

mInsertButton.setEnabled(enable);

mEditText.setEnabled(enable);

intvisable = enable ? View.VISIBLE : View.INVISIBLE;

mOsSetButtons[0].setVisibility(visable);

mOsSetButtons[1].setVisibility(visable);

mOsTextView.setVisibility(visable);

if(!enable){

mResultTextView.setText(ContactContant.NO_TEXT);

}

}

privatevoidinsertContact() {

String path = mEditText.getText().toString();

if(path ==null|| path.equals(ContactContant.NO_TEXT)) {

ContactToolUtils.showToast(this,

ContactContant.FAIL_EDITTEXT_NOT_INPUT);

mResultTextView.setText(ContactContant.FAIL_EDITTEXT_NOT_INPUT);

return;

}

path = ContactContant.FILE_NAME_PARENT + path;

if(!newFile(path).exists()) {

ContactToolUtils

.showToast(this, ContactContant.FAIL_FIRE_NOT_EXIST);

mResultTextView.setText(ContactContant.FAIL_FIRE_NOT_EXIST);

return;

}

if(mInsertThread !=null) {

mInsertThread.interrupt();

mInsertThread =null;

}

String charset = mOsSetButtons[0].isChecked() ? ContactContant.CHARSET_GBK

: ContactContant.CHARSET_UTF8;

mInsertThread =newThread(newInsertRunnable(this, path, charset));

createDialog(this, ContactContant.WARNDIALOG_TITLE,

ContactContant.INSERT_WARNDIALOG_MESSAGE,true,

ContactContant.DIALOG_TYPE_INSERT);

}

privatevoiddoInsertContact() {

setInsertWidgetEnabled(false);

mResultTextView.setText(ContactContant.STATUS_INSERTING);

if(mInsertThread !=null) {

mInsertThread.start();

}

}

privatevoidendInsertContact() {

mEditText.setText(ContactContant.NO_TEXT);

setInsertWidgetEnabled(true);

}

privateThread mInsertThread;

classInsertRunnableimplementsRunnable {

privateContext mContext;

privateString mPath;

privateString mCharset;

publicInsertRunnable(Context context, String path, String charset) {

mPath = path;

mContext = context;

mCharset = charset;

}

@Override

publicvoidrun() {

booleanresult = ContactToolInsertUtils.insertIntoContact(mContext,

mPath, mCharset);

if(result) {

mHandler.sendEmptyMessage(ContactContant.INSERT_SUCCESS);

}else{

mHandler.sendEmptyMessage(ContactContant.INSERT_FAIL);

}

}

}

privatevoidsetOutputWidgetEnabled(booleanenable) {

mOutputButton.setEnabled(enable);

if(!enable){

mResultTextView.setText(ContactContant.NO_TEXT);

}

}

privatevoidoutputContact(){

File file =newFile(ContactContant.OUTPUT_PATH);

if(file.exists()){

createDialog(this, ContactContant.WARNDIALOG_TITLE,

ContactContant.OUTPUT_WARNDIALOG_MESSAGE,true,

ContactContant.DIALOG_TYPE_OUTPUT);

}else{

doOutputContact();

}

}

privatevoiddoOutputContact(){

setOutputWidgetEnabled(false);

mResultTextView.setText(ContactContant.STATUS_OUTPUTING);

if(mOutputThread !=null) {

mOutputThread.interrupt();

mOutputThread =null;

}

mOutputThread =newThread(newOutputRunnable(this));

if(mOutputThread !=null) {

mOutputThread.start();

}

}

privateThread mOutputThread;

classOutputRunnableimplementsRunnable {

privateContext mContext;

publicOutputRunnable(Context context) {

mContext = context;

}

@Override

publicvoidrun() {

booleanresult = ContactToolOutputUtils.outputContacts(mContext);

if(result) {

mHandler.sendEmptyMessage(ContactContant.OUTPUT_SUCCESS);

}else{

mHandler.sendEmptyMessage(ContactContant.OUTPUT_FAIL);

}

}

}

privatevoidendOutputContact() {

setOutputWidgetEnabled(true);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值