大家好,经过国庆七天终于又和大家见面了,今天给大家分享的是基于BroadCastReceiver的SD卡装载卸载实例.
Android设备默认的是当我们插上USB和电脑相连接时,在Android设备状态栏上会发一条通知信息,当我们点击这条消息时,会出现一个对话框有"装载SD卡"和"取消"两个按钮,当我们点击装载时,我们的SD卡将会变成U盘一样,我们通过电脑可以对SD卡进行操作。
但是我们客户认为插上USB以后以通知的形式提示用户,这样不智能,他们的需求是当我们插入USB后,就会弹出一个窗口,让用户选择装载SD卡或者不装载。
当我拿到这个需求时,我首先想到了去改framework,当framework改得小有模样时,突然想到自己写个应用实现这个功能是多么的简单,也就是利用到了BroadCastReceiver这个组件,当然我这个应用是集成到了System/app下的,并且是在Launcher应用列表中看不到的。
BroadCastReceiver是Android中重要的五大组件之一,当然实现BroadCastReceiver有两种方法:一种是我们写一个类继承BroadCastReceiver并在AndroidManifest.xml文件中注册;另一种方法是在代码中直接注册BroadCastReceiver。
为了便于大家理解,我简单写了一个实例,希望对大家有所帮助,下面是具体步骤:
第一步:新建一个Android工程,命名为UsbStorage。目录结构如下:
第二步:修改main.xml布局文件,代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:paddingTop="10px" android:id="@+id/usbstatus" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/usb_android" /> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:paddingTop="30px" > <ToggleButton android:id="@+id/mountsdcard" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textOn="卸载SD卡" android:textOff="装载SD卡" /> <ToggleButton android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textOff="取消" android:textOn="取消" /> </LinearLayout> </LinearLayout>
第三步:新建一个BroadcastReceiver,命名为UsbBroadCastRecevier.java,代码如下:
package com.smit.usbstorage; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class UsbBroadCastRecevier extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); //当插上USB后启动UsbStorageActivity if(action.equals(Intent.ACTION_UMS_CONNECTED)){ final Intent mIntent = new Intent(); mIntent.setClass(context, UsbStorageActivity.class); mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(mIntent); } } }
第四步:修改主核心程序UsbStorageActivity.java代码如下:
package com.smit.usbstorage; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.Toast; import android.widget.ToggleButton; import com.smit.usbstorage.R; public class UsbStorageActivity extends Activity implements OnClickListener{ private ImageView mImageView; private ToggleButton mMountSdcard; private ToggleButton mCancel; //定义一个BroadcastReceiver当接收拔掉USB广播后,关闭当前Activity private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if(action.equals(Intent.ACTION_UMS_DISCONNECTED)){ finish(); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setupViews(); } //初始化工作 private void setupViews(){ mImageView = (ImageView)findViewById(R.id.usbstatus); mMountSdcard = (ToggleButton)findViewById(R.id.mountsdcard); mCancel = (ToggleButton)findViewById(R.id.cancel); //判断Sdcard是否存在,不存在按钮不可用 if(!sdcardIsMounted()){ mMountSdcard.setEnabled(false); } mMountSdcard.setOnClickListener(this); mCancel.setOnClickListener(this); //代码中注册BroadCastReceiver IntentFilter mIntentFilter = new IntentFilter(); mIntentFilter.addAction(Intent.ACTION_UMS_DISCONNECTED); registerReceiver(mBroadcastReceiver, mIntentFilter); } //判断SD卡是否存在 public boolean sdcardIsMounted(){ if (android .os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED)){ return true; }else{ return false; } } private boolean usbIsMounted(){ boolean res = false; if(mMountSdcard.isChecked()){ res = true; } return res; } private void toggleMountSdcard(){ if(mMountSdcard.isChecked()){ mountAsUsbStorage(); }else{ stopUsbStorage(); } } //装载SD卡方法,我这里就没有具体实现 private void mountAsUsbStorage() { Toast.makeText(this, "SD卡被装载", Toast.LENGTH_LONG).show(); } //卸载SD卡方法 private void stopUsbStorage() { Toast.makeText(this, "SD卡被卸载", Toast.LENGTH_LONG).show(); } public void onClick(View v) { if(v == mMountSdcard){ toggleMountSdcard(); if(usbIsMounted()){ mImageView.setImageResource(R.drawable.usb_android_connected); }else{ mImageView.setImageResource(R.drawable.usb_android); } }else if(v == mCancel){ finish(); } } }
第五步:修改AndroidManifest.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.smit.usbstorage" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".UsbStorageActivity" android:theme="@android:style/Theme.NoTitleBar" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".UsbBroadCastRecevier"> <intent-filter> <action android:name="android.intent.action.UMS_CONNECTED"></action> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>
第六步:运行上述工程,当我们插上USB后启动UsbStorageActivity,效果如下:
点击装载SD卡按钮:
当拔掉USB或者点击取消按钮,该应用关闭。
当然我们这个应用如果按以上方法是必然出现在应用列表中的,而这个应用只能是在插上USB后才启动,用户不能自启动,所以我们需要将这个应用隐藏起来,这里其实很简单,只要将第五步中的第12行代码去掉即可,即:
<category android:name="android.intent.category.LAUNCHER" />
不信大家可以试试。lol~
Ok,今天晚上就先写到这里。大家有什么疑问的可以留言.....bye~