Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Android APIs

Options
  • 18-09-2012 6:26pm
    #1
    Registered Users Posts: 8,324 ✭✭✭


    I have to create an Android app of my choice for a project. I've made my decision which will focus on the following APIs

    * GPS Location (getting and storing location, not necessarily displaying it)

    * Sending an email (sent from the app with predefined user inputs as declared in a settings page, and sent using the default Email app settings)

    * Storing user name (essentially 4 inputs - Name, Email Address, Email Message and Location Data [pref one that is readable by Google Maps on a PC/Phone]) - I think the Shared Preferences API is what I need here.

    The Google Docs are good, but would anyone have links to good tutorials/sites on the above material?

    I don't want code, as such, which is why I'm not providing an app description, but where to educate myself.

    I have an okay understanding of Java, and this project won't be too tasking on that front as it's not overly complicated, it's just linking it in with the phone hardware and OS APIs to make a working, albeit basic enough, app that only consists of about 4-5 activities.


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    The samples that come with the sdk cover all that stuff, best place to start.


  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    chrislad wrote: »
    * GPS Location (getting and storing location, not necessarily displaying it)

    * Sending an email (sent from the app with predefined user inputs as declared in a settings page, and sent using the default Email app settings)

    * Storing user name (essentially 4 inputs - Name, Email Address, Email Message and Location Data [pref one that is readable by Google Maps on a PC/Phone]) - I think the Shared Preferences API is what I need here.
    I found these videos by Marakana a good place to get started but ditched them pretty quickly after I started and just used the Android Developers website.

    For point 1 search for location and you will find a good guide and training section along with a blog post.
    For point 2 look at intents, there is an example which does pretty much what you want on one of the intent pages.
    For point 3 the AccountManager is probably a better choice but you can do it with SharedPreferences.


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    Kavrocks wrote: »
    chrislad wrote: »
    * GPS Location (getting and storing location, not necessarily displaying it)

    * Sending an email (sent from the app with predefined user inputs as declared in a settings page, and sent using the default Email app settings)

    * Storing user name (essentially 4 inputs - Name, Email Address, Email Message and Location Data [pref one that is readable by Google Maps on a PC/Phone]) - I think the Shared Preferences API is what I need here.
    I found these videos by Marakana a good place to get started but ditched them pretty quickly after I started and just used the Android Developers website.

    For point 1 search for location and you will find a good guide and training section along with a blog post.
    For point 2 look at intents, there is an example which does pretty much what you want on one of the intent pages.
    For point 3 the AccountManager is probably a better choice but you can do it with SharedPreferences.

    Thanks. I have gotten sharedpresferences started so I might as well finish it off. Doesn't seem to be designed for large information but it's fine for the context of this app as all it is storing is a name, email address and maybe an email body. Nothing more than that.


  • Registered Users Posts: 211 ✭✭CrazyFish


    For the gps location look at http://developer.android.com/reference/android/location/LocationManager.html. You can use this to get a Location object. Then if you wish to use this location on a google maps you can just convert the location object to a Geopoint https://developers.google.com/maps/documentation/android/reference/com/google/android/maps/GeoPoint


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    package cs4084.panic.button;
    
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.support.v4.app.NavUtils;
    
    public class Settings extends Activity {
    	
    	EditText currentName, currentEmail, currentBody, setName, setEmail, setBody;
    	Button btnSet, btnReset;
    	SharedPreferences app_preferences;
    	
    	
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);
            getActionBar().setDisplayHomeAsUpEnabled(true); // caused error, ignore for now, related to different Android versions
            currentName = (EditText) findViewById(R.id.currentName);
            currentEmail = (EditText) findViewById(R.id.currentEmail);
            currentBody = (EditText) findViewById(R.id.currentBody); // populate the fields already existing with previous last entry
            setName = (EditText) findViewById(R.id.currentName);
            setEmail = (EditText) findViewById(R.id.currentEmail);
            setBody = (EditText) findViewById(R.id.currentBody); // Do I need to do this? Declaring values, when not really needed right now. change all the entries (or 1 or 2)
            btnSet = (Button) findViewById(R.id.btnSet); // button to commit changes
            btnReset = (Button) findViewById(R.id.btnReset); // button to change all values to null
            
            
            app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
            
            SharedPreferences.Editor restore = app_preferences.edit();
            restore.putString(currentName.toString(), currentName.getText().toString().trim());
            restore.putString(currentEmail.toString(), currentEmail.getText().toString().trim());
            restore.putString(currentBody.toString(), currentBody.getText().toString().trim());
            restore.commit();
            
            btnSet.setOnClickListener(new View.OnClickListener() 
            		{
    			
            			@Override
            					public void onClick(View v) 
            						{
            							SharedPreferences.Editor editor = app_preferences.edit();
            							editor.putString(currentName.toString(), setName.getText().toString().trim());
            							editor.putString(currentEmail.toString(), setEmail.getText().toString().trim());
            							editor.putString(currentBody.toString(), setBody.getText().toString().trim());
            							Log.d("Preferences", setName.getText().toString().trim());
            							Log.d("Preferences", setEmail.getText().toString().trim());
            							Log.d("Preferences", setBody.getText().toString().trim());
            								editor.commit();
    				
            						}
            		});
            
            btnReset.setOnClickListener(new View.OnClickListener()
            {
            	@Override
            		public void onClick(View v)
            	{
            		
            	}
            });	
        }
            
            
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_settings, menu);
            return true;
        }
    
        
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    NavUtils.navigateUpFromSameTask(this);
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
    }
    

    Can someone see what I'm doing wrong here?

    What I am trying to do is have 6 variables, and two buttons

    The variables are currentName, currentEmail, currentBody (which hold the current values) and setName, setEmail and setBody which update currentVarName with their value. They are defaulted to the same value as current and currentValue is changed when Save is pressed (reset isn't current coded but will reset currentValue to null or black or whatever)

    I just get nothing saved.

    This is the debug stuff
    09-20 11:24:56.475: D/Preferences(17855): g
    09-20 11:24:56.475: D/Preferences(17855): Tesr
    09-20 11:24:56.475: D/Preferences(17855): G
    09-20 11:24:58.690: D/SensorManager(17855): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41c63898
    09-20 11:24:58.690: D/Sensors(17855): Remain listener = Sending .. normal delay 200ms
    09-20 11:24:58.690: I/Sensors(17855): sendDelay --- 200000000
    09-20 11:24:58.690: D/SensorManager(17855): JNI - sendDelay
    09-20 11:24:58.690: I/SensorManager(17855): Set normal delay = true
    09-20 11:24:59.575: D/SensorManager(17855): registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41c8ba68
    09-20 11:24:59.660: E/SpannableStringBuilder(17855): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    09-20 11:24:59.660: E/SpannableStringBuilder(17855): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
    

    This is all self taught so bear with me!!!


  • Advertisement
  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    How do you know you get nothing saved? You don't read the values. For some reason unbeknown to me you add the values to SharedPreferences in onCreate and then you add them again when the button is clicked.

    SharedPreferences.Editor is not used for reading values.

    Why do you use currentName.toString() as your key? For the sake of this you would probably be better off just defining constants as currentName.toString() may not give you the same value each time.

    To get a value you previously put use
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    
    String currentNameVal = prefs.getString(CURRENT_NAME_KEY);
    
    currentName.setText(currentNameVal);
    

    Edit: I think what you are trying to do would be easier using PreferenceActivity and PreferenceFragment. It will handle putting values when a preference is changed and will allow you to set default values too.


  • Registered Users Posts: 8,324 ✭✭✭chrislad


    I was just messing with code from other sites trying to figure it out. I've rewritten it a bit, but it's still not working fully. The reset button is working (in that it fills the fields) but it doesn't seem to be saving (or it is, and I'm not loading it properly)
    package cs4084.panic.button;
    
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.support.v4.app.NavUtils;
    
    public class Settings extends Activity {
    	
    	EditText currentName, currentEmail, currentBody, setName, setEmail, setBody;
    	Button btnSet, btnReset;
    	SharedPreferences app_preferences;
    	
    	
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_settings);
            getActionBar().setDisplayHomeAsUpEnabled(true); // caused error, ignore for now, related to different Android versions
            
            currentName = (EditText) findViewById(R.id.currentName);
            currentEmail = (EditText) findViewById(R.id.currentEmail);
            currentBody = (EditText) findViewById(R.id.currentBody); // populate the fields already existing with previous last entry
            setName = (EditText) findViewById(R.id.currentName);
            setEmail = (EditText) findViewById(R.id.currentEmail);
            setBody = (EditText) findViewById(R.id.currentBody); // Do I need to do this? Declaring values, when not really needed right now. change all the entries (or 1 or 2)
            btnSet = (Button) findViewById(R.id.btnSet); // button to commit changes
            btnReset = (Button) findViewById(R.id.btnReset); // button to change all values to null
            app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
            loadData();
            
            
            
            btnSet.setOnClickListener(new View.OnClickListener() 
            		{
            		@Override
            		public void onClick(View v) 
            			{
            				saveData(currentName.toString(), setName.toString());
            				saveData(currentEmail.toString(), setEmail.toString());
            				saveData(currentBody.toString(), setBody.toString());
    				
            			}
            		});
            
            btnReset.setOnClickListener(new View.OnClickListener()
            {
            	@Override
            		public void onClick(View v)
            	{
            		clearData();
            	}
            });	
        }
            
        
        private void clearData()
        {
        	SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        	String deleteName = sharedPreferences.getString(currentName.toString(), "Enter Name");
        	String deleteEmail = sharedPreferences.getString(currentEmail.toString(), "Enter Email Address");
        	String deleteBody = sharedPreferences.getString(currentBody.toString(), "Enter Email Body");
        	setName.setText(deleteName);
        	setEmail.setText(deleteEmail);
        	setBody.setText(deleteBody);
        	
        	
        }
        
        
        private void saveData(String current, String set)
        {
        	SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        	SharedPreferences.Editor editor = sharedPreferences.edit();
        	editor.putString(current, set);
        	editor.commit();
        }
            
        
        private void loadData()
        {
        	SharedPreferences s = getPreferences(MODE_PRIVATE);
        	String loadName = s.getString(currentName.toString(), null);
        	String loadEmail = s.getString(currentEmail.toString(), null);
        	String loadBody = s.getString(currentBody.toString(), null);
        	currentName.setText(loadName);
        	currentEmail.setText(loadEmail);
        	currentBody.setText(loadBody);
        	
        }
        
        
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_settings, menu);
            return true;
        }
    
        
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    NavUtils.navigateUpFromSameTask(this);
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }
    
    }
    


Advertisement