android - how to make activity output string to be another activity input string -
i created 2 activity first 1 toast string after function , second activity must take string first activity making input in textview
this first activity
package com.example.project; import android.app.activity; import android.content.intent; import android.graphics.color; import android.os.bundle; import android.view.view; import android.widget.toast; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.googlemap.onmapclicklistener; import com.google.android.gms.maps.googlemap.onmaplongclicklistener; import com.google.android.gms.maps.googlemap.onmarkerclicklistener; import com.google.android.gms.maps.mapfragment; import com.google.android.gms.maps.model.bitmapdescriptorfactory; import com.google.android.gms.maps.model.latlng; import com.google.android.gms.maps.model.marker; import com.google.android.gms.maps.model.markeroptions; import com.google.android.gms.maps.model.polyline; import com.google.android.gms.maps.model.polylineoptions; import java.util.arraylist; import java.util.list; import java.util.stack; public class airportactivity extends activity implements onmapclicklistener, onmaplongclicklistener, onmarkerclicklistener{ boolean markerclicked; polylineoptions rectoptions; polyline polyline; googlemap googlemap; list<latlng> points; list<polyline> polylines; string f = "" ; string[] placenames = {"cairo international airport","alexandria international airport","borg el arab airport", "marsa matrouh airport","sharm el-sheikh international airport","taba international airport","el kharga airport", "assiut airport","luxor international airport","aswan international airport","el arish international airport", "st. catherine international airport","sharq al-owainat airport","abu simbel airport","sohag international airport", "port said airport","el tor airport","dakhla oasis airport","marsa alam international airport","cairo west air base","almaza air force base"}; string[] placenamessnippet = {"cairo international airport1","alexandria international airport2","borg el arab airport3", "marsa matrouh airport4","sharm el-sheikh international airport5","taba international airport6","el kharga airport7", "assiut airport8","luxor international airport9","aswan international airport10","el arish international airport11", "st. catherine international airport12","sharq al-owainat airport13","abu simbel airport14","sohag international airport15", "port said airport16","el tor airport17","dakhla oasis airport18","marsa alam international airpor19t","cairo west air bas20e","almaza air force base21"}; double[] placelatitude = {30.111370, 31.192553, 30.917138,31.324435,27.978620,29.590988,27.188222,27.047695, 25.670264, 23.960397,31.076449,28.684537,22.580600,22.375813,26.331926,31.281150,28.208842,25.688581,25.558141, 30.116704,30.095975}; double[] placelongitude = {31.413910, 29.953141,29.693375, 27.222200,34.393354,34.778946 , 33.800840, 31.013473 , 32.704063, 32.821163,33.832256,34.062882, 28.720754, 31.611667,31.728437,32.242223,33.645257,28.972356,34.582821, 30.916667,31.362748}; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.airportactivity); googlemap = ((mapfragment)getfragmentmanager().findfragmentbyid( r.id.mapview)).getmap(); //googlemap.setmaptype(googlemap.map_type_hybrid); // googlemap.animatecamera(cameraupdatefactory.newlatlng(point)); points = new arraylist<latlng>(); polylines = new arraylist<polyline>(); setmap(); } private void setmap() { for(int = 0 ; i<21;i++) { googlemap.addmarker(new markeroptions() .snippet(placenamessnippet[i]) .position(new latlng(placelatitude[i], placelongitude[i])) .title(placenames[i])); points.add(new latlng(placelatitude[i], placelongitude[i])); } markerclicked = false; googlemap.setonmaplongclicklistener(this); googlemap.setonmarkerclicklistener(new onmarkerclicklistener() { @override public boolean onmarkerclick(marker arg0) { // todo auto-generated method stub points.add(arg0.getposition()); googlemap.clear(); setmap(); findshortestpath(); markerclicked = true; return false; } }); } public void dis (int[] p) { double x = 0 ; double s= 0; int numofnodes = points.size(); (int index = 0 ; index < numofnodes-1 ; index++) { x = distance2(points.get(p[index]).latitude,points.get(p[index]).longitude , points.get(p[index+1]).latitude,points.get(p[index+1]).longitude, 'k'); s = s+x ; } toast.maketext(this, "total distance of path = " + s+"km" , toast.length_short).show(); f = string.valueof(s); } /*:: available @ http://www.geodatasource.com :*/ /*:: official web site: http://www.geodatasource.com :*/ private double distance2(double lat1, double lon1, double lat2, double lon2, char unit) { double theta = lon1 - lon2; double dist = math.sin(deg2rad(lat1)) * math.sin(deg2rad(lat2)) + math.cos(deg2rad(lat1)) * math.cos(deg2rad(lat2)) * math.cos(deg2rad(theta)); dist = math.acos(dist); dist = rad2deg(dist); dist = dist * 60 * 1.1515; if (unit == 'k') { dist = dist * 1.609344; } return (dist); } /*:: function converts decimal degrees radians :*/ private double deg2rad(double deg) { return (deg * math.pi / 180.0); } /*:: function converts radians decimal degrees :*/ /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ private double rad2deg(double rad) { return (rad * 180 / math.pi); } public void onclick(view view) { startactivity(new intent("net.learn2develop.main")); } public static double distance(latlng startp, latlng endp) { double lat1 = startp.latitude; double lat2 = endp.latitude; double lon1 = startp.longitude; double lon2 = endp.longitude; double dlat = math.toradians(lat2-lat1); double dlon = math.toradians(lon2-lon1); double = math.sin(dlat/2) * math.sin(dlat/2) + math.cos(math.toradians(lat1)) * math.cos(math.toradians(lat2)) * math.sin(dlon/2) * math.sin(dlon/2); double c = 2 * math.asin(math.sqrt(a)); return 6366000 * c; } // calculate distances matrix shortest path call draw public void findshortestpath() { tspnearestneighbour tsp = new tspnearestneighbour(); int adjacencymatrix[][] = new int[points.size()][]; int counter = 0,innercounter ; int[] result = new int[points.size()]; for(latlng point : points) { innercounter=0; adjacencymatrix[counter] = new int[points.size()]; for(latlng point1 : points) { if(point.equals(point1)) { adjacencymatrix[counter][innercounter] = 0; } else adjacencymatrix[counter][innercounter] =(int) distance(point,point1); innercounter ++; } counter ++; } result = tsp.tsp(adjacencymatrix); drawshortestpath(result); dis(result); } //draws shortest path markers public void drawshortestpath(int[] path) { googlemap.clear(); int numofnodes = points.size(); for(latlng point : points) { googlemap.addmarker(new markeroptions().icon(bitmapdescriptorfactory.defaultmarker(bitmapdescriptorfactory.hue_azure)) .position(point)); } (int index = 0 ; index < numofnodes-1 ; index++) { googlemap.addpolyline(new polylineoptions() .add(points.get(path[index]), points.get(path[index+1])) .width(3) .color(color.red)); } } class tspnearestneighbour { private int numberofnodes; private stack<integer> stack; public tspnearestneighbour() { stack = new stack<integer>(); } public int[] tsp(int adjacencymatrix[][]) { numberofnodes = adjacencymatrix[0].length ; int[] result = new int[adjacencymatrix[0].length]; int resultcounter = 1; int[] visited = new int[numberofnodes]; visited[0] = 1; stack.push(0); int element, dst = 0, i; int min = integer.max_value; boolean minflag = false; result[0] = 0; //system.out.print(1 + "\t"); while (!stack.isempty()) { element = stack.peek(); = 0; min = integer.max_value; while (i < numberofnodes) { if (adjacencymatrix[element][i] > 1 && visited[i] == 0) { if (min > adjacencymatrix[element][i]) { min = adjacencymatrix[element][i]; dst = i; minflag = true; } } i++; } if (minflag) { visited[dst] = 1; stack.push(dst); result[resultcounter] = dst; resultcounter++; //system.out.print(dst + "\t"); minflag = false; continue; } stack.pop(); } return result; } } @override public void onmapclick(latlng arg0) { // todo auto-generated method stub } @override public void onmaplongclick(latlng arg0) { // todo auto-generated method stub } @override public boolean onmarkerclick(marker arg0) { // todo auto-generated method stub return false; } }
note : output " s " double , convert string
second activity
package com.example.project; import android.app.activity; import android.app.pendingintent; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.content.intentfilter; import android.os.bundle; import android.telephony.gsm.smsmanager; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class sms extends activity { button btnsendsms; edittext txtphoneno; airportactivity air = new airportactivity(); /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); btnsendsms = (button) findviewbyid(r.id.btnsendsms); txtphoneno = (edittext) findviewbyid(r.id.txtphoneno); final textview ltextview=(textview) findviewbyid(r.id.textview1); /* intent sendintent = new intent(intent.action_view); sendintent.putextra("sms_body", "content of sms goes here..."); sendintent.settype("vnd.android-dir/mms-sms"); startactivity(sendintent); */ ltextview.settext(air.f); btnsendsms.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string phoneno = txtphoneno.gettext().tostring(); air.f = ltextview.gettext().tostring(); if (phoneno.length()>0 && air.f.length()>0) sendsms(phoneno, air.f); else toast.maketext(getbasecontext(), "please enter both phone number , message.", toast.length_short).show(); } }); } public string take(string s) { air.f = s; return air.f; } //---sends sms message device--- private void sendsms(string phonenumber, string message) { /* pendingintent pi = pendingintent.getactivity(this, 0, new intent(this, test.class), 0); smsmanager sms = smsmanager.getdefault(); sms.sendtextmessage(phonenumber, null, message, pi, null); */ string sent = "sms_sent"; string delivered = "sms_delivered"; pendingintent sentpi = pendingintent.getbroadcast(this, 0, new intent(sent), 0); pendingintent deliveredpi = pendingintent.getbroadcast(this, 0, new intent(delivered), 0); //---when sms has been sent--- registerreceiver(new broadcastreceiver(){ @override public void onreceive(context arg0, intent arg1) { switch (getresultcode()) { case activity.result_ok: toast.maketext(getbasecontext(), "sms sent", toast.length_short).show(); break; case smsmanager.result_error_generic_failure: toast.maketext(getbasecontext(), "generic failure", toast.length_short).show(); break; case smsmanager.result_error_no_service: toast.maketext(getbasecontext(), "no service", toast.length_short).show(); break; case smsmanager.result_error_null_pdu: toast.maketext(getbasecontext(), "null pdu", toast.length_short).show(); break; case smsmanager.result_error_radio_off: toast.maketext(getbasecontext(), "radio off", toast.length_short).show(); break; } } }, new intentfilter(sent)); //---when sms has been delivered--- registerreceiver(new broadcastreceiver(){ @override public void onreceive(context arg0, intent arg1) { switch (getresultcode()) { case activity.result_ok: toast.maketext(getbasecontext(), "sms delivered", toast.length_short).show(); break; case activity.result_canceled: toast.maketext(getbasecontext(), "sms not delivered", toast.length_short).show(); break; } } }, new intentfilter(delivered)); smsmanager sms = smsmanager.getdefault(); sms.sendtextmessage(phonenumber, null, message, sentpi, deliveredpi); } }
what must make word message toast of first activity? sorry bad english
pass data using intents, have is: create intent object , load string start new activity follows:
intent intent = new intent("activitya.this", "activityb.class"); intent.putextra("key", "your string"); startactivity(intent);
in second activity extras sent , ever want is:
string mystring = getintents().getextras().getstring("key");
notice "key" key string value or can think of mark identifies string other extras, can name related string.
you visit http://developer.android.com/guide/components/intents-filters.html more info on intents.
Comments
Post a Comment