Android Radio (II)

Source: Internet
Author: User

Below, we introduce the radio in SMS by SMS blocker.

Layout file

You can set the number that you want to intercept in the layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textstyle= "Bold"android:textsize= "25SP"Android:text= "number to intercept"/>    <EditTextAndroid:id= "@+id/phonenum"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" />    <ButtonAndroid:id= "@+id/sure"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:textsize= "18SP"Android:text= "Confirm"/></LinearLayout>

Activity

Get the data in the activity and save it to a local XML database

 PackageXidian.dy.com.chujia;ImportAndroid.content.Context;Importandroid.content.SharedPreferences;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {sharedpreferences sp; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); SP= Getsharedpreferences ("Hate", context.mode_private); Button Button=(Button) Findviewbyid (r.id.sure); if(Button! =NULL) Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {EditText et=(EditText) Findviewbyid (r.id.phonenum); if(Et! =NULL) {sp.getstring ("Num", Et.gettext (). toString ()); Toast.maketext (mainactivity. This, "Saved successfully", Toast.length_short). Show ();    }                }            }); }}
SMS Blocker
 PackageXidian.dy.com.chujia;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.os.Build;ImportAndroid.os.Bundle;ImportAndroid.provider.Telephony;ImportAndroid.telephony.SmsMessage;ImportAndroid.util.Log;/*** Created by Dy on 2016/7/11.*/ Public classSmsrecieverextendsBroadcastreceiver {@Override Public voidOnReceive (Context context, Intent Intent) {//Message information encapsulated in intentBundle bundle =Intent.getextras (); Object[] Objects= (object[]) bundle.get ("PDUs"));        Smsmessage SMS; intCurrentapivversion =Build.VERSION.SDK_INT; //get all the text messages on the radio (it's possible that the text message is too long to be split into multiple pieces for sending)         for(Object obj:objects) {if(Currentapivversion >=Build.version_codes. LOLLIPOP) SMS= SMSMESSAGE.CREATEFROMPDU ((byte[]) obj, Telephony.Sms.Intents.SMS_RECEIVED_ACTION); ElseSMS= SMSMESSAGE.CREATEFROMPDU ((byte[]) obj); LOG.I ( This. GetClass (). GetName (), sms.getoriginatingaddress ()); LOG.I ( This. GetClass (). GetName (), Sms.getmessagebody ()); //Broadcast interception//messages from 1243 will be intercepted.            if(Sms.getoriginatingaddress (). Equals ("1243") ) {abortbroadcast (); }        }    }}

The format of the SMS is complex (number + information), so it is stored in the intent object. Due to the problem of the API level considered here it is necessary to make version judgments. The broadcast will be destroyed directly after calling Abortbroadcast.

Manifest file

Message acceptance is required in the manifest file because the broadcast recipient also has a priority problem ( -1000~1000), so we should set our interceptor priority to the highest (1000)

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Xidian.dy.com.chujia">    <uses-permissionAndroid:name= "Android.permission.RECEIVE_SMS"/>    <ApplicationAndroid:allowbackup= "true"Android:icon= "@mipmap/ic_launcher"Android:label= "@string/app_name"Android:supportsrtl= "true"Android:theme= "@style/apptheme">        <ActivityAndroid:name=". Mainactivity "Android:label= "Main interface">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <receiverAndroid:name=". Smsreciever ">            <Intent-filterandroid:priority= "+">                <ActionAndroid:name= "Android.provider.Telephony_SMS_RECIEVED"/>            </Intent-filter>        </receiver>    </Application></Manifest>

Attention

After Android4.0, if the app is installed but has never been launched by the user, or if the user manually kills the app process, the system does not actively activate the broadcast recipient in the app to accept the broadcast.

Android Radio (II)

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.