Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.contactsharvester;
- import java.io.File;
- import java.io.FileOutputStream;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.ContactsContract;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.database.Cursor;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.EditText;
- import android.widget.TextView;
- public class MainActivity extends Activity implements OnClickListener{
- String phoneNo;
- String phoneNos="\n Phone : ";;
- String name;
- String names="\n Name : ";
- String id;
- TextView tv = (TextView) findViewById(R.id.textView1);
- EditText ed = (EditText) findViewById(R.id.editText1);
- public void datasaver(String a , String b) {
- // write on SD card file data in the text box
- try {
- File newFolder = new File(Environment.getExternalStorageDirectory(), "Contact Harvester");
- if (!newFolder.exists()) {
- newFolder.mkdir();
- }
- try {
- File file = new File(newFolder, b + ".txt");
- file.createNewFile();
- FileOutputStream fos;
- fos = new FileOutputStream(file);
- byte[] data = a.getBytes();
- fos.write(data);
- fos.flush();
- fos.close();
- } catch (Exception ex) {
- System.out.println("ex: " + ex);
- }
- } catch (Exception e) {
- System.out.println("e: " + e);
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ContentResolver cr = getContentResolver();
- Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
- null, null, null, null);
- if (cur.getCount() > 0) {
- while (cur.moveToNext()) {
- id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
- name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
- names = names+"\n"+name;
- if (Integer.parseInt(cur.getString(
- cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
- Cursor pCur = cr.query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- null,
- ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
- new String[]{id}, null);
- while (pCur.moveToNext()) {
- phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
- // Toast.makeText(this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
- }
- phoneNos = phoneNos+"\n"+phoneNo;
- pCur.close();
- }
- }
- tv.setText(names+"\n"+phoneNos);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.button1:
- datasaver(tv.getText().toString(),ed.getText().toString());
- break;
- default:
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement