Android app instance-load and unload an instance based on broadcastreceiver SD card!

Source: Internet
Author: User

Hello everyone, I finally met you again after seven days of the national day. Today I want to share with you the broadcastreceiver-based SD card loading and unloading instance.

By default, the Android device sends a notification message on the status bar of the Android device when we plug in USB to connect to the computer. When we click this message, there will be a dialog box with the "load SD card" and "cancel" buttons. When we click the "LOAD" button, our SD card will become the same as a USB flash disk. We can operate on the SD card through the computer.

However, our customers think that after USB is inserted, they will prompt the user in the form of a notification. This is not smart. Their requirement is that a window will pop up when we insert the USB, let the user choose to load the SD card or not to load it.

When I got this requirement, I first thought about modifying the framework. When the framework was changed slightly, I suddenly thought of how simple it was to write an application to implement this function, that is, the broadcastreceiver component is used. Of course, my application is integrated into the system/APP and cannot be seen in the launcher Application List.

Broadcastreceiver is one of the five important components in Android. Of course, there are two ways to implement broadcastreceiver: one is to write a class that inherits broadcastreceiver and runs it in androidmanifest. XML file registration; another method is to directly register broadcastreceiver in the code.

To make it easier for you to understand, I have simply written an instance and want to help you. The following are the specific steps:

Step 1: Create an android project named USB storage. The directory structure is as follows:

Step 2: Modify the main. xml layout file. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <imageview <br/> Android: paddingtop = "10px" <br/> Android: Id = "@ + ID/usbstatus" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: src = "@ drawable/usb_android" <br/> </P> <p> <linearlayout <br/> Android: orientation = "horizontal" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_gravity = "bottom" <br/> Android: paddingtop = "30px" <br/> <togglebutton <br/> Android: id = "@ + ID/mountsdcard" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_weight = "1" <br/> Android: texton = "Uninstall SD card" <br/> Android: textoff = "load SD card" <br/> <togglebutton <br/> Android: Id = "@ + ID/cancel" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_weight = "1" <br/> Android: textoff = "cancel" <br/> Android: texton = "cancel" <br/> </linearlayout> <br/>

Step 3: Create a New broadcastreceiver named usbbroadcastrecevier. java. The Code is as follows:

Package COM. smit. USB storage; <br/> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> public class usbbroadcastrecevier extends broadcastreceiver {<br/> @ override <br/> Public void onreceive (context, intent) {<br/> // todo auto-generated method stub <br/> string action = intent. getaction (); <br/> // start usbstorageactivity after USB is inserted <br/> If (action. equals (intent. action_ums_connected) {</P> <p> final intent mintent = new intent (); <br/> mintent. setclass (context, usbstorageactivity. class); <br/> mintent. setflags (intent. flag_activity_new_task); <br/> context. startactivity (mintent); <br/>}</P> <p >}< br/>

Step 4: Modify the main core program usbstorageactivity. Java code as follows:

Package COM. smit. USB storage; <br/> Import android. app. activity; <br/> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. content. intentfilter; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. imageview; <br/> Import android. widget. to AST; <br/> Import android. widget. togglebutton; <br/> Import COM. smit. USB storage. r; <br/> public class usbstorageactivity extends activity implements onclicklistener {</P> <p> private imageview mimageview; <br/> private togglebutton mmountsdcard; <br/> private togglebutton mcancel; <br/> // define a broadcastreceiver. When receiving a USB broadcast, disable the current activity. <br/> private broadcastreceiver mbroadcastreceiver = new broadcastrece Iver () {<br/> @ override <br/> Public void onreceive (context, intent) {<br/> string action = intent. getaction (); <br/> If (action. equals (intent. action_ums_disconnected) {<br/> finish (); <br/>}< br/> }; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> setupviews (); <br/>}</P> <P> // initialization <br/> private void setupviews () {<br/> mimageview = (imageview) findviewbyid (R. id. usbstatus); <br/> mmountsdcard = (togglebutton) findviewbyid (R. id. mountsdcard); <br/> mcancel = (togglebutton) findviewbyid (R. id. cancel); <br/> // determines whether the sdcard exists and the button does not exist. <br/> If (! Sdcardismounted () {<br/> mmountsdcard. setenabled (false); <br/>}< br/> mmountsdcard. setonclicklistener (this); <br/> mcancel. setonclicklistener (this); </P> <p> // register broadcastreceiver in the Code <br/> intentfilter mintentfilter = new intentfilter (); <br/> mintentfilter. addaction (intent. action_ums_disconnected); <br/> registerreceiver (mbroadcastreceiver, mintentfilter); <br/>}< br/> // determines whether the SD card exists <br/> Public Boolean sdcardismounted () {<br/> If (Android. OS. environment. getexternalstoragestate (). equals (<br/> android. OS. environment. media_mounted) {<br/> return true; <br/>} else {<br/> return false; <br/>}</P> <p> private Boolean usbismounted () {<br/> Boolean res = false; <br/> If (mmountsdcard. ischecked () {<br/> res = true; <br/>}< br/> return res; <br/>}</P> <p> private void togglemountsdcard () {<br/> If (mmountsdcard. ischecked () {<br/> mountasusbstorage (); <br/>}else {<br/> stopusbstorage (); <br/>}</P> <p> // the SD card loading method is not implemented here <br/> private void mountasusbstorage () {<br/> toast. maketext (this, "SD card mounted", toast. length_long ). show (); <br/>}</P> <p> // Method for uninstalling the SD card <br/> private void stopusbstorage () {<br/> toast. maketext (this, "SD card uninstalled", toast. length_long ). show (); <br/>}</P> <p> Public void onclick (view v) {<br/> If (V = mmountsdcard) {<br/> togglemountsdcard (); <br/> If (usbismounted () {<br/> mimageview. setimageresource (R. drawable. usb_android_connected); <br/>}else {<br/> mimageview. setimageresource (R. drawable. usb_android); <br/>}< br/>} else if (V = mcancel) {<br/> finish (); <br/>}</P> <p>}

Step 5: Modify the androidmanifest. xml file. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. smit. usbstorage "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". usbstorageactivity "<br/> Android: theme =" @ Android: style/theme. notitlebar "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <Cycler Android: Name = ". usbbroadcastrecevier "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. ums_connected "> </Action> <br/> </intent-filter> <br/> </receiver> <br/> </Application> <br/> <uses- SDK Android: minsdkversion = "7"/> <br/> </manifest>

Step 6: run the above project. When we plug in USB and start the USB storageactivity, the effect is as follows:

Click "load SD card:

When you unplug the USB or click the cancel button, the application is disabled.

 

Of course, if we use the above method, this application will inevitably appear in the Application List, and this application can only be started after USB is inserted, and users cannot start it on their own, so we need to hide this application. Here it is actually very simple. We just need to remove the 12th lines of code in step 5, that is:

<Category Android: Name = "android. Intent. Category. launcher"/>

If you don't believe it, try it. LOL ~

OK. Write it here this evening. If you have any questions, please leave a message... Bye ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.