今天给大家分享的是基于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布局文件,代码如下:
01
<?xml version="1.0" encoding="utf-8"?>
02
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03
android:orientation="vertical"
04
android:layout_width="fill_parent"
05
android:layout_height="fill_parent"
06
>
07
<ImageView
08
android:paddingTop="10px"
09
android:id="@+id/usbstatus"
10
android:layout_width="fill_parent"
11
android:layout_height="wrap_content"
12
android:src="@drawable/usb_android"
13
/>
14
15
<LinearLayout
16
android:orientation="horizontal"
17
android:layout_width="fill_parent"
18
android:layout_height="wrap_content"
19
android:layout_gravity="bottom"
20
android:paddingTop="30px"
21
>
22
<ToggleButton
23
android:id="@+id/mountsdcard"
24
android:layout_width="wrap_content"
25
android:layout_height="wrap_content"
26
android:layout_weight="1"
27
android:textOn="卸载SD卡"
28
android:textOff="装载SD卡"
29
/>
30
<ToggleButton
31
android:id="@+id/cancel"
32
android:layout_width="wrap_content"
33
android:layout_height="wrap_content"
34
android:layout_weight="1"
35
android:textOff="取消"
36
android:textOn="取消"
37
/>
38
</LinearLayout>
39
</LinearLayout>
第三步:新建一个BroadcastReceiver,命名为UsbBroadCastRecevier.java,代码如下:
01
package com.smit.usbstorage;
02
import android.content.BroadcastReceiver;
03
import android.content.Context;
04
import android.content.Intent;
05
public class UsbBroadCastRecevier extends BroadcastReceiver {
06
@Override
07
public void onReceive(Context context, Intent intent) {
08
// TODO Auto-generated method stub
09
String action = intent.getAction();
10
//当插上USB后启动UsbStorageActivity
11
if(action.equals(Intent.ACTION_UMS_CONNECTED)){
12
13
final Intent mIntent = new Intent();
14
mIntent.setClass(context, UsbStorageActivity.class);
15
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
16
context.startActivity(mIntent);
17
}
18
}
19
20
}
第四步:修改主核心程序UsbStorageActivity.java代码如下:
001
package com.smit.usbstorage;
002
import android.app.Activity;
003
import android.content.BroadcastReceiver;
004
import android.content.Context;
005
import android.content.Intent;
006
import android.content.IntentFilter;
007
import android.os.Bundle;
008
import android.view.View;
009
import android.view.View.OnClickListener;
010
import android.widget.ImageView;
011
import android.widget.Toast;
012
import android.widget.ToggleButton;
013
import com.smit.usbstorage.R;
014
public class UsbStorageActivity extends Activity implements OnClickListener{
015
016
private ImageView mImageView;
017
private ToggleButton mMountSdcard;
018
private ToggleButton mCancel;
019
//定义一个BroadcastReceiver当接收拔掉USB广播后,关闭当前Activity
020
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
021
@Override
022
public void onReceive(Context context, Intent intent) {
023
String action = intent.getAction();
024
if(action.equals(Intent.ACTION_UMS_DISCONNECTED)){
025
finish();
026
}
027
}
028
};
029
@Override
030
public void onCreate(Bundle savedInstanceState) {
031
super.onCreate(savedInstanceState);
032
setContentView(R.layout.main);
033
setupViews();
034
}
035
036
//初始化工作
037
private void setupViews(){
038
mImageView = (ImageView)findViewById(R.id.usbstatus);
039
mMountSdcard = (ToggleButton)findViewById(R.id.mountsdcard);
040
mCancel = (ToggleButton)findViewById(R.id.cancel);
041
//判断Sdcard是否存在,不存在按钮不可用
042
if(!sdcardIsMounted()){
043
mMountSdcard.setEnabled(false);
044
}
045
mMountSdcard.setOnClickListener(this);
046
mCancel.setOnClickListener(this);
047
048
//代码中注册BroadCastReceiver
049
IntentFilter mIntentFilter = new IntentFilter();
050
mIntentFilter.addAction(Intent.ACTION_UMS_DISCONNECTED);
051
registerReceiver(mBroadcastReceiver, mIntentFilter);
052
}
053
//判断SD卡是否存在
054
public boolean sdcardIsMounted(){
055
if (android .os.Environment.getExternalStorageState().equals(
056
android.os.Environment.MEDIA_MOUNTED)){
057
return true;
058
}else{
059
return false;
060
}
061
}
062
063
064
private boolean usbIsMounted(){
065
boolean res = false;
066
if(mMountSdcard.isChecked()){
067
res = true;
068
}
069
return res;
070
}
071
072
private void toggleMountSdcard(){
073
if(mMountSdcard.isChecked()){
074
mountAsUsbStorage();
075
}else{
076
stopUsbStorage();
077
}
078
}
079
080
//装载SD卡方法,我这里就没有具体实现
081
private void mountAsUsbStorage() {
082
Toast.makeText(this, "SD卡被装载", Toast.LENGTH_LONG).show();
083
}
084
085
//卸载SD卡方法
086
private void stopUsbStorage() {
087
Toast.makeText(this, "SD卡被卸载", Toast.LENGTH_LONG).show();
088
}
089
090
public void onClick(View v) {
091
if(v == mMountSdcard){
092
toggleMountSdcard();
093
if(usbIsMounted()){
094
mImageView.setImageResource(R.drawable.usb_android_connected);
095
}else{
096
mImageView.setImageResource(R.drawable.usb_android);
097
}
098
}else if(v == mCancel){
099
finish();
100
}
101
}
102
103
}
第五步:修改AndroidManifest.xml文件,代码如下:
01
<?xml version="1.0" encoding="utf-8"?>
02
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03
package="com.smit.usbstorage"
04
android:versionCode="1"
05
android:versionName="1.0">
06
<application android:icon="@drawable/icon" android:label="@string/app_name">
07
<activity android:name=".UsbStorageActivity"
08
android:theme="@android:style/Theme.NoTitleBar"
09
android:label="@string/app_name">
10
<intent-filter>
11
<action android:name="android.intent.action.MAIN" />
12
<category android:name="android.intent.category.LAUNCHER" />
13
</intent-filter>
14
</activity>
15
<receiver android:name=".UsbBroadCastRecevier">
16
<intent-filter>
17
<action android:name="android.intent.action.UMS_CONNECTED"></action>
18
</intent-filter>
19
</receiver>
20
</application>
21
<uses-sdk android:minSdkVersion="7" />
22
</manifest>
第六步:运行上述工程,当我们插上USB后启动UsbStorageActivity,效果如下:
点击装载SD卡按钮:
当拔掉USB或者点击取消按钮,该应用关闭。
当然我们这个应用如果按以上方法是必然出现在应用列表中的,而这个应用只能是在插上USB后才启动,用户不能自启动,所以我们需要将这个应用隐藏起来,这里其实很简单,只要将第五步中的第12行代码去掉即可,即:
1
<category android:name="android.intent.category.LAUNCHER" />