Advertisement
H4T3D

Get All Contacts and save in Test file android sdk

Mar 10th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.62 KB | None | 0 0
  1. package com.example.contactsharvester;
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FileOutputStream;
  6.  
  7. import android.os.Bundle;
  8. import android.os.Environment;
  9. import android.provider.ContactsContract;
  10. import android.app.Activity;
  11. import android.content.ContentResolver;
  12. import android.database.Cursor;
  13. import android.view.Menu;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.EditText;
  17. import android.widget.TextView;
  18.  
  19. public class MainActivity extends Activity implements OnClickListener{
  20.     String phoneNo;
  21.     String phoneNos="\n Phone : ";;
  22.     String name;
  23.     String names="\n Name : ";
  24.     String id;
  25.    
  26.    
  27.     TextView tv = (TextView) findViewById(R.id.textView1);
  28.     EditText ed = (EditText) findViewById(R.id.editText1);
  29.    
  30.    
  31. public void datasaver(String a , String b) {
  32.     // write on SD card file data in the text box
  33.     try {
  34.         File newFolder = new File(Environment.getExternalStorageDirectory(), "Contact Harvester");
  35.         if (!newFolder.exists()) {
  36.             newFolder.mkdir();
  37.         }
  38.         try {
  39.             File file = new File(newFolder, b + ".txt");
  40.             file.createNewFile();
  41.             FileOutputStream fos;
  42.             fos = new FileOutputStream(file);
  43.             byte[] data = a.getBytes();
  44.             fos.write(data);
  45.             fos.flush();
  46.             fos.close();
  47.         } catch (Exception ex) {
  48.             System.out.println("ex: " + ex);
  49.         }
  50.     } catch (Exception e) {
  51.         System.out.println("e: " + e);
  52.     }
  53.    
  54. }
  55.    
  56.     @Override
  57.     protected void onCreate(Bundle savedInstanceState) {
  58.         super.onCreate(savedInstanceState);
  59.         setContentView(R.layout.activity_main);
  60.      
  61.         ContentResolver cr = getContentResolver();
  62.         Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
  63.                 null, null, null, null);
  64.         if (cur.getCount() > 0) {
  65.             while (cur.moveToNext()) {
  66.                    id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
  67.                   name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
  68.                   names = names+"\n"+name;
  69.                   if (Integer.parseInt(cur.getString(
  70.                         cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
  71.                      Cursor pCur = cr.query(
  72.                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  73.                                null,
  74.                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
  75.                                new String[]{id}, null);
  76.                      while (pCur.moveToNext()) {
  77.                          phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  78.                         // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
  79.                     }
  80.                      
  81.                      phoneNos = phoneNos+"\n"+phoneNo;
  82.                      
  83.                     pCur.close();
  84.                 }
  85.             }
  86.            
  87.             tv.setText(names+"\n"+phoneNos);
  88.         }
  89.        
  90.     }
  91.  
  92.  
  93.     @Override
  94.     public boolean onCreateOptionsMenu(Menu menu) {
  95.         // Inflate the menu; this adds items to the action bar if it is present.
  96.         getMenuInflater().inflate(R.menu.main, menu);
  97.         return true;
  98.     }
  99.  
  100.     @Override
  101.     public void onClick(View v) {
  102.         // TODO Auto-generated method stub
  103.         switch (v.getId()) {
  104.         case R.id.button1:
  105.             datasaver(tv.getText().toString(),ed.getText().toString());
  106.             break;
  107.  
  108.         default:
  109.             break;
  110.         }
  111.        
  112.     }
  113.    
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement