Get Android Plugin for Eclipse
1. Open Eclipse and create a new Android project. The first thing that we will handle is the import of Google Map classes. To get the Google Maps files we need to download the last version of Google Play Services via the Android SDK Manager.
2. After you downloaded the Google Play Services, restart Eclipse and in the Package Explorer Right-Click –> Import…. In the opened windows choose “Existing Android Code into Workspace” and click “Next”. Click the “Browse…” Button and head to the location of your SDK folder. in it find the following folder:
\extras\google\google_play_services\libproject\google-play-services_lib
and press “OK”, check the V next to it in the window and press the “Finish” button.
3. Now you added Google Play Services to your work space, we have to create a reference from our project to this library. Right-Click your project and choose “Properties” go to the Android section, in the lower part press the “Add…” button and add a reference to that library.
Note: If you try to reference google-play-service library and you receive a red X next to this reference, what you should do is to move the .jar file to a location where it’s path will be shorter and then reference it again.4. Another import we have to make in order to make our application work on Operation system prior to API11 is to import the support library this can be done very easily using Eclipse: Right-Click you project and choose “Android Tools” and then choose “Add Support Library…”:
Libraries in the project
- Google APIs [Android 4.2.2]: android.jar, effects.jar, usb.jar, maps.jar
- Android Private Libraries: google-play-services.jar, android-support-v4.jar
- Android Dependencies: goog-play-serivces_lib.jar
- android-google-maps-api8.jar
Now create the following files
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.peble" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="17" /> <permission android:name="com.example.peble.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="com.example.peble.permission.MAPS_RECEIVE"/> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <uses-feature android:glEsVersion="0x00020000" android:required="true"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.peble.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.peble.DisplayMessageActivity" android:label="@string/title_activity_display_message" android:parentActivityName="com.example.peble.MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.peble.MainActivity" /> </activity> <uses-library android:name="com.google.android.maps" /> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyA1_JSw9QBI2yEUoPfXInKE5o-_F96c70I"/> </application> </manifest>
MainActivity.java
package com.example.peble; import java.io.FileInputStream; import java.io.IOException; import java.util.List; import android.content.Context; import android.content.Intent; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.maps.CameraUpdate; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; public class MainActivity extends FragmentActivity { public final static String EXTRA_MESSAGE = "com.example.peble.MESSAGE"; String message=""; LocationManager locationManager; //<2> Geocoder geocoder; //<3> TextView locationText; // MapView map; // MapController mapController; //<4> GoogleMap mMap; // @Override // public boolean isRouteDisplayed() { // return false; // } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); String textMessage=""; String filename = "peble_contacts.txt"; FileInputStream inputStream; try { inputStream = openFileInput(filename); int content; while((content = inputStream.read()) != -1) { textMessage += (char) content; } } catch (Exception e) { Toast.makeText(getApplicationContext(), "No peble_contacts.txt!", Toast.LENGTH_LONG).show(); } if (!textMessage.trim().equals("")) { EditText editText = (EditText) findViewById(R.id.edit_message); editText.setText(textMessage); } else { EditText editText = (EditText) findViewById(R.id.edit_message); editText.setText("Enter Phone Number"); } try { locationText = (TextView)this.findViewById(R.id.lblLocationInfo); mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); if (mMap == null) { Toast.makeText(getApplicationContext(), "no map!!!!!!!!!!!", Toast.LENGTH_LONG).show(); } // map = (MapView)this.findViewById(R.id.mapview); // map.setBuiltInZoomControls(true); //// mapController = map.getController(); //<4> //// mapController.setZoom(16); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); geocoder = new Geocoder(this); if (geocoder == null) { Toast.makeText(getApplicationContext(), "geocoder null!!!!!!!!!!!", Toast.LENGTH_LONG).show(); } if (location != null) { this.onLocationChanged(location); } // SupportMapFragment fragment = new SupportMapFragment(); // getSupportFragmentManager().beginTransaction().add(R.id.mapview, fragment).commit(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Location faild, please try again later!", Toast.LENGTH_LONG).show(); } } @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; } /** Called when the user clicks the Send button */ public void sendMessage(View view) { // Do something in response to button Intent intent = new Intent(this, DisplayMessageActivity.class); message=""; EditText editText = (EditText) findViewById(R.id.edit_message); message = editText.getText().toString(); intent.putExtra(EXTRA_MESSAGE, message); startActivity(intent); } @Override protected void onStart() { super.onStart(); System.out.println("on start"); } @Override protected void onRestart() { super.onRestart(); System.out.println("on restart"); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state // Always call the superclass so it can save the view hierarchy state savedInstanceState.putString("message", message); super.onSaveInstanceState(savedInstanceState); } public void onLocationChanged(Location location) { //<9> Log.d("jello", "onLocationChanged with location " + location.toString()); String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f", location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getBearing()); this.locationText.setText(text); try { // Toast.makeText(getApplicationContext(), // "before address!", // Toast.LENGTH_LONG).show(); // List addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10); //<10> // for (Address address : addresses) { // this.locationText.append("\n" + address.getAddressLine(0)); // } // GeoPoint point = new GeoPoint(latitude,longitude); // Toast.makeText(getApplicationContext(), // "Could get geopoint!", // Toast.LENGTH_LONG).show(); //mapController.animateTo(point); //<11> LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10); if (mMap != null) mMap.animateCamera(cameraUpdate); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Could not get geocoderdata!", Toast.LENGTH_LONG).show(); } } }
DisplayMessageActivity.java
package com.example.peble; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.location.LocationManager; import android.os.Build; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.telephony.SmsManager; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; public class DisplayMessageActivity extends MapActivity { String message=""; LocationManager locationManager; //<2> @Override protected boolean isRouteDisplayed() { return false; } @Override protected void onStart() { super.onStart(); message=""; } @Override protected void onRestart() { super.onRestart(); message=""; } public void onRestoreInstanceState(Bundle savedInstanceState) { // Always call the superclass so it can restore the view hierarchy super.onRestoreInstanceState(savedInstanceState); // Restore state members from saved instance } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (savedInstanceState != null) { message = savedInstanceState.getString("message"); } // Get the message from the intent Intent intent = getIntent(); if (message.trim().equals("")) message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); String filename = "peble_contacts.txt"; FileOutputStream outputStream; try { outputStream = openFileOutput(filename, Context.CONTEXT_IGNORE_SECURITY); outputStream.write(message.getBytes()); outputStream.close(); } catch (Exception e) { System.out.println("cannot read file"); } String smsText="Please help!"; try { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { smsText += String.format( "Current Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude() ); } } catch (Exception e) { Toast.makeText(getApplicationContext(), "Location faild, please try again later!", Toast.LENGTH_LONG).show(); } try { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(message, null, smsText, null, null); Toast.makeText(getApplicationContext(), smsText + " Sent!", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show(); e.printStackTrace(); } // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); textView.setText(message + " will be sent your help Message: " + smsText); // Set the text view as the activity layout setContentView(textView); } /** * Set up the {@link android.app.ActionBar}, if the API is available. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setupActionBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { getActionBar().setDisplayHomeAsUpEnabled(true); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.display_message, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // This ID represents the Home or Up button. In the case of this // activity, the Up button is shown. Use NavUtils to allow users // to navigate up one level in the application structure. For // more details, see the Navigation pattern on Android Design: // // http://developer.android.com/design/patterns/navigation.html#up-vs-back // NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } }
res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:map="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <EditText android:id="@+id/edit_message" android:layout_width="200dp" android:layout_height="100dp" android:layout_marginTop="0dp" android:layout_marginLeft="10dp" android:hint="@string/edit_message" /> <Button android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="0dp" android:layout_marginLeft="220dp" android:text="@string/button_send" android:onClick="sendMessage" /> <TextView android:layout_width="wrap_content" android:layout_height="200dp" android:layout_marginTop="110dp" android:layout_marginLeft="10dp" android:text="Waiting for location..." android:id="@+id/lblLocationInfo"/> <!-- <com.google.android.maps.MapView android:id="@+id/mapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" /> --> <fragment android:id="@+id/map" android:layout_width="300dp" android:layout_height="500dp" android:layout_marginTop="220dp" android:layout_marginLeft="10dp" android:name="com.google.android.gms.maps.SupportMapFragment"/> </RelativeLayout>
res/layout/acitivty_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".DisplayMessageActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
res/values/Strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">PEBLE</string> <string name="edit_message">Help Me</string> <string name="button_send">Send</string> <string name="action_settings">Settings</string> <string name="title_activity_main">MainActivity</string> <string name="title_activity_display_message">My Help</string> <string name="hello_world">Hello world!</string> </resources>
Misc Notes
See https://blog-emildesign.rhcloud.com/?p=435 on how to set up the Android maps project
jar signer
C:\Program Files (x86)\Java\jdk1.6.0_17\bin>jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore my-release-key.keystore c:\users\dgandikota\Desktop\Peble.apk alias_nameAdb installation
C:\eclipseworkspace\Peble\bin>"c:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe" install -r c:\users\dgandikota\desktop\Peble.apkTo fire up emulator
C:\eclipseworkspace\Peble\bin>"c:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe" shellroot@android:/ # input keyevent 82
No comments:
Post a Comment