Android Development: A variety of applications for broadcasting

Source: Internet
Author: User

SD Card Listener:

 Packageorg.dreamtech.sdstate;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent; Public classSdcardstatereceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//Get Event TypeString action =intent.getaction (); if("Android.intent.action.MEDIA_MOUNTED". Equals (action)) {            //SD card Mount}Else if("Android.intent.action.MEDIA_UNMOUNTED". Equals (action)) {            //SD Card Uninstall        }    }}

Configuring receiver

       <receiverAndroid:name= "Org.dreamtech.sdstate.SdcardStateReceiver">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MEDIA_MOUNTED"/>                <ActionAndroid:name= "android.intent.action."/>                                <!--the constraint type is called file because the data type stored in the SD is file -                 <DataAndroid:scheme= "File"/>            </Intent-filter>        </receiver>

SMS Listener:

 PackageOrg.dreamtech.smslistener;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.telephony.SmsMessage; Public classSmslistenerreceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {object[] objects= (object[]) Intent.getextras (). Get ("PDUs");  for(Object object:objects) {smsmessage smsmessage= SMSMESSAGE.CREATEFROMPDU ((byte[]) object); String MessageBody=Smsmessage.getmessagebody (); String Address=smsmessage.getoriginatingaddress (); System.out.println ("SMS Content:" + MessageBody + "# # #" + "from" +address); }    }}

Note Configuration and Permissions:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Org.dreamtech.smslistener"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <uses-permissionAndroid:name= "Android.permission.RECEIVE_SMS"/>    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Org.dreamtech.smslistener.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>                <receiverAndroid:name= "Org.dreamtech.smslistener.SmsListenerReceiver">            <Intent-filter>                <ActionAndroid:name= "Android.provider.Telephony.SMS_RECEIVED"/>            </Intent-filter>        </receiver>    </Application></Manifest>

To uninstall the installation listener:

 Packageorg.dreamtech.appstate;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent; Public classAppstatereceiverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); if("Android.intent.action.PACKAGE_INSTALL". Equals (action)) {            //app installation (although present, but not actually called)}Else if("Android.intent.action.PACKAGE_ADDED". Equals (action)) {            //Application Installation}Else if("Android.intent.action.PACKAGE_REMOVED". Equals (action)) {            //App Uninstall        }    }}

Configuration:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Org.dreamtech.appstate"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "8"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Org.dreamtech.appstate.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <receiverAndroid:name= "Org.dreamtech.appstate.AppStateReceiver" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.PACKAGE_INSTALL" />                <ActionAndroid:name= "Android.intent.action.PACKAGE_ADDED" />                <ActionAndroid:name= "Android.intent.action.PACKAGE_REMOVED" />                <!--Note Configuration Constraints -                <DataAndroid:scheme= "Package" />            </Intent-filter>        </receiver>    </Application></Manifest>

Android Development: A variety of applications for broadcasting

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.