2011-09-26 14 views
5

im cercando di fare qualcosa di simile a questo: android: how to listen to "sd card removed unexpectedly" ma OnReceive di chi ascolta non viene mai chiamato, quando non ho sdcard montato o rimuovo sdcard. Ecco il codice.ascoltatore per la rimozione sdcard

public class MyClass1 extends Activity{ 
    BroadcastReceiver mSDCardStateChangeListener = null; 
    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    mSDCardStateChangeListener = MyClass2.registerSDCardStateChangeListener(this); 
    //some code which needs SDCard and throws unhandled exception if sdcard is not there 

} 

@Override 
    protected void onDestroy() 
    { 
     MyClass2.unRegisterSDCardStateChangeListener(this, mSDCardStateChangeListener); 
     super.onDestroy(); 
    } 



//in MyClass2 
public static BroadcastReceiver registerSDCardStateChangeListener(Activity act) { 

     BroadcastReceiver mSDCardStateChangeListener = new BroadcastReceiver() { 

      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       String action = arg1.getAction(); 
        if(action.equalsIgnoreCase(Intent.ACTION_MEDIA_REMOVED) 
          || action.equalsIgnoreCase(Intent.ACTION_MEDIA_UNMOUNTED) 
          || action.equalsIgnoreCase(Intent.ACTION_MEDIA_BAD_REMOVAL) 
          || action.equalsIgnoreCase(Intent.ACTION_MEDIA_EJECT)) 
        { 
         //i never come here ;(
        //do something 

        } 

      } 
     }; 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(Intent.ACTION_MEDIA_REMOVED); 
     filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED); 
     filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL); 
     filter.addAction(Intent.ACTION_MEDIA_EJECT); 
     filter.addDataScheme("file"); 
     act.registerReceiver(mSDCardStateChangeListener, filter); 
     return mSDCardStateChangeListener; 
} 

public static void unRegisterSDCardStateChangeListener(Activity act, BroadcastReceiver mSDCardStateChangeListener) 
    { 
     act.unregisterReceiver(mSDCardStateChangeListener); 
    } 

non voglio verificare se sdcard è presente o meno da se (android.os.Environment.getExternalStorageState(). Equals (android.os.Environment.MEDIA_MOUNTED)) ma utilizzare il ricevitore, invece. Qualsiasi aiuto è benvenuto. Grazie !.

+0

hai impostato le tue autorizzazioni nel file manifest? – Lukap

+0

@Lukap ho Android.permission.READ_PHONE_STATE nel mio manifest –

+0

@ con_9 Hai ragione, mi dispiace. – Caner

risposta

3

Ok penso che il codice che ho postato sia destinato all'azione &, non lo stato & funziona correttamente.

dalla documentazione:

azione android.content.Intent.ACTION_MEDIA_REMOVED Broadcast: supporto esterno è stato rimosso. Il percorso del punto di montaggio per il supporto rimosso è contenuto nel campo Intent.mData .

quindi quello che mi aspettavo (mi sbagliavo, vedere le prime due righe della questione), che, se io non sono SDCard (vale a dire che è stato rimosso in precedenza) e poi ho lanciare l'applicazione avrei ricevuto la chiamata sottintendendo che non ho la SDCard (lo so che stpid;)). L'intento sono le azioni (e non lo stato). Così se rimuovo la sdcard mentre l'app è attiva, ricevo il callback. Grazie per il tuo tempo Vegas.

Problemi correlati