Android应用实例之----基于BroadCastReceiver的SD卡装载卸载实例!

大家好,经过国庆七天终于又和大家见面了,今天给大家分享的是基于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布局文件,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ImageView
  8. android:paddingTop="10px"
  9. android:id="@+id/usbstatus"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:src="@drawable/usb_android"
  13. />
  14. <LinearLayout
  15. android:orientation="horizontal"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_gravity="bottom"
  19. android:paddingTop="30px"
  20. >
  21. <ToggleButton
  22. android:id="@+id/mountsdcard"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_weight="1"
  26. android:textOn="卸载SD卡"
  27. android:textOff="装载SD卡"
  28. />
  29. <ToggleButton
  30. android:id="@+id/cancel"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_weight="1"
  34. android:textOff="取消"
  35. android:textOn="取消"
  36. />
  37. </LinearLayout>
  38. </LinearLayout>

第三步:新建一个BroadcastReceiver,命名为UsbBroadCastRecevier.java,代码如下:

  1. package com.smit.usbstorage;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. public class UsbBroadCastRecevierextends BroadcastReceiver {
  6. @Override
  7. public void onReceive(Context context, Intent intent) {
  8. // TODO Auto-generated method stub
  9. String action = intent.getAction();
  10. //当插上USB后启动UsbStorageActivity
  11. if(action.equals(Intent.ACTION_UMS_CONNECTED)){
  12. final Intent mIntent =new Intent();
  13. mIntent.setClass(context, UsbStorageActivity.class);
  14. mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  15. context.startActivity(mIntent);
  16. }
  17. }
  18. }

第四步:修改主核心程序UsbStorageActivity.java代码如下:

  1. package com.smit.usbstorage;
  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.ImageView;
  11. import android.widget.Toast;
  12. import android.widget.ToggleButton;
  13. import com.smit.usbstorage.R;
  14. public class UsbStorageActivityextends Activity implements OnClickListener{
  15. private ImageView mImageView;
  16. private ToggleButton mMountSdcard;
  17. private ToggleButton mCancel;
  18. //定义一个BroadcastReceiver当接收拔掉USB广播后,关闭当前Activity
  19. private BroadcastReceiver mBroadcastReceiver =new BroadcastReceiver() {
  20. @Override
  21. public void onReceive(Context context, Intent intent) {
  22. String action = intent.getAction();
  23. if(action.equals(Intent.ACTION_UMS_DISCONNECTED)){
  24. finish();
  25. }
  26. }
  27. };
  28. @Override
  29. public void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.main);
  32. setupViews();
  33. }
  34. //初始化工作
  35. private void setupViews(){
  36. mImageView = (ImageView)findViewById(R.id.usbstatus);
  37. mMountSdcard = (ToggleButton)findViewById(R.id.mountsdcard);
  38. mCancel = (ToggleButton)findViewById(R.id.cancel);
  39. //判断Sdcard是否存在,不存在按钮不可用
  40. if(!sdcardIsMounted()){
  41. mMountSdcard.setEnabled(false);
  42. }
  43. mMountSdcard.setOnClickListener(this);
  44. mCancel.setOnClickListener(this);
  45. //代码中注册BroadCastReceiver
  46. IntentFilter mIntentFilter = new IntentFilter();
  47. mIntentFilter.addAction(Intent.ACTION_UMS_DISCONNECTED);
  48. registerReceiver(mBroadcastReceiver, mIntentFilter);
  49. }
  50. //判断SD卡是否存在
  51. public boolean sdcardIsMounted(){
  52. if (android .os.Environment.getExternalStorageState().equals(
  53. android.os.Environment.MEDIA_MOUNTED)){
  54. return true;
  55. }else{
  56. return false;
  57. }
  58. }
  59. private boolean usbIsMounted(){
  60. boolean res = false;
  61. if(mMountSdcard.isChecked()){
  62. res = true;
  63. }
  64. return res;
  65. }
  66. private void toggleMountSdcard(){
  67. if(mMountSdcard.isChecked()){
  68. mountAsUsbStorage();
  69. }else{
  70. stopUsbStorage();
  71. }
  72. }
  73. //装载SD卡方法,我这里就没有具体实现
  74. private void mountAsUsbStorage() {
  75. Toast.makeText(this, "SD卡被装载", Toast.LENGTH_LONG).show();
  76. }
  77. //卸载SD卡方法
  78. private void stopUsbStorage() {
  79. Toast.makeText(this,"SD卡被卸载", Toast.LENGTH_LONG).show();
  80. }
  81. public void onClick(View v) {
  82. if(v == mMountSdcard){
  83. toggleMountSdcard();
  84. if(usbIsMounted()){
  85. mImageView.setImageResource(R.drawable.usb_android_connected);
  86. }else{
  87. mImageView.setImageResource(R.drawable.usb_android);
  88. }
  89. }else if(v == mCancel){
  90. finish();
  91. }
  92. }
  93. }

第五步:修改AndroidManifest.xml文件,代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.smit.usbstorage"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:icon="@drawable/icon" android:label="@string/app_name">
  7. <activity android:name=".UsbStorageActivity"
  8. android:theme="@android:style/Theme.NoTitleBar"
  9. 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" />

不信大家可以试试。lol~

Ok,今天晚上就先写到这里。大家有什么疑问的可以留言.....bye~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值