Hi all,

I am new to Android programming and I am trying to write an
application which uses a blocking list based on ArrayList.

The class Querycontacts presents to the user contains to block.
Querycontacts calls ExtractContracts to add the number to an ArrayList
checking to see if the number is already on the list.

My problem :-

When I call QueryContacts from myPhone.java then any numbers added to
ArrayList items in ExtractContacts are lost. This is preventing my
from using the ArrayList items.

I am new to Android, so thank you for any help you can provide me.

Kind Regards,
Graham

myPhone.java >>>
case 4:
 Intent z = new Intent(myPhone.this, QueryContacts.class);
startActivity(z);
<<<
Code

myPhone.java

package telephone.org;


import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.PhoneNumberUtils;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;



public class myPhone extends Activity {
        String srvcName = Context.TELEPHONY_SERVICE;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       final EditText phoneNumber =
(EditText)findViewById(R.id.phone);
       Button phoneDialer = (Button)findViewById(R.id.dialer);

        //format number rules
        if (!
PhoneNumberUtils.isWellFormedSmsAddress(phoneNumber.toString())){
                Toast.makeText(myPhone.this, "Invalid
Number",Toast.LENGTH_SHORT);
         }

        phoneDialer.setOnClickListener(new OnClickListener(){
                @Override

                        public void onClick(View v1) {
                                //NEW CODE VALIDATION FOR CONTACT DETAILS
                                //REPLACE WITH CONTACTS CLASS
                                String phoneNumberContact = 
phoneNumber.getText().toString();
                                if 
(PhoneNumberUtils.isWellFormedSmsAddress(phoneNumberContact)) {
                                // TODO Auto-generated method stub
                                Toast toast = Toast.makeText(myPhone.this, 
"Number is " +
phoneNumber.getText().toString(),Toast.LENGTH_SHORT);
                                toast.show();
                                Intent intent = new Intent(Intent.ACTION_DIAL);
                                
intent.setData(Uri.parse("tel:"+phoneNumber.getText()));
                                startActivity(intent);
                                }
                                else {
                                        Toast.makeText (myPhone.this, "Invalid 
Phone Number  - try
again", Toast.LENGTH_LONG).show();
                                }
                        }
    });

}

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
        boolean supRetVal = super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, 0, Menu.NONE,
getString(R.string.LOCATION_ENABLE));
        menu.add(Menu.NONE, 1, Menu.NONE,
getString(R.string.LOCATION_DISABLE));
        menu.add(Menu.NONE, 2, Menu.NONE, getString(R.string.IMSI));
        menu.add(Menu.NONE, 3, Menu.NONE, getString(R.string.SMS));
        menu.add(Menu.NONE, 4, Menu.NONE, getString(R.string.BLOCK));
        return supRetVal;

    }

    //listen for cell changes
      PhoneStateListener cellLocationListener = new
PhoneStateListener() {
                @Override
                public void onCellLocationChanged(CellLocation location) {
                GsmCellLocation gsmLocation = (GsmCellLocation)location;
                Toast.makeText(getApplicationContext(),"CELL ID:" +
String.valueOf(gsmLocation.getCid()) + "  LAC:" +
String.valueOf(gsmLocation.getLac()), Toast.LENGTH_LONG).show();

                }
                };

    @Override
        public boolean onOptionsItemSelected(MenuItem item){

        switch (item.getItemId()) {
        case 0:
                Toast.makeText(getApplicationContext(),"Enabling Cell location
tracking", Toast.LENGTH_LONG).show();
                        TelephonyManager telephonyManager =
(TelephonyManager)getSystemService(srvcName);  //must be declared here
or after context
        
telephonyManager.listen(cellLocationListener,PhoneStateListener.LISTEN_CELL_LOCATION);
                return true;
        case 1:
                Toast.makeText(getApplicationContext(),"Disabling Cell location
tracking", Toast.LENGTH_LONG).show();
                TelephonyManager tm = (TelephonyManager)
getSystemService(srvcName);
                tm.listen(cellLocationListener,
PhoneStateListener.LISTEN_NONE);
                return true;
        case 2:
                Intent i = new Intent(myPhone.this, status.class);
                startActivity(i);
                return true;
        case 3:
                Intent y = new Intent(myPhone.this, SMSTest.class);
                startActivity(y);
            return true;
        case 4:
                Intent z = new Intent(myPhone.this, QueryContacts.class);
                startActivity(z);


    }
        return true;
    }



    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onPause() {
        super.onPause();
    }

 @Override
public void onDestroy() {
        super.onDestroy();



 }






}


code

QueryContacts

package telephone.org;



import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;


public class QueryContacts extends Activity {

        //declarations
    Button btnContacts;
    TextView txtContacts;
    EditText ed;
    static final int PICK_CONTACT=1;
    public String xnumber;


    ExtractContacts block = new ExtractContacts();


//end declarations


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blockinglist);


        Button btnContacts;




btnContacts = (Button)findViewById(R.id.button1);
txtContacts = (TextView)findViewById(R.id.textview1);
ed = (EditText)findViewById(R.id.edittxt1);
ed.setText(xnumber);


btnContacts.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
                //setType sets MIME type of intent only specify a type not data
                //startActivityForResult when finished pass result to
onActivityResult
                //allow the user to pick content
                //set data type of intent MIME
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                //A data kind representing a telephone number  MIME type used 
when
storing data in data table
        
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);


                //INTENT and REQUEST CODE
                startActivityForResult(intent, PICK_CONTACT);

        }


});

    }

    public void onActivityResult(int requestCode, int resultCode,
Intent data){
   //CURSOR IS A RESULT SET YOU MUST MOVE TO THE FIRST POSTION USING
moveToFirst()
   //URI is not equal to null and is set to CONTENT_ITEM_TYPE
  if (data !=null) {
                Uri uri = data.getData();
                if (uri != null) {
                        Cursor c = null;
                        try {
                                //QUERY THE CONTENT URI using Cursor 
getContentResolver
                                c = getContentResolver().query(uri, new 
String[]{
 
ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.TYPE},
null, null, null);
                                if (c!=null && c.moveToFirst()) {
                                        String number = c.getString(0);
                                        int type = c.getInt(1);
                                        showSelectedNumber(type, number);
                                }

                        } finally {
                                if (c!=null){
                                        c.close();
                                }
                        }
                }
  }
}
//RESULT FUNCTION FOR ALL THINGS BEAUTIFUL
//ADD TO BLOCKING LIST
public void showSelectedNumber(int type, String number)
{ Toast.makeText(this, type + ": " + number,
Toast.LENGTH_LONG).show();
xnumber = number.toString();   //COULD BE USED AS A RESULT
EditText ed=(EditText)findViewById(R.id.edittxt1);
ed.setText(xnumber);
int valuetocheck = 0;
valuetocheck = block.addToArray(xnumber);

if (valuetocheck == 1)
{

Toast.makeText(this, " NUMBER IS NOT BLOCKED ALREADY IN BLOCKING
LIST:" + xnumber, Toast.LENGTH_LONG).show();

}

if (valuetocheck == 0)
{

Toast.makeText(this, "ADDING NUMBER TO BLOCKING LIST:" + xnumber,
Toast.LENGTH_LONG).show();


}


}
public boolean onKeydown(int keyCode, KeyEvent event){
         if ((keyCode == KeyEvent.KEYCODE_BACK )) {
                        Intent a = new Intent(QueryContacts.this, 
myPhone.class);
                startActivity(a);
         }
        return false;

}
}


Code

ExtractContacts

package telephone.org;

import java.util.ArrayList;
import android.app.Activity;


//I need an activity for my toast to work correctly
public class ExtractContacts extends Activity {




         ArrayList<String> items=new ArrayList<String>();



public int addToArray(String contactToBlock) {

        //check if item exits or not
        if(items.contains(contactToBlock)){
                System.out.print("NOT BLOCKING ITEM IT ALREADY EXISTS");
                System.out.print("\n");

                return 1;
        }
        else {
         items.add(contactToBlock);
         System.out.print("BLOCKED ITEMS ARE: " +  items);
     System.out.print("\n");

       return 0;
        }

}
//new code to check list if the contact is on the list
public int checkList(String contactToBlock){

        if(items.contains(contactToBlock)){
                System.out.print("SMS ORIGINATOR IS ON THE LIST SHOULD BE 
BLOCKED");
                System.out.print("\n");
                return 1;
        }

        else if(!items.contains(contactToBlock))
        {
                System.out.print("SMS ORIGINATOR IS NOT ON THE LIST ");
                System.out.print("\n");
                return 0;
}
        return 0;
}
//end of new code

@Override
public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((items == null) ? 0 : items.hashCode());
        return result;
}



@Override
public boolean equals(Object obj) {
        if (this == obj)
                return true;
        if (obj == null)
                return false;
        if (getClass() != obj.getClass())
                return false;
        ExtractContacts other = (ExtractContacts) obj;
        if (items == null) {
                if (other.items != null)
                        return false;
        } else if (!items.equals(other.items))
                return false;
        return true;
}
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to