Corso Base ActionSMS Maurizio Cozzetto, Francesco Sarasini
Broadcast Receivers A broadcast receiver is a component that does nothing but receive and react to broadcast announcements (Intents). For a Broadcast Receiver to be enabled it needs to be registered. When registering a Broadcast Receiver you must use an Intent Filter to specify which Intents it is listening for.
Services The invisible workers of your application Android offers the Service class to create application components specifically to handle operations and functionality that should run invisibly, without a user interface.
ActionSMS: Argomenti trattati - Broadcast Receiver - Content provider - Activity - Service - Text To Speech - Text files - GPS Location - Device vibration -...
ActionSMS: Funzionalità - Lettura di un SMS in entrata tramite sintesi vocale - Esecuzione di compiti specifici indicati da remoto tramite SMS - Invio SMS
ActionSMS: Interfaccia
ActionSMS: Attesa di un SMS (1/4) <uses-permission android:name="android.permission.receive_sms" /> <uses-permission android:name="android.permission.read_sms" />... <receiver android:name="it.sarasini.android.actionsms.smsbroadcastreceiver"> <intent-filter> <action android:name="android.provider.telephony.sms_received" /> </intent-filter> </receiver>
ActionSMS: Attesa di un SMS (2/4) public class SMSBroadcastReceiver extends BroadcastReceiver { @Override public void onreceive(context context, Intent intent) { // Attenzione // ciò che viene effettuato qui va fatto velocemente! // leggo dal file di conf. se il servizio è abilitato ConfigFile config = new ConfigFile(context); if(!config.isenabled()) return;...
ActionSMS: Attesa di un SMS (3/4)... // Prendo il messaggio Bundle bundle = intent.getextras(); if(bundle == null) return; protocol data unit Object[] pduarray = (Object[]) bundle.get("pdus"); SmsMessage message = SmsMessage.createFromPdu((byte[])pduArray[0]);...
ActionSMS: Attesa di un SMS (4/4)... // lancio un servizio per gestire il lavoro Intent i = new Intent(); i.setaction("it.sarasini.android.actionsms.actionservice"); // Passo numero e testo del messaggio al servizio i.putextra( number, message.getoriginatingaddress()); i.putextra( text, message.getmessagebody().tostring()); } } if(context.startservice(serviceintent) == null) toast(impossibile avviare il servizio);
ActionSMS: Gestione di un SMS public class ActionService extends Service { @Override public int onstartcommand(intent intent, int flags, int startid) { // Prendo numero e testo che mi hanno passato Bundle extras = intent.getextras(); numero = extras.getstring( number ); testo = extras.getstring( text ); // Faccio qualcosa...... } } return START_STICKY;
ActionSMS: Sintesi vocale... extends Service implements OnInitListener { @Override public int onstartcommand(intent intent, int flags, int startid) { } tts = new TextToSpeech(getApplicationContext(), this);... @Override public void oninit(int status) { } } if(status == TextToSpeech.SUCCESS) tts.speak( Ciao, TextToSpeech.QUEUE_ADD, null);
ActionSMS: Dal numero al nome Uri rubrica = ContactsContract.PhoneLookup.CONTENT_FILTER_URI; Uri lookupuri = Uri.withAppendedPath(rubrica, number); Cursor idcursor = getcontentresolver().query(lookupuri, null, null, null, null); if(idcursor.movetofirst()) { int colonna = idcursor.getcolumnindexorthrow( ContactsContract.Contacts.DISPLAY_NAME); } String nome = idcursor.getstring(colonna); idcursor.close();
ActionSMS: Invio di un SMS <uses-permission android:name="android.permission.send_sms"/> String number = 123456789 ; String text = Ciao sono un sms! ; SmsManager smsmanager = SmsManager.getDefault(); smsmanager.sendtextmessage(number, null, text, null, null);
ActionSMS: Localizzazione GPS LocationManager locationmanager; String cls = Context.LOCATION_SERVICE; lm = (LocationManager) getsystemservice(cls); Location location = lm.getlastknownlocation(locationmanager.gps_provider); if(location == null) // Errore String lat = location.getlatitude(); String long = location.getlongitude();
ActionSMS: Good Vibrations String vibratorservice = Context.VIBRATOR_SERVICE; Vibrator vibrator = (Vibrator)getSystemService(vibratorService); // Vibra per 4000 ms vibrator.vibrate(4000);
ActionSMS: Dimostrazione