2016-08-10 2 views
0

나는 안드로이드 개발에 새로운입니다. 나는 백업 연락처에 대한 안드로이드 애플 리케이션을 설계 할 계획입니다. 백업 연락처에 대한 진행률 표시 줄을 추가해야합니다 어떻게 내 코드는 여기에 가능합니다안드로이드에 백업 연락처에 대한 진행률 표시 줄을 추가하는 방법

public class MainActivity extends AppCompatActivity { 
Button backup,restore; 
String vfile; 
FileOutputStream mFileOutputStream = null; 
Cursor cursor; 
ArrayList<String> vCard ; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    backup=(Button)findViewById(R.id.btnbkp); 
    restore=(Button)findViewById(R.id.btnres); 

    vfile = "contacts.vcf"; 
    final String storage_path = Environment.getExternalStorageDirectory().toString() +"/"+ vfile; 
    final File f = new File(storage_path); 

    backup.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try { 
       if (!f.exists()) 
        f.createNewFile(); 
       mFileOutputStream = new FileOutputStream(storage_path, false); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      getVcardString(); 
     } 
    }); 
    restore.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      final Intent intent = new Intent(); 

      final MimeTypeMap mime = MimeTypeMap.getSingleton(); 
      String tmptype = mime.getMimeTypeFromExtension("vcf"); 
      final File file = new File(Environment.getExternalStorageDirectory().toString() + "/contacts.vcf"); 

      intent.setDataAndType(Uri.fromFile(file), tmptype); 
      startActivity(intent); 
     } 
    }); 

} 
private void getVcardString() 
{ 
    vCard = new ArrayList<String>(); 
    cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
    if(cursor!=null&&cursor.getCount()>0) 
    { 
     cursor.moveToFirst(); 
     for(int i =0;i<cursor.getCount();i++) 
     { 

      get(cursor); 
      Log.d("TAG", "Contact "+(i+1)+"VcF String is"+vCard.get(i)); 
      cursor.moveToNext(); 
     } 

    } 
    else 
    { 
     Log.d("TAG", "No Contacts in Your Phone"); 
    } 
    try 
    { 
     mFileOutputStream.close(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
public void get(Cursor cursor) 
{ 
    //cursor.moveToFirst(); 
    String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
    AssetFileDescriptor fd; 
    try 
    { 
     fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); 
     FileInputStream fis = fd.createInputStream(); 
     byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
     fis.read(buf); 
     String vcardstring= new String(buf); 
     vCard.add(vcardstring); 

     mFileOutputStream.write(vcardstring.toString().getBytes()); 

    } 
    catch (Exception e1) 
    { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

위 내 앱 코드, 나는 길고 배경 작업에 응용 프로그램

+0

당신은 ,,,은 불행하게도 정지 진행 표시 줄 –

답변

0

사용 AsyncTask에 백업 연락처를 진행 표시 줄을 추가 할 및 onPreExecute()에서의 ProgressBar를 보여 dismiss() 그 코드 아래 onPostExecute() 확인한다.

public class MainActivity extends AppCompatActivity { 
    Button backup, restore; 
    String vfile; 
    FileOutputStream mFileOutputStream = null; 
    Cursor cursor; 
    ArrayList<String> vCard; 
    File f; 
    String storage_path; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     backup = (Button) findViewById(R.id.btnbkp); 
     restore = (Button) findViewById(R.id.btnres); 

     vfile = "contacts.vcf"; 
     storage_path = Environment.getExternalStorageDirectory().toString() + "/" + vfile; 
     f = new File(storage_path); 

     backup.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       new LongOperation().execute(""); 

      } 
     }); 
     restore.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       final Intent intent = new Intent(); 

       final MimeTypeMap mime = MimeTypeMap.getSingleton(); 
       String tmptype = mime.getMimeTypeFromExtension("vcf"); 
       final File file = new File(Environment.getExternalStorageDirectory().toString() + "/contacts.vcf"); 

       intent.setDataAndType(Uri.fromFile(file), tmptype); 
       startActivity(intent); 
      } 
     }); 

    } 

    private class LongOperation extends AsyncTask<String, Void, String> { 

     ProgressDialog progress; 

     @Override 
     protected void onPreExecute() { 
      progress = new ProgressDialog(MainActivity.this); 
      progress.setMessage("Loading.... "); 
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      progress.setIndeterminate(true); 
      progress.setProgress(0); 
      progress.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      try { 
       if (!f.exists()) 
        f.createNewFile(); 
       mFileOutputStream = new FileOutputStream(storage_path, false); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      getVcardString(); 
      return "Executed"; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      progress.dismiss(); 
     } 

    } 

    private void getVcardString() { 
     vCard = new ArrayList<String>(); 
     cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
     if (cursor != null && cursor.getCount() > 0) { 
      cursor.moveToFirst(); 
      for (int i = 0; i < cursor.getCount(); i++) { 

       get(cursor); 
       Log.d("TAG", "Contact " + (i + 1) + "VcF String is" + vCard.get(i)); 
       cursor.moveToNext(); 
      } 

     } else { 
      Log.d("TAG", "No Contacts in Your Phone"); 
     } 
     try { 
      mFileOutputStream.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    public void get(Cursor cursor) { 
     //cursor.moveToFirst(); 
     String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
     Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey); 
     AssetFileDescriptor fd; 
     try { 
      fd = this.getContentResolver().openAssetFileDescriptor(uri, "r"); 
      FileInputStream fis = fd.createInputStream(); 
      byte[] buf = new byte[(int) fd.getDeclaredLength()]; 
      fis.read(buf); 
      String vcardstring = new String(buf); 
      vCard.add(vcardstring); 

      mFileOutputStream.write(vcardstring.toString().getBytes()); 

     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
    } 


} 
+0

을 보여 AsyncTask를 사용할 수 있습니다 이유 – sanjay

+0

@sanjay 당신이 teamviwer을해야합니까? –

+0

감사 ..its은 0/100 – sanjay

관련 문제