Programmazione Android – Walkmind

CODICE ANDROID

Walkmind è un’applicazione Android sviluppata come prima versione qualche anno fa. Il cuore del software Walkmind è il player dei file audio. Il software deve estrarre il file audio che contiene la domanda e dopo un tempo determinato il corrispondente file audio contenente la risposta. Le opzioni implementate sono già numerose ed esaustive per poter studiare qualsiasi argomento o materia. L’applicazione Walkmind può essere scaricata gratuitamente dal google play
Logo Walkmind. Player audio di studio

APERTURA DEL PLAYER

Preparazione del Layout. Suddivisione di ogni domanda per argomento o materia. In apertura del Player il codice deve caricare nei vettori stringa e temporali tutti i dati salvati nell’ultima sessione. Ogni domanda e risposta viene abbinata alla materia assegnata. Le azioni scelte per gli oggetti creati nel menù sono implementate nella prima parte del file.
  1. // Autore
  2. // Ing. Paolo Sordi, via casilina n. 88 San Cesareo (Roma) 
  3. //
  4. package com.idros.walkmind;
  5.  
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13. import android.view.View;
  14. import android.app.AlertDialog;
  15. import android.content.Context;
  16. import java.util.*;
  17. import android.media.MediaPlayer;
  18. import android.media.MediaPlayer.OnCompletionListener;
  19. import java.util.Random;
  20. import java.text.DecimalFormat;
  21. import com.idros.walkmind.R;
  22. import android.os.PowerManager;
  23. import android.content.DialogInterface;
  24. import android.content.DialogInterface.*;
  25. import android.view.KeyEvent;
  26. import android.view.Menu;
  27. import android.view.MenuItem;
  28. import android.view.WindowManager;
  29. import android.content.BroadcastReceiver;
  30. import android.content.IntentFilter;
  31. import android.media.AudioManager;
  32. import android.bluetooth.BluetoothAdapter;
  33. import android.bluetooth.BluetoothDevice;
  34.  
  35. public class Player_mp3 extends Activity implements OnClickListener{
  36. 	public Timer timer; 
  37. 	public boolean aggiorna_edit_materia=false; 
  38. 	public short esegui_il_timer_task = 1; 	
  39. 	public short indice = -1;
  40. 	public short intervallo ;
  41. 	public short num_risposte_date = 0;
  42. 	public long debug_ritardo = 0;
  43. 	final short intervallo_salvataggio = 10;
  44. 	final short intervallo_spegnimento = 30;
  45. 	short conteggio_allo_spegnimento = 0;
  46. 	public short num_risposte_al_salvataggio = intervallo_salvataggio;
  47. 	public int length;
  48.     public long durata_wav;
  49. 	public TextView text_percorso, text_indice, text_durata, text_num_domande, text_save, textView7; 
  50. 	public MyTimerTask myTask;
  51. 	public String nomefile = "";
  52. 	public GLOBALCLASS GLOBALE;
  53. 	public MediaPlayer mp, mp_song , mp_falso;
  54. 	public Button button1, button2, button9;
  55. 	public String stringa1, stringa2, stringa3, stringa4, stringa5, stringa6, stringa7, stringa8; 
  56. 	public EditText editText1;
  57. 	private static PowerManager.WakeLock powerLock;
  58.  
  59.     public void onCreate(Bundle savedInstanceState) {
  60.     	myTask = null;
  61.     	MyTimerTask myTask = new MyTimerTask();
  62.     	GLOBALE = (GLOBALCLASS) getApplicationContext();
  63.         super.onCreate(savedInstanceState);
  64.     	stringa1 = getString(R.string.player_indice);  
  65.     	stringa2 = getString(R.string.player_percentuale); 
  66.     	stringa3 = getString(R.string.n_al_salvataggio); 
  67.     	stringa4 = getString(R.string.STRING_n_domande);  
  68.     	stringa5 = getString(R.string.player_durata);  
  69.     	stringa6 = getString(R.string.player_indice);  
  70.     	stringa7 = getString(R.string.player_file); 
  71.     	stringa8 = getString(R.string.player_time_pausa); 
  72.         setContentView(R.layout.form_player_mp3);
  73.         button2 = (Button) findViewById(R.id.button2);
  74.         button2.setOnClickListener(this);
  75.         button2.setEnabled(false);
  76.         button1 = (Button) findViewById(R.id.button1);
  77.         button1.setOnClickListener(this);
  78.         final Button button3 = (Button) findViewById(R.id.button3);
  79.         button3.setOnClickListener(this);
  80.         final Button button4 = (Button) findViewById(R.id.button4);
  81.         button4.setOnClickListener(this);
  82.         final Button button7 = (Button) findViewById(R.id.button7);
  83.         button7.setOnClickListener(this);
  84.         final Button button8 = (Button) findViewById(R.id.button8);
  85.         button8.setOnClickListener(this);
  86.         final Button button9 = (Button) findViewById(R.id.button9);
  87.         button9.setOnClickListener(this);
  88.         final Button button10 = (Button) findViewById(R.id.button10);
  89.         button10.setOnClickListener(this);
  90.         text_percorso = (TextView) findViewById(R.id.textView1);
  91. 	    text_percorso.setText(stringa7);   
  92.         text_indice = (TextView) findViewById(R.id.textView2);
  93. 	    text_indice.setText(stringa6);
  94.         text_num_domande = (TextView) findViewById(R.id.textView4);
  95.         text_num_domande.setText(stringa4 + GLOBALE.numero_elementi);
  96.         textView7 = (TextView) findViewById(R.id.textView7);
  97.         textView7.setText(".");
  98.         editText1 = (EditText) findViewById(R.id.editText1);
  99.         editText1.setText(".");
  100.         int i,j;
  101.         IntentFilter filter1, filter2, filter3, filter4;
  102.         filter1 = new IntentFilter("android.bluetooth.a2dp.profile.action.CONNECTION_STATE_CHANGED");
  103.         filter2 = new IntentFilter(android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECTED);
  104.         filter3 = new IntentFilter(android.bluetooth.BluetoothDevice.ACTION_ACL_CONNECTED);
  105.         filter4 = new IntentFilter(android.bluetooth.BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
  106.         this.registerReceiver(mReceiver, filter1);
  107.         this.registerReceiver(mReceiver, filter2);
  108.         this.registerReceiver(mReceiver, filter3);
  109.         this.registerReceiver(mReceiver, filter4);
  110.         PowerManager pM = (PowerManager) getSystemService(Context.POWER_SERVICE);
  111.         powerLock = pM.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Prevent sleeping");
  112.         powerLock.acquire();
  113.         boolean non_presente;
  114.         GLOBALE.numero_materie =0;
  115.         GLOBALE.vettore_elenco_materie_aux = new String[GLOBALE.numero_elementi];
  116.         GLOBALE.vettore_elenco_materie_ok_play = new boolean[GLOBALE.numero_elementi];
  117.         GLOBALE.vettore_elenco_materie_statistica = new short[GLOBALE.numero_elementi];
  118. // trova tutte le materie presenti
  119.         for (i=1; i< GLOBALE.numero_elementi; i++) {
  120.              non_presente = true;
  121.              if (GLOBALE.vettore_materia[i].equals("null") || GLOBALE.vettore_materia[i] == null || GLOBALE.vettore_materia[i].length()==0) {
  122.             	 GLOBALE.vettore_materia[i] = "*";
  123.              }    
  124.              for (j=0; j< GLOBALE.numero_materie; j++) { if (GLOBALE.vettore_elenco_materie_aux[j].equals(GLOBALE.vettore_materia[i])) { non_presente = false; GLOBALE.vettore_elenco_materie_statistica[j] ++; } } // if (non_presente && !GLOBALE.vettore_materia[i].equals("null") && GLOBALE.vettore_materia[i] != null && GLOBALE.vettore_materia[i].length()>0) {
  125.              if (non_presente && GLOBALE.vettore_materia[i].length()>0) {
  126.             	 GLOBALE.vettore_elenco_materie_aux[GLOBALE.numero_materie] = GLOBALE.vettore_materia[i];
  127.         		 GLOBALE.vettore_elenco_materie_statistica[GLOBALE.numero_materie] = 1;
  128.             	 GLOBALE.vettore_elenco_materie_ok_play[GLOBALE.numero_materie] = true;
  129.                	 if (GLOBALE.vettore_materia[i].equals("x1matem")) GLOBALE.vettore_elenco_materie_ok_play[GLOBALE.numero_materie] = false;
  130.                	 if (GLOBALE.vettore_materia[i].equals("x2fisic")) GLOBALE.vettore_elenco_materie_ok_play[GLOBALE.numero_materie] = false;
  131.                	 if (GLOBALE.vettore_materia[i].equals("x3elett")) GLOBALE.vettore_elenco_materie_ok_play[GLOBALE.numero_materie] = false;
  132.                	 if (GLOBALE.vettore_materia[i].equals("x4softw")) GLOBALE.vettore_elenco_materie_ok_play[GLOBALE.numero_materie] = false;
  133.             	 GLOBALE.numero_materie++;
  134.              }
  135.         }
  136.         boolean alfabeto_non_finito = true;
  137.         while (alfabeto_non_finito) {
  138.         	alfabeto_non_finito = false;
  139. 	        for  (i=0; i < GLOBALE.numero_materie -1; i++) { int sorteggio = GLOBALE.vettore_elenco_materie_aux[i].toString().compareTo(GLOBALE.vettore_elenco_materie_aux[i+1].toString()); if (sorteggio > 0)  
  140. 		        {  
  141. 		            short aux = GLOBALE.vettore_elenco_materie_statistica[i+1];
  142. 		            String aux_string = GLOBALE.vettore_elenco_materie_aux[i+1].toString();
  143. 		            boolean aux_boolean = GLOBALE.vettore_elenco_materie_ok_play[i+1];
  144. 		            GLOBALE.vettore_elenco_materie_statistica[i+1] = GLOBALE.vettore_elenco_materie_statistica[i];
  145. 		            GLOBALE.vettore_elenco_materie_aux[i+1] = GLOBALE.vettore_elenco_materie_aux[i]; 
  146. 		            GLOBALE.vettore_elenco_materie_ok_play[i+1] = GLOBALE.vettore_elenco_materie_ok_play[i];
  147. 		            GLOBALE.vettore_elenco_materie_statistica[i] = aux;
  148. 		            GLOBALE.vettore_elenco_materie_aux[i] = aux_string; 
  149. 		            GLOBALE.vettore_elenco_materie_ok_play[i] = aux_boolean;
  150. 		            alfabeto_non_finito = true;
  151. 		        	//a is smaller
  152. 		        } 
  153. 	        }
  154.         }
  155. // Statistica indice di estrazione 
  156.         GLOBALE.somma_vettore_estrazioni = 0;
  157.         for (i=1; i< GLOBALE.numero_elementi; i++) { short indice; indice = GLOBALE.vettore_domande[i]; GLOBALE.somma_vettore_estrazioni += 1/(float)GLOBALE.vettore_estrazione[indice]; } // System.out.println ("%%%%%%%%%%%%%YYYTimerTask-------S " ); Timer timer = new Timer(); if (GLOBALE.numero_elementi > 0) {
  158.         	intervallo = 1;
  159. 	        timer.schedule(myTask, 1000*intervallo);
  160. 	        intervallo = 15;
  161.         }
  162.         else
  163.         {
  164.         	GLOBALE.domanda_risposta = -1000;
  165.         }
  166. 		mp_falso = null;
  167. 		mp_falso = new MediaPlayer();
  168. 		String direttorio_wav, percorso_wav;
  169.  
  170. //-WAV--------------------------------------------------------------------------------------------------
  171. 		try {
  172. 			direttorio_wav =  GLOBALE.direttorio_SDCARD + GLOBALE.direttorio_file;
  173. 			percorso_wav = direttorio_wav + "beep_up.mp3";
  174. 			mp_falso.setDataSource(percorso_wav);
  175. 			mp_falso.prepare(); 
  176. 			mp_falso.start();
  177. 			mp_falso.pause();
  178. 		} 
  179. 		catch (Exception e)
  180. 		{
  181. 			System.out.println (">>>>>>>>>>>>>>Errore in playSound_x " ); //in caso di eccezione ...
  182. //			textView01.setText("errore mp3 pause");
  183. 		}
  184.     }
  185. 	@Override
  186. 	public boolean onCreateOptionsMenu(Menu menu) {
  187. 		// Inflate the menu; this adds items to the action bar if it is present.
  188. 		getMenuInflater().inflate(R.menu.main, menu);
  189. 		return true;
  190. 	}
  191.     @Override
  192.     public boolean onOptionsItemSelected(MenuItem item) {
  193.         // Handle presses on the action bar items
  194.     	int i;
  195.         switch (item.getItemId()) {
  196.             case R.id.item1:
  197.                 //openSearch();
  198.                 for (i=0; i< GLOBALE.numero_materie; i++) {
  199.                     GLOBALE.vettore_elenco_materie_ok_play[i] = false;
  200.                 }
  201. //                GLOBALE.vettore_elenco_materie_ok_play[GLOBALE.numero_materie-1] = true;
  202.                 return true;
  203.                // openSettings();
  204.             case R.id.item3:
  205.                 //openSearch();
  206.                 for (i=0; i< GLOBALE.numero_materie; i++) {
  207.                     GLOBALE.vettore_elenco_materie_ok_play[i] = true;
  208.                 }
  209.                 return true;
  210.                // openSettings();   
  211.             case R.id.item2:
  212.                 //openSearch();
  213.             	getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  214.                 return true;
  215.                // openSettings();  
  216.             case R.id.item4:
  217.                 //openSearch();
  218.             	AlertDialog.Builder miaAlert = new AlertDialog.Builder(this);
  219.        	        miaAlert.setTitle("Info");
  220.        	        miaAlert.setMessage("Question to save " + num_risposte_al_salvataggio + "\n Question to exit " +  (intervallo_spegnimento - conteggio_allo_spegnimento));
  221.        	        AlertDialog alert = miaAlert.create();
  222.        	        alert.show();
  223.                 return true;
  224.                // openSettings(); 
  225.             case R.id.item5:
  226.                 //openSearch();
  227.             	esegui_il_timer_task = 0;
  228.                 return true;
  229.                // openSettings(); 
  230.             case R.id.item6:
  231.                 //openSearch();
  232.             	esegui_il_timer_task = 1;
  233.         		myTask = null;
  234. 		  		MyTimerTask myTask2 = new MyTimerTask();
  235. 		  		Timer timer2 = new Timer();
  236. 	  			timer2.schedule(myTask2, 1000);	
  237.                 return true;
  238.                // openSettings(); 
  239.             case R.id.item7:
  240.                 //openSearch();
  241.             	esegui_il_timer_task = 2;
  242.         		myTask = null;
  243. 		  		MyTimerTask myTask1 = new MyTimerTask();
  244. 		  		Timer timer1 = new Timer();
  245. 	  			timer1.schedule(myTask1, 1000);	
  246.                 return true;
  247.             default:
  248.                 return super.onOptionsItemSelected(item);
  249.         }
  250.     }

TIMER DI ESECUZIONE DEI FILE AUDIO

Definizione della classe timertask. Creazione dei timer da rispettare per mandare in play i file audio
  1. // Autore
  2. // Ing. Paolo Sordi, via casilina n. 88 San Cesareo (Roma) 
  3. //
  4.     class MyTimerTask extends TimerTask {
  5.     	public boolean materia_da_mandare_in_play = false;
  6.     	public void run() {
  7.     		if(timer != null) {
  8.     		   timer.cancel();
  9.     		   timer.purge();
  10.     		   timer = null;
  11.         	}
  12.     		if (esegui_il_timer_task == 0) return;
  13.     		if (esegui_il_timer_task == 2) esegui_il_timer_task = 0;
  14.     		Random r = new Random();
  15.     		int i1;
  16. 	  		System.out.println ("-------TimerTask-------S "  ); 
  17. 	  		if (GLOBALE.domanda_risposta == 1){
  18. 	  			boolean non_trovato = true;
  19. 	  			num_risposte_date++;
  20. 	  			num_risposte_al_salvataggio--;
  21. 	  			conteggio_allo_spegnimento++;
  22. //	  			textView01.setText("." + conteggio_allo_spegnimento);
  23. /*	  	 
  24.  				vettore = new short [4];
  25. 	  			vettore [0] = GLOBALE.vettore_estrazione[indice];
  26. 	  			vettore [1] = GLOBALE.vettore_scalata[indice];
  27. 	  			vettore [2] = 1;
  28. 	  			vettore [3] = 1;
  29. 	  		  	GLOBALE.scrivi_file_informazioni(GLOBALE.vettore_nome_domande[indice], vettore);  		  	
  30. */			
  31. 	  			if (num_risposte_date % intervallo_salvataggio == 0) {
  32. 	  				GLOBALE.salvataggio_automatico();
  33. 	  				num_risposte_al_salvataggio = intervallo_salvataggio;
  34. 	  				playSound_x ("beep_save.mp3");
  35. 	  			}		
  36. 	  			if (conteggio_allo_spegnimento > intervallo_spegnimento) {
  37. 	  				salva_e_chiudi();
  38. 	  			}  			
  39. 	  			while (non_trovato){
  40. 	  				if (indice >-1) {
  41. //		  			  	GLOBALE.scrivi_file_informazioni(GLOBALE.vettore_nome_domande[indice], indice);
  42. 	  				}	
  43. 		  			i1 = r.nextInt(GLOBALE.numero_elementi-1) + 1;
  44. 		  			indice = GLOBALE.vettore_domande[i1];
  45. //		  			GLOBALE.leggi_file_informazioni(indice);
  46. //		  			System.out.println ("-controlla_se_la_materia_puo_andare_in_play- " + indice);
  47. 		  			if (controlla_se_la_materia_puo_andare_in_play()) 
  48. 		  			{	
  49. 		  				GLOBALE.vettore_scalata[indice]++;
  50.     		  			materia_da_mandare_in_play = true;
  51. 		  			}
  52. 		  			else
  53. 		  			{	
  54.     		  			materia_da_mandare_in_play = false;
  55. 		  			}
  56. //		  			System.out.println ("-controlla_se_la_materia_puo_andare_in_play- ");	
  57. //		  			System.out.println ("-------TimerTask-------Z " + i1  );
  58. 		  			if (GLOBALE.vettore_scalata[indice] >= GLOBALE.vettore_estrazione[indice] && materia_da_mandare_in_play) {
  59. 		  				non_trovato = false;
  60. 	  				}
  61. 	  			}
  62.   			}
  63. 	  		if (GLOBALE.domanda_risposta == 1 && controlla_se_la_materia_puo_andare_in_play()) 
  64. 	  		{	
  65. 	  			playSound();
  66. 	  		}
  67. 	  		else 
  68. 	  		{			
  69. 	  			if (GLOBALE.domanda_risposta == 0 ) playSound();
  70. 	  		}
  71. 	  		Player_mp3.this.runOnUiThread(new Runnable() {
  72. 	            @Override
  73. 	            public void run() {
  74. 	        		text_percorso.setText(stringa7 + nomefile );
  75. //	        		text_num_domande.setText(stringa4 + GLOBALE.numero_elementi + " - " + debug_ritardo	+ " - " + durata_wav);
  76. 	        		text_num_domande.setText(stringa4 + indice);
  77.  
  78. 	            	textView7.setText("Topic");
  79. 	            	if (aggiorna_edit_materia) {
  80. 	            		editText1.setText(GLOBALE.vettore_materia[indice]);
  81. 	            	}
  82. 	            	aggiorna_edit_materia = false;
  83. 	            	if (indice>=0 && GLOBALE.domanda_risposta == 0) {
  84. 		            	editText1.setText(GLOBALE.vettore_materia[indice]);
  85. 	            	}
  86.                 	calcola_percentuale (false); 
  87. 	            	aggiorna_durata();
  88. 	            }
  89. 	        });
  90. 	        if (GLOBALE.chiudi_applicazione) {
  91. 	    		GLOBALE.salvataggio_automatico();
  92. 	    	    if (powerLock != null && powerLock.isHeld())       powerLock.release(); 
  93. 	        	finish();
  94. 	        	System.exit(0);
  95. 	        }
  96. 	  		if (GLOBALE.domanda_risposta == 1) {
  97. // tempo tra la domanda e la risposta
  98. 	  	  		myTask = null;
  99. 		  		MyTimerTask myTask = new MyTimerTask();
  100. 		  		Timer timer = new Timer();
  101. 		  		debug_ritardo = intervallo + durata_wav + GLOBALE.vettore_ritardo_risposta[indice];
  102. 	  			timer.schedule(myTask, 1000*(intervallo + durata_wav + GLOBALE.vettore_ritardo_risposta[indice]));	
  103. 	  		}
  104. // tempo tra la fine di una risposta e una nuova domanda
  105. 	  		if (GLOBALE.domanda_risposta == 0) {
  106. 	  	  		myTask = null;
  107.  		  		MyTimerTask myTask = new MyTimerTask();
  108. 		  		Timer timer = new Timer();
  109. 		  		debug_ritardo = intervallo + durata_wav ;
  110. 	  			timer.schedule(myTask, 1000*(intervallo + durata_wav));
  111. 	  		}
  112.   	  	}
  113.   	}
  114.  
  115.  
  116.     public void salva_e_chiudi() {
  117. 		GLOBALE.salvataggio_automatico();
  118. 	    if (powerLock != null && powerLock.isHeld())       powerLock.release(); 
  119. 		finish();
  120. 		System.exit(0);
  121.     }

PROGETTO DEGLI EVENTI ALL’INTERNO DELL’ACTIVITY

Gestione degli eventi di click sui bottoni del layout
  1. // Autore
  2. // Ing. Paolo Sordi, via casilina n. 88 San Cesareo (Roma) 
  3. //
  4.  
  5.     public void onClick(View v) {
  6.         // TODO Auto-generated method stub
  7.         if ( v.getId() == R.id.button8) {
  8.             Intent form_intent2 = new Intent(getApplicationContext(), Menu_principale.class);
  9.             startActivity(form_intent2);
  10.         }
  11.         if ( v.getId() == R.id.button1) {
  12.         	GLOBALE.domanda_risposta = -1000;
  13.         	myTask = null;
  14.         	if(timer != null) {
  15.       		   timer.cancel();
  16.       		   timer.purge();
  17.       		   timer = null;
  18.           	}
  19. 			System.out.println ("STOP STOP STOP " ); //in caso di eccezione ...
  20. 			if (mp != null) {
  21. 				   if (mp.isPlaying()) {
  22. 	                    mp.stop();
  23. 	                }
  24. 			}
  25. 			button1.setEnabled(false);
  26. 			button2.setEnabled(true);
  27.         }
  28.         if ( v.getId() == R.id.button2) {
  29.         	GLOBALE.domanda_risposta = 1;
  30. 			System.out.println ("START START START " );
  31. 	  		myTask = null;
  32. 	  		if(timer != null) {
  33. 	     		   timer.cancel();
  34. 	     		   timer.purge();
  35. 	     		   timer = null;
  36. 	         	}
  37. 	  		MyTimerTask myTask = new MyTimerTask();
  38. 	  		Timer timer = new Timer();
  39.         	timer.schedule(myTask, 1000*2);
  40. 			button2.setEnabled(false);
  41. 			button1.setEnabled(true);
  42.         }       
  43.         if ( v.getId() == R.id.button3) {
  44.     		GLOBALE.vettore_estrazione[indice]++;
  45.     		calcola_percentuale (true);
  46.         }
  47.         if ( v.getId() == R.id.button4) {
  48.     		if (GLOBALE.vettore_estrazione[indice] > 1) {
  49.     			GLOBALE.vettore_estrazione[indice]--;
  50.     			calcola_percentuale (true);
  51.         	}
  52.     	}        
  53. /*        if ( v.getId() == R.id.button6) {
  54.         	GLOBALE.vettore_ritardo_risposta[indice] += 1;
  55.      		aggiorna_durata ();
  56.         }
  57.         if ( v.getId() == R.id.button5) {
  58.      		if (GLOBALE.vettore_ritardo_risposta[indice] > 1) {
  59.      			GLOBALE.vettore_ritardo_risposta[indice] -=1 ;
  60.      			aggiorna_durata ();
  61.          	}
  62.      	}   
  63. */
  64.         if ( v.getId() == R.id.button9) {
  65.         	textView7.setText("Topic new");
  66.         	GLOBALE.vettore_materia[indice] = editText1.getText().toString();
  67.         	inserisci_la_nuova_materia_se_non_esiste ();
  68.      	}  
  69.         if ( v.getId() == R.id.button7) {
  70. /*
  71.         	mp.pause();
  72.     	    length=mp.getCurrentPosition();
  73. 	  		if (GLOBALE.domanda_risposta == 1) GLOBALE.domanda_risposta = -1001;
  74. 	  		if (GLOBALE.domanda_risposta == 0) GLOBALE.domanda_risposta = -1000;
  75. */
  76.         	scelta_materia();
  77.         	editText1.setText(GLOBALE.vettore_materia[indice]);
  78.         }  
  79.         if ( v.getId() == R.id.button10) {
  80.  /*        	 
  81.     	     mp.pause();
  82.     	     length=mp.getCurrentPosition();
  83.  	  		 if (GLOBALE.domanda_risposta == 1) GLOBALE.domanda_risposta = -1001;
  84.  	  		 if (GLOBALE.domanda_risposta == 0) GLOBALE.domanda_risposta = -1000;
  85. */
  86.         	 showMultipleChoiceItems("Topics");    
  87.         }  
  88.     }

CONNESSIONE CON L’AURICOLARE BLUETOOTH

Questa parte del software è stata realizzata quasi subito dopo il framework principale. L’obiettivo del programma è infatti quello di poter studiare anche quando manualmente si è impegnati in altre attività L’applicazione può essere controllata dai pulsanti di un auricolare bluetooth. Una volta stabilita la connessione la pressione dei pulsanti è un evento che corrisponde alla digitazione di un tasto della tastier (evento keydown)
  1. // Autore
  2. // Ing. Paolo Sordi, via casilina n. 88 San Cesareo (Roma) 
  3. //
  4.  
  5.  //The BroadcastReceiver that listens for bluetooth broadcasts
  6.     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  7.         @Override
  8.         public void onReceive(Context context, Intent intent) {
  9.             String action = intent.getAction();
  10. //            Toast.makeText(BlueDetectSrv.this, "BT change received !", Toast.LENGTH_LONG).show();
  11.             if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  12. //                Toast.makeText(BlueDetectSrv.this, device.getName() + " Device found", Toast.LENGTH_LONG).show();
  13.             	editText1.setText("1");
  14.             }
  15.             else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
  16.             	editText1.setText("2");
  17.              }
  18.             else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
  19.                	editText1.setText("3");
  20.              }
  21.             else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
  22.                	editText1.setText("4");
  23.                	salva_e_chiudi();
  24.             }           
  25.         }
  26.     };
  27. // --------------------------------------------------------
  28.   public boolean onKeyDown(int keyCode, KeyEvent event) {
  29. //    	stringa4.setText("-" + keyCode);
  30.         if (keyCode == 127 || keyCode == 84) {
  31.         	GLOBALE.vettore_estrazione[indice]++;
  32.     		calcola_percentuale (true);
  33.     		playSound_x ("beep_up.mp3");
  34.         }
  35.         if (keyCode == 88) {
  36.              	GLOBALE.vettore_estrazione[indice]=1;
  37.         		calcola_percentuale (true);
  38.         		playSound_x ("beep_uno.mp3");
  39.             }
  40.         if (keyCode == 87) {
  41.         	GLOBALE.vettore_materia[indice]= "xbug";
  42.         	editText1.setText(GLOBALE.vettore_materia[indice]);
  43.  
  44.     		playSound_x ("beep_bug.mp3");
  45.         }
  46.         return super.onKeyDown(keyCode, event);
  47.     }
L’esecuzione del file audio avviene in una subroutine specifica che deve innanzitutto caricare il file audio dal dispositivo prepararne l’esecuzione e mandarlo in play.
  1. // Autore
  2. // Ing. Paolo Sordi, via casilina n. 88 San Cesareo (Roma) 
  3. //
  4.  
  5. 	public void playSound (){
  6. 		String percorso_wav;
  7. 		String direttorio_wav;
  8.  
  9. //-WAV--------------------------------------------------------------------------------------------------
  10. 		GLOBALE.vettore_scalata[indice] = 0;
  11. 		System.out.println ("Playsound" ); //in caso di eccezione ...
  12. 		direttorio_wav =  GLOBALE.direttorio_SDCARD + GLOBALE.direttorio_file;
  13. 		nomefile = GLOBALE.vettore_nome_risposte[indice];
  14. 		GLOBALE.scrivi_stringa_file_cronologia("nomefile: " + nomefile);
  15. 		if (GLOBALE.vettore_ritardo_risposta[indice] == -45645) 
  16. 					    {   
  17. 						    percorso_wav = direttorio_wav + nomefile;
  18. 							try {
  19. 								mp = new MediaPlayer();
  20. 								mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK); 
  21. 								mp.setDataSource(percorso_wav);
  22. 								mp.prepare();
  23. 						        durata_wav = (long)mp.getDuration();
  24. 						        durata_wav = durata_wav /1000;
  25. 				                mp.release();
  26. 				                mp = null;
  27.  
  28. 				// passiamo il file che deve essere in res
  29. 				// creiamo il player passandogli l'inputstream e il tipo di file
  30. 							    GLOBALE.vettore_ritardo_risposta[indice] = (short)durata_wav;
  31. 							}
  32. 							catch (Exception e)
  33. 							{
  34. 								text_percorso.setText(stringa7 + nomefile + " error1 ...");
  35. 							    GLOBALE.vettore_ritardo_risposta[indice] = 1;
  36. 								System.out.println ("sfore in playSound " ); //in caso di eccezione ...
  37. 								GLOBALE.scrivi_stringa_file_cronologia("E1 nomefile: " + nomefile);
  38.  
  39. 				 //           	text_percorso.setText(stringa7 + nomefile + " - Error");
  40. 							}
  41. 					    }
  42. 	    if (GLOBALE.domanda_risposta == 1) {
  43. 	       direttorio_wav =  (GLOBALE.direttorio_SDCARD + GLOBALE.direttorio_file);
  44. 	       nomefile = GLOBALE.vettore_nome_domande[indice];
  45. 	    }
  46. 	    GLOBALE.domanda_risposta += 1;
  47. 	    if (GLOBALE.domanda_risposta > 1) {
  48. 	    	GLOBALE.domanda_risposta = 0;
  49. 	    }
  50. 	    percorso_wav = direttorio_wav + nomefile;
  51. 		try {
  52. 			mp = new MediaPlayer();
  53. 			mp.setDataSource(percorso_wav);
  54. //			mp.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
  55. 			mp.prepare();
  56.             durata_wav = (long)mp.getDuration();
  57.             durata_wav = durata_wav /1000;
  58. 	        mp.start();
  59. //            Toast.makeText(getBaseContext(), "Playing", Toast.LENGTH_SHORT).show();
  60. 	        mp.setOnCompletionListener(new OnCompletionListener() {
  61. 	            @Override
  62. 	            public void onCompletion(MediaPlayer mp) {
  63. 	                mp.release();
  64. 	                mp = null;
  65. 	            }
  66. 	        });
  67. //text_percorso.setText(stringa7 + nomefile + " [" + durata_wav + " s]");
  68. //passiamo il file che deve essere in res
  69. //creiamo il player passandogli l'inputstream e il tipo di file
  70.  
  71. 		}
  72. 		catch (Exception e)
  73. 		{
  74. 			text_percorso.setText(stringa7 + nomefile + " error2 ...");
  75. 			System.out.println ("Errore in playSound " ); //in caso di eccezione ...
  76. 			GLOBALE.scrivi_stringa_file_cronologia("E2 nomefile: " + nomefile);
  77.  
  78. 		}
  79. 	}