1 Επικοινωνία 1.1 Κλήση αριθμού Για να επιτρέψουμε σ το κινητό να κάνει κλήσ η σ ε τηλέφωνο θα πρέπει να έχουμε προσ θέσ ει την επόμενη άδεια σ το αρχείο AndroidManifest.xml <uses permission android:name=" android. permission.call_phone"> </ uses permission> Το επόμενο πρόγραμμα πραγματοποιεί τηλεφωνική κλήσ η είτε απλά ακουμπώντας σ το TextView το οποίο περιέχει τον τηλεφωνικό αριθμό είτε με την πίεσ η σ το πλήκτρο με την εικόνα του τηλεφώνου. 1 public class MainActivity extends A c t i v i t y { 2 3 4 LinearLayout mainlayout=null ; 5 TextView telephone=null ; 6 ImageButton callbutton=null ; 7 f i n a l S t r i n g mytelephone=" 1212121212 " ; 8 9 private boolean istelephone ( S t r i n g x ) 10 { 11 i f ( x. length ()!=10) return f a l s e ; 12 i f ( x. charat(0)== ' 0 ' ) return f a l s e ; 13 for ( int i =0; i<x. length ( ) ; i++) 14 { 15 i f (! ( x. charat ( i )>= ' 0 ' && x. charat ( i )<= ' 9 ' ) ) return f a l s e ; 16 } 17 return true ; 18 } 19 20 @Override 21 protected void oncreate ( Bundle s a v e d I n s t a n c e S t a t e ) { 22 super. oncreate ( s a v e d I n s t a n c e S t a t e ) ; 23 mainlayout=new LinearLayout ( this ) ; 24 mainlayout. s e t O r i e n t a t i o n ( LinearLayout.VERTICAL) ; 25 setcontentview ( mainlayout ) ; 26 telephone=new TextView ( this ) ; 27 mainlayout. addview ( telephone ) ; 28 telephone. settext ( mytelephone ) ; 29 telephone. settextcolor ( Color.BLUE) ; 30 telephone. setbackgroundcolor ( Color.DKGRAY) ; 31 telephone. settypeface ( null, Typeface.BOLD_ITALIC) ; 32 telephone. s e t C l i c k a b l e ( true ) ; 33 OnClickListener c a l l e r=new OnClickListener ( ) 34 { 1
35 36 @Override 37 public void onclick ( View v ) { 38 S t r i n g t e l=telephone. gettext ( ). t o S t r i n g ( ) ; 39 i f (! istelephone ( t e l ) ) 40 { 41 AlertDialog. Builder a l e r t = new 42 AlertDialog. Builder ( MainActivity. this ) ; 43 a l e r t. s e t T i t l e ( "ERROR" ) ; 44 a l e r t. setmessage ( " S t r i n g "+t e l+" I s not v a l i d telephone! " ) ; 45 46 a l e r t. s e t P o s i t i v e B u t t o n ( "Ok", 47 new D i a l o g I n t e r f a c e. OnClickListener ( ) 48 { 49 50 @Override 51 public void onclick ( D i a l o g I n t e r f a c e dialog, 52 int which ) { 53 d i a l o g. d i s m i s s ( ) ; 54 55 } 56 57 } ) ; 58 a l e r t. show ( ) ; 59 } 60 else 61 { 62 I n t e n t c a l l I n t e n t =new I n t e n t ( I n t e n t.action_call) ; 63 c a l l I n t e n t. setdata ( Uri. parse ( " t e l : "+t e l ) ) ; 64 s t a r t A c t i v i t y ( c a l l I n t e n t ) ; 65 } 66 67 } 68 69 } ; 70 telephone. s e t O n C l i c k L i s t e n e r ( c a l l e r ) ; 71 callbutton=new ImageButton ( this ) ; 72 mainlayout. addview ( callbutton ) ; 73 callbutton. setimageresource (R. drawable. c a l lphone ) ; 74 callbutton. s e t O n C l i c k L i s t e n e r ( c a l l e r ) ; 75 } 76 } 2
1.2 Αποστολή Email Η αποσ τολή Email είναι μια σ υνηθισ μένη διαδικασ ία από ένα κινητό τηλέφωνο. Στο επόμενο παράδειγμα ο χρήσ της δίνει την διεύθυνσ η email σ την οποία θέλει να σ τείλει ένα κείμενο. Για να μπορέσ ει να γίνει η αποσ τολή EMAIL χρειάζεται η επόμενη άδεια σ το AndroidManifest.xml <uses permission android:name=" android. permission.internet"> </ uses permission> Ο κώδικας της εφαρμογής έχει ως εξής: 1 public class MainActivity extends A c t i v i t y { 2 LinearLayout mainlayout=null ; 3 EditText emailbody=null ; 4 EditText emailreceiver=null ; 5 ImageButton emailsend=null ; 6 7 private boolean isemail ( S t r i n g s ) 8 { 9 int countat =0; 10 int posat =0; 11 int i ; 12 for ( i =0; i<s. length ( ) ; i++) 13 { 14 i f ( s. charat ( i)== '@' ) 15 { 16 countat++; 17 posat=i ; 18 } 19 } 20 i f ( countat!=1) return f a l s e ; 21 i f ( posat==0) return f a l s e ; 22 i f ( posat==s. length () 1) return f a l s e ; 23 return true ; 24 } 25 26 @Override 27 protected void oncreate ( Bundle s a v e d I n s t a n c e S t a t e ) { 28 super. oncreate ( s a v e d I n s t a n c e S t a t e ) ; 29 mainlayout=new LinearLayout ( this ) ; 30 mainlayout. s e t O r i e n t a t i o n ( LinearLayout.VERTICAL) ; 31 setcontentview ( mainlayout ) ; 32 emailbody=new EditText ( this ) ; 33 emailbody. sethint ( "Write your t e x t here " ) ; 34 mainlayout. addview ( emailbody ) ; 35 emailbody. s e t L i n e s ( 1 0 ) ; 36 emailreceiver=new EditText ( this ) ; 3
37 emailreceiver. sethint ( " to : " ) ; 38 mainlayout. addview ( emailreceiver ) ; 39 emailreceiver. setinputtype ( InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS) ; 40 emailsend=new ImageButton ( this ) ; 41 emailsend. setimageresource (R. drawable. emailsend ) ; 42 mainlayout. addview ( emailsend ) ; 43 emailsend. s e t O n C l i c k L i s t e n e r (new OnClickListener ( ) 44 { 45 @Override 46 public void onclick ( View v ) 47 { 48 S t r i n g e=emailreceiver. gettext ( ). t o S t r i n g ( ) ; 49 i f (! isemail ( e ) ) 50 { 51 AlertDialog. Builder a l e r t = new 52 AlertDialog. Builder ( MainActivity. this ) ; 53 a l e r t. s e t T i t l e ( "ERROR" ) ; 54 a l e r t. setmessage ( " S t r i n g "+e+" I s not v a l i d EMAIL! " ) ; 55 a l e r t. s e t P o s i t i v e B u t t o n ( "Ok", 56 new D i a l o g I n t e r f a c e. OnClickListener ( ) 57 { 58 @Override 59 public void onclick ( D i a l o g I n t e r f a c e dialog, int which ) 60 { 61 d i a l o g. d i s m i s s ( ) ; 62 } 63 } ) ; 64 a l e r t. show ( ) ; 65 } 66 else 67 { 68 I n t e n t i = new I n t e n t ( I n t e n t.action_send) ; 69 i. settype ( " t e x t / p l a i n " ) ; 70 i. putextra ( I n t e n t.extra_email, 71 new S t r i n g [ ] { e } ) ; 72 i. putextra ( I n t e n t.extra_subject, " t e i e p " ) ; 73 S t r i n g mymessage="<h1>your message </h1>"+emailbody ; 74 i. putextra ( I n t e n t.extra_text, Html. fromhtml ( mymessage ) ) ; 75 try { 76 s t a r t A c t i v i t y ( I n t e n t. createchooser ( i, "Send mail... " ) ) ; 77 } catch ( android. content. ActivityNotFoundException ex ) 78 { 79 Toast. maketext ( MainActivity. this, 80 "There are no email c l i e n t s i n s t a l l e d. ", 81 Toast.LENGTH_SHORT). show ( ) ; 82 } 4
83 } 84 85 } 86 87 } ) ; 88 } 89 } 1.3 Αποστολή SMS Μια ακόμα λειτουργία που μπορεί να επιτευχθεί με κινητό είναι η αποσ τολή SMS μέσ α από αυτό. Για αν μπορέσ ουμε να το επιτύχουμε χρειαζόμασ τε την επόμενη άδεια σ το αρχείο AndroidManifest.xml <uses permission android:name=" android. permission.send_sms"> </ uses permission> Ο κώδικας της εφαρμογής ακολουθεί σ την σ υνέχεια. Σε αυτήν την εφαρμογή αν ο χρήσ της ξεπεράσ ει τα 160 γράμματα ο μετρητής γραμμάτων αποκτά κόκκινα γράμματα. 1 public class MainActivity extends A c t i v i t y { 2 LinearLayout mainlayout=null ; 3 TextView charcount=null ; 4 EditText smsbody=null ; 5 TextView smsreceiver=null ; 6 ImageButton smssend=null ; 7 8 9 @Override 10 protected void oncreate ( Bundle s a v e d I n s t a n c e S t a t e ) { 11 super. oncreate ( s a v e d I n s t a n c e S t a t e ) ; 12 mainlayout=new LinearLayout ( this ) ; 13 mainlayout. s e t O r i e n t a t i o n ( LinearLayout.VERTICAL) ; 14 setcontentview ( mainlayout ) ; 15 charcount=new TextView ( this ) ; 16 charcount. settext ( " Characters : 0" ) ; 17 mainlayout. addview ( charcount ) ; 18 smsreceiver=new EditText ( this ) ; 19 smsreceiver. sethint ( " Telephone " ) ; 20 mainlayout. addview ( smsreceiver ) ; 21 smsreceiver. setinputtype ( InputType.TYPE_CLASS_PHONE) ; 22 smsbody=new EditText ( this ) ; 23 smsbody. sethint ( "SMS BODY" ) ; 24 mainlayout. addview ( smsbody ) ; 25 smsbody. addtextchangedlistener ( new TextWatcher ( ) { 26 public void aftertextchanged ( Editable s ) { 5
27 int count=s. t o S t r i n g ( ). length ( ) ; 28 charcount. settext ( " Characters : "+count ) ; 29 i f ( count >=160) 30 charcount. settextcolor ( Color.RED) ; 31 else 32 charcount. settextcolor ( Color.BLACK) ; 33 } 34 35 public void beforetextchanged ( CharSequence s, int s t a r t, 36 int count, int a f t e r ) { 37 // TODO Auto generated method s t u b 38 } 39 40 public void ontextchanged ( CharSequence s, int s t a r t, 41 int before, int count ) { 42 // TODO Auto generated method s t u b 43 } 44 45 } ) ; 46 smssend=new ImageButton ( this ) ; 47 mainlayout. addview ( smssend ) ; 48 smssend. setimageresource (R. drawable. smssend ) ; 49 smssend. s e t O n C l i c k L i s t e n e r (new OnClickListener ( ) 50 { 51 52 @Override 53 public void onclick ( View v ) { 54 55 S t r i n g phoneno=smsreceiver. gettext ( ). t o S t r i n g ( ) ; 56 S t r i n g message=smsbody. gettext ( ). t o S t r i n g ( ) ; 57 try { 58 SmsManager smsmanager = SmsManager. g e t D e f a u l t ( ) ; 59 smsmanager. sendtextmessage ( phoneno, null, 60 message, null, null ) ; 61 Toast. maketext ( getapplicationcontext ( ), "SMS Sent! ", 62 Toast.LENGTH_LONG). show ( ) ; 63 } catch ( Exception e ) { 64 Toast. maketext ( getapplicationcontext ( ), 65 "SMS f a i l d, p l e a s e try again l a t e r! ", 66 Toast.LENGTH_LONG). show ( ) ; 67 e. printstacktrace ( ) ; 68 } 69 70 71 } 72 6
73 } ) ; 74 } 75 } 1.4 Λήψη τοποθεσίας με GPS Το τελευταίο θέμα με το οποίο θα ασ χοληθούμε είναι αυτό της λήψης σ τίγματος με GPS καθώς και η πλοήγησ η μέσ ω της google σ ε σ υγκεκριμένες θέσ εις. 1.4.1 Λήψη σ τίγματος Καταρχήν πρέπει να προσ τεθεί η ακόλουθη άδεια σ το AndroidManifest.xml <uses permission android:name=" android. permission.access_fine_location" > </ uses permission> Η κεντρική Activity που ελέγχει το GPS παρουσ ιάζεται σ την σ υνέχεια. Σε αυτήν ο χρήσ της πατώντας το μοναδικό πλήκτρο μπορεί να ξεκινήσ ει την αναζήτησ η για GPS και κάθε φορά που αλλάζει η τρέχουσ α θέσ η γίνεται εμφάνισ ή της σ το TextView που είναι πάνω από το πλήκτρο. 1 public class MainActivity extends A c t i v i t y { 2 LinearLayout mainlayout=null ; 3 TextView s t a t u s b a r ; 4 Button startgps ; 5 LocationManager locationmanager=null ; 6 GpsLocation l o c a t i o n l i s t e n e r=null ; 7 f i n a l int timebetween =5000; 8 f i n a l int meters =10; 9 10 @Override 11 protected void oncreate ( Bundle s a v e d I n s t a n c e S t a t e ) { 12 super. oncreate ( s a v e d I n s t a n c e S t a t e ) ; 13 mainlayout=new LinearLayout ( this ) ; 14 mainlayout. s e t O r i e n t a t i o n ( LinearLayout.VERTICAL) ; 15 setcontentview ( mainlayout ) ; 16 s t a t u s b a r=new TextView ( this ) ; 17 mainlayout. addview ( s t a t u s b a r ) ; 18 s t a t u s b a r. setbackgroundcolor ( Color.DKGRAY) ; 19 s t a t u s b a r. settextcolor ( Color.YELLOW) ; 20 s t a t u s b a r. s e t T e x t S i z e ( 2 0 ) ; 21 startgps=new Button ( this ) ; 22 startgps. settext ( "START GPS" ) ; 23 mainlayout. addview ( startgps ) ; 24 startgps. s e t O n C l i c k L i s t e n e r (new OnClickListener ( ) 25 { 26 7
27 @Override 28 public void onclick ( View v ) { 29 i f ( l o c a t i o n l i s t e n e r==null ) 30 { 31 locationmanager = ( LocationManager ) 32 getsystemservice ( Context.LOCATION_SERVICE) ; 33 l o c a t i o n l i s t e n e r = new GpsLocation ( s t a t u s b a r ) ; 34 locationmanager. requestlocationupdates ( 35 LocationManager.GPS_PROVIDER, timebetween, meters, l o c a t i o n l i s t e n e r ) ; 36 startgps. settext ( "STOP GPS" ) ; 37 } 38 else 39 { 40 locationmanager. removeupdates ( l o c a t i o n l i s t e n e r ) ; 41 l o c a t i o n l i s t e n e r=null ; 42 startgps. settext ( "START GPS" ) ; 43 44 } 45 } 46 47 } ) ; 48 } 49 } Η κατηγορία GpsLocation ορίζεται σ την σ υνέχεια: 1 public class GpsLocation implements L o c a t i o n L i s t e n e r { 2 3 TextView s t a t u s b a r ; 4 public GpsLocation ( TextView tx ) 5 { 6 s t a t u s b a r=tx ; 7 8 } 9 10 @Override 11 public void onlocationchanged ( Location l o c a t i o n ) { 12 double l a t=l o c a t i o n. getlatitude ( ) ; 13 double lng=l o c a t i o n. getlongitude ( ) ; 14 s t a t u s b a r. settext ( "New Location : "+l a t+", "+lng ) ; 15 } 16 17 @Override 18 public void onstatuschanged ( S t r i n g provider, int status, Bundle e x t r a s ) { 19 s t a t u s b a r. settext ( " Status o f gps changed "+s t a t u s ) ; 20 21 } 8
22 23 @Override 24 public void onproviderenabled ( S t r i n g provider ) { 25 s t a t u s b a r. settext ( " Proviced enabled "+provider ) ; 26 27 } 28 29 @Override 30 public void onproviderdisabled ( S t r i n g provider ) { 31 s t a t u s b a r. settext ( " Proviced d i s a b l e d "+p rovider ) ; 32 33 } 34 } 1.4.2 Εύρεσ η τοποθεσ ίας Μια πολύ χρήσ ιμη διαδικασ ία είναι το geocoding με την οποία μπορούμε να μάθουμε πληροφορίες για την θέσ η μας ρωτώντας servers της google. Στην σ υνέχεια παρουσ ιάζεται μια ελαφρώς αλλαγμένη εκδοχή της GpsLocation όπου τυπώνεται και η πόλη σ την οποία βρισ κόμασ τε. Απαραίτητη προϋπόθεσ η είναι η χρήσ η διαδικτύου: 1 2 public class GpsLocation implements L o c a t i o n L i s t e n e r { 3 4 TextView s t a t u s b a r ; 5 Context mcontext ; 6 public GpsLocation ( Context ctx, TextView t e x t ) 7 { 8 mcontext=ctx ; 9 s t a t u s b a r=t e x t ; 10 11 } 12 13 @Override 14 public void onlocationchanged ( Location l o c a t i o n ) { 15 double l a t=l o c a t i o n. getlatitude ( ) ; 16 double lng=l o c a t i o n. getlongitude ( ) ; 17 s t a t u s b a r. settext ( "New Location : "+l a t+", "+lng ) ; 18 S t r i n g cityname = null ; 19 Geocoder gcd = new Geocoder ( mcontext, Locale. g e t D e f a u l t ( ) ) ; 20 List <Address> a d d r e s s e s ; 21 try { 22 a d d r e s s e s = gcd. getfromlocation ( l o c a t i o n. getlatitude ( ), 23 l o c a t i o n. getlongitude ( ), 1 ) ; 24 i f ( a d d r e s s e s. s i z e ( ) > 0) 9
25 System. out. p r i n t l n ( a d d r e s s e s. get ( 0 ). g e t L o c a l i t y ( ) ) ; 26 cityname = a d d r e s s e s. get ( 0 ). g e t L o c a l i t y ( ) ; 27 } 28 catch ( IOException e ) { 29 e. printstacktrace ( ) ; 30 } 31 S t r i n g s = lng + "\n" + l a t + "\n\nmy Current City i s : " 32 + cityname ; 33 s t a t u s b a r. settext ( s ) ; 34 } 35 36 @Override 37 public void onstatuschanged ( S t r i n g provider, int status, Bundle e x t r a s ) { 38 s t a t u s b a r. settext ( " Status o f gps changed "+s t a t u s ) ; 39 40 } 41 42 @Override 43 public void onproviderenabled ( S t r i n g provider ) { 44 s t a t u s b a r. settext ( " Proviced enabled "+p rovider ) ; 45 46 } 47 48 @Override 49 public void onproviderdisabled ( S t r i n g provider ) { 50 s t a t u s b a r. settext ( " Proviced d i s a b l e d "+p rovider ) ; 51 52 } 53 54 55 56 57 } 1.4.3 Πλοήγησ η σ ε θέσ η Μια ακόμα σ υνηθισ μένη ενέργεια με χρήσ η GPS είναι η πλόηγησ η σ ε κάποια σ υγκεκριμένη θέσ η, πχ σ ε μια υπηρεσ ία ή ένα σ ημείο ενδιαφέροντος. Για τον λόγο αυτό θα χρησ ιμοποιήσ ουμε έτοιμες υπηρεσ ίες της google (χρήσ η internet) αλλά και την τρέχουσ α θέσ η που θα λάβουμε από ένα LocationListener. Στην επόμενη εφαρμογή δημιουργούμε δύο μεθόδους που επισ τρέφουν την τελευταία θέσ η και με το πάτημα ενός πλήκτρου δίνονται οδηγιές για την μετάβασ η σ τον χώρο του ΤΕΙ Ηπείρου. Σε αυτήν περίπτωσ η η νέα MainActivity θα έχει ως εξής: 1 public class MainActivity extends A c t i v i t y { 2 LinearLayout mainlayout=null ; 10
3 TextView s t a t u s b a r ; 4 Button startgps ; 5 Button gototeiep ; 6 LocationManager locationmanager=null ; 7 GpsLocation l o c a t i o n l i s t e n e r=null ; 8 f i n a l int timebetween =5000; 9 f i n a l int meters =10; 10 11 @Override 12 protected void oncreate ( Bundle s a v e d I n s t a n c e S t a t e ) { 13 super. oncreate ( s a v e d I n s t a n c e S t a t e ) ; 14 mainlayout=new LinearLayout ( this ) ; 15 mainlayout. s e t O r i e n t a t i o n ( LinearLayout.VERTICAL) ; 16 setcontentview ( mainlayout ) ; 17 s t a t u s b a r=new TextView ( this ) ; 18 mainlayout. addview ( s t a t u s b a r ) ; 19 s t a t u s b a r. setbackgroundcolor ( Color.DKGRAY) ; 20 s t a t u s b a r. settextcolor ( Color.YELLOW) ; 21 s t a t u s b a r. s e t T e x t S i z e ( 2 0 ) ; 22 startgps=new Button ( this ) ; 23 startgps. settext ( "START GPS" ) ; 24 mainlayout. addview ( startgps ) ; 25 gototeiep=new Button ( this ) ; 26 gototeiep. settext ( "DIRECTIONS TO TEIEP" ) ; 27 mainlayout. addview ( gototeiep ) ; 28 29 30 startgps. s e t O n C l i c k L i s t e n e r (new OnClickListener ( ) 31 { 32 33 @Override 34 public void onclick ( View v ) { 35 i f ( l o c a t i o n l i s t e n e r==null ) 36 { 37 locationmanager = ( LocationManager ) 38 getsystemservice ( Context.LOCATION_SERVICE) ; 39 l o c a t i o n l i s t e n e r = new GpsLocation ( MainActivity. this, s t a t u s b a r ) ; 40 locationmanager. requestlocationupdates ( 41 LocationManager.GPS_PROVIDER, timebetween, meters, l o c a t i o n l i s t e n e r ) ; 42 startgps. settext ( "STOP GPS" ) ; 43 } 44 else 45 { 46 locationmanager. removeupdates ( l o c a t i o n l i s t e n e r ) ; 47 l o c a t i o n l i s t e n e r=null ; 48 startgps. settext ( "START GPS" ) ; 11
49 50 } 51 } 52 53 } ) ; 54 gototeiep. s e t O n C l i c k L i s t e n e r (new OnClickListener ( ) 55 { 56 57 @Override 58 public void onclick ( View v ) { 59 i f ( l o c a t i o n l i s t e n e r!= null ) 60 { 61 double t e i l a t =39.149709; double t e i l n g =20.978286; 62 63 S t r i n g l o c a t i o n=" http : / / maps. google. com/maps? saddr="+ 64 l o c a t i o n l i s t e n e r. g e t l a t i t u d e ()+", "+ 65 l o c a t i o n l i s t e n e r. g e t l o n g i t u d e ()+ 66 "&daddr="+t e i l a t+", "+t e i l n g ; 67 I n t e n t i n t e n t = new I n t e n t ( android. content. I n t e n t.action_view, 68 Uri. parse ( l o c a t i o n ) ) ; 69 i n t e n t. setclassname ( "com. google. android. apps. maps", 70 "com. google. android. maps. MapsActivity " ) ; 71 MainActivity. this. s t a r t A c t i v i t y ( i n t e n t ) ; 72 } 73 } 74 75 } ) ; 76 } 77 } 12