2011年9月28日 星期三

[Android] How to monitor sdcard mount/unmout?

In this example, we demo the sdcard mount/unmount monitor.
	public void onCreate() {
		super.onCreate();
		this.registerReceiver(this.myReceiver, getSDCardIntentFilter());
	}

	public IntentFilter getSDCardIntentFilter() {
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
		intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
		intentFilter.addDataScheme("file");
		return intentFilter;
	}

	private BroadcastReceiver myReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) 
		{
			String action = intent.getAction();
			if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED))
			{
			  // handle SD card unmounted
			}
			else if (action.equals(Intent.ACTION_MEDIA_MOUNTED))
			{
			  // handle SD card mounted
			}
		}
	};

沒有留言:

張貼留言