zoukankan      html  css  js  c++  java
  • Android-监听sdcard状态

    public class MyService extends Service {
    
        private static final String TAG = "MyService";
        File imagepath;
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.i(TAG, "服务启动...");
            // 在IntentFilter中选择你要监听的行为
            IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);// sd卡被插入,且已经挂载
            intentFilter.setPriority(1000);// 设置最高优先级
            intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);// sd卡存在,但还没有挂载
            intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);// sd卡被移除
            intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);// sd卡作为 USB大容量存储被共享,挂载被解除
            intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);// sd卡已经从sd卡插槽拔出,但是挂载点还没解除
            intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);// 开始扫描
            intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);// 扫描完成
            intentFilter.addDataScheme("file");
            registerReceiver(broadcastRec, intentFilter);// 注册监听函数
            Log.i(TAG, "sd状态改变");
        }
    
        private final BroadcastReceiver broadcastRec = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equals("android.intent.action.MEDIA_MOUNTED"))// SD
                // 卡已经成功挂载
                {
                    imagepath = android.os.Environment.getExternalStorageDirectory();// 你的SD卡路径
                    Toast.makeText(MyService.this, "我的卡已经成功挂载", 0).show();
                } else if (action.equals("android.intent.action.MEDIA_REMOVED")// 各种未挂载状态
                        || action.equals("android.intent.action.ACTION_MEDIA_UNMOUNTED")
                        || action.equals("android.intent.action.ACTION_MEDIA_BAD_REMOVAL")) {
                    imagepath = android.os.Environment.getDataDirectory();// 你的本地路径
                    Toast.makeText(MyService.this, "我的各种未挂载状态", 0).show();
                }else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)){//开始扫描
                    Toast.makeText(MyService.this, "开始扫描...", 0).show();
                }else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)){//扫描完成
                    Toast.makeText(MyService.this, "扫描完成...", 0).show();
                }else if (action.equals(Intent.ACTION_MEDIA_SHARED)){//扩展介质的挂载被解除 (unmount)。因为它已经作为 USB 大容量存储被共享
                    Toast.makeText(MyService.this, " USB 大容量存储被共享...", 0).show();
                }else {
                    Toast.makeText(MyService.this, "其他状态...", 0).show();
                }
                Log.i(TAG, "imagepath---" + imagepath);
            }
        };
        
        public void onDestroy() {
            unregisterReceiver(broadcastRec);//取消注册
        };
    
    }
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.ls"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk android:minSdkVersion="10" />
    
        <!-- 在SDCard中创建与删除文件权限 -->
        <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
        <!-- 往SDCard写入数据权限 -->
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".Android_sdcActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            
            
             <!-- 注册服务 -->
            <service android:name=".service.MyService">
                <intent-filter>
                    <action android:name="cn.ls.service.myservice.action" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </service>
    
            <!-- 在SDCard中创建与删除文件权限 -->
        </application>
    
    </manifest>

    本文转自:http://blog.csdn.net/yigelangmandeshiren/article/details/8145059

  • 相关阅读:
    mysql 历史版本下载
    mysql 5.7 版本 You must reset your password using ALTER USER statement before executing this statement报错处理
    5.7 zip 版本的安装 以及遇到的坑
    mysql 5.6zip版本的卸载与5.7 zip 版本的安装
    mysql数据库的备份与还原
    本地Navicat连接docker里的mysql
    docker修改数据库密码
    docker 在push镜像到本地registry出现的500 Internal Server Error
    linux 没有界面内容显示不全解决办法
    json与map互相转换
  • 原文地址:https://www.cnblogs.com/sishuiliuyun/p/4250618.html
Copyright © 2011-2022 走看看