GIS的学习(四十四)osmdroid sdcard检查

     在许多应用中使用到sdcard的检查,在osmdroid当下载比较大的文件时候,需要检查sdcard是否挂载,

在android应用注册一个广播,监听事件是否为挂载或者卸载sdcard的事件,当卸载或者加载sdcard给出提示。

 

重点代码如下:

	/**
	 * This broadcast receiver will recheck the sd card when the mount/unmount messages happen
	 *
	 */
	private class MyBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(final Context aContext, final Intent aIntent) {

			final String action = aIntent.getAction();

			checkSdCard();

			if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
				onMediaMounted();
			} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
				onMediaUnmounted();
			}
		}
	}

 

检查方法为:

	private void checkSdCard() {
		final String state = Environment.getExternalStorageState();
		logger.info("sdcard state: " + state);
		mSdCardAvailable = Environment.MEDIA_MOUNTED.equals(state);
	}

 

 

完整代码如下:

package org.osmdroid.tileprovider.modules;

import org.osmdroid.tileprovider.IRegisterReceiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;

public abstract class MapTileFileStorageProviderBase extends MapTileModuleProviderBase {

	private static final Logger logger = LoggerFactory.getLogger(MapTileFileStorageProviderBase.class);

	/** whether the sdcard is mounted read/write */
	private boolean mSdCardAvailable = true;

	private final IRegisterReceiver mRegisterReceiver;
	private MyBroadcastReceiver mBroadcastReceiver;

	public MapTileFileStorageProviderBase(final IRegisterReceiver pRegisterReceiver,
			final int pThreadPoolSize, final int pPendingQueueSize) {
		super(pThreadPoolSize, pPendingQueueSize);

		checkSdCard();

		mRegisterReceiver = pRegisterReceiver;
		mBroadcastReceiver = new MyBroadcastReceiver();

		final IntentFilter mediaFilter = new IntentFilter();
		mediaFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
		mediaFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
		mediaFilter.addDataScheme("file");
		pRegisterReceiver.registerReceiver(mBroadcastReceiver, mediaFilter);
	}

	private void checkSdCard() {
		final String state = Environment.getExternalStorageState();
		logger.info("sdcard state: " + state);
		mSdCardAvailable = Environment.MEDIA_MOUNTED.equals(state);
	}

	protected boolean getSdCardAvailable() {
		return mSdCardAvailable;
	}

	@Override
	public void detach() {
		if (mBroadcastReceiver != null) {
			mRegisterReceiver.unregisterReceiver(mBroadcastReceiver);
			mBroadcastReceiver = null;
		}
		super.detach();
	}

	protected void onMediaMounted() {
		// Do nothing by default. Override to handle.
	}

	protected void onMediaUnmounted() {
		// Do nothing by default. Override to handle.
	}

	/**
	 * This broadcast receiver will recheck the sd card when the mount/unmount messages happen
	 *
	 */
	private class MyBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(final Context aContext, final Intent aIntent) {

			final String action = aIntent.getAction();

			checkSdCard();

			if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
				onMediaMounted();
			} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
				onMediaUnmounted();
			}
		}
	}
}

 

你可能感兴趣的:(osmdroid)