Mobile security defender 06-Mobile Anti-Theft custom dialog box

Source: Internet
Author: User

Modify the titlebar of the main interface

You can modify the corresponding configuration in the system's Androidmanifest.xml file to change the theme of the main interface (set to no titlebar style)

The current main interface style is:

        <activity android:name= "com.liuhao.mobilesafe.ui.MainActivity"            android:theme= "@android: style/ Theme.notitlebar "            android:label=" @string/main_screen ">        </activity>
After setting the style to:

Add a custom title and add the appropriate text directly to the top of the main interface layout, as follows:
    <linearlayout         android:layout_width= "match_parent"        android:layout_height= "40dip"        android: background= "@drawable/title_background"        android:gravity= "center_horizontal|center_vertical"        android:o rientation= "vertical"        >        <textview             android:layout_width= "wrap_content"            android:layout_ height= "Wrap_content"            android:textcolor= "@color/textcolor"            android:textsize= "22SP"            android:text = "Cottage phone Guardian"            />    </LinearLayout>
It also adds a title_background background:
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"     android:shape= "Rectangle"    >    <!--border--    <stroke        android:width= "0.5dip"        Android:color= "#ff505050"        />        <!--specify Corners--    <corners         android:radius= "2dip"        / >        <!--gradient--    <gradient         android:startcolor= "#ff505050"        android:centercolor= "# ff383030 "        android:endcolor=" #ff282828 "/></shape>
Add after Effect:

From the main interface click on the activation diagram to switch to live phone anti-theft interface
@Override public    void Onitemclick (adapterview<?> parent, view view, int position,            long id) {        log.i ( TAG, "Click on location" + position);        Switch (position) {case        0:            log.i (TAG, "Enter mobile phone anti-theft");            Intent lostintent = new Intent (mainactivity.this, lostprotectedactivity.class);            StartActivity (lostintent);            break;        }    }
After the icon is hidden, the user can dial a number in the dial-up interface to enter the mobile phone anti-theft interface (Knowledge Point: Broadcast) to get the broadcast sent by the system
    • Callphonereceiver: In the broadcast recipient, after receiving the corresponding processing

    • Configuring the System receiver Androidmanifest.xml:

    <receiver android:name= "Com.liuhao.mobilesafe.receiver.CallPhoneReceiver" >        <intent-filter android: priority= ">            <action android:name=" Android.intent.action.NEW_OUTGOING_CALL "/>        </ Intent-filter>    </receiver>
    • Configure permissions: Android.permission.PROCESS_OUTGOING_CALLS, reset the path of the outgoing call
<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS"/>
Exception handling:

Failed to activate the target activity, the program terminated abnormally.

Unable to start receiver Com.liuhao.mobilesafe.receiver.CallPhoneReceiver:android.util.AndroidRuntimeException: Calling StartActivity () from outside of an Activity context requires the Flag_activity_new_task FLAG. Is this really what do you want?

In an ACTIVITY stack out of the startactivity () operation, the specified flag_activity_new_task flag must be displayed for the intent.

Analysis: We are activating a activity,activity in a broadcast receiver is running in the task stack, while the broadcast receiver is not in the task stack. Therefore, activating an activity in a broadcast recipient or service must specify the FLAG_ACTIVITY_NEW_TASK flag, which specifies that the activity to be activated runs in its own task stack.

<pre name= "code" class= "Java" >lostintent.setflags (Intent.flag_activity_new_task)
    • Callphonereceiver.java the complete
Package Com.liuhao.mobilesafe.receiver;import Com.liuhao.mobilesafe.ui.lostprotectedactivity;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;public Class Callphonereceiver extends Broadcastreceiver {    @Override public    void OnReceive (context context, Intent Intent) {        String number = Getresultdata ();        if ("20122012". Equals (number)) {            Intent lostintent = new Intent (context, lostprotectedactivity.class);            Specifies that the ACTIVITY to be activated runs            lostintent.setflags (intent.flag_activity_new_task) in its own task stack;            Context.startactivity (lostintent);            Terminate this dial-up            //cannot terminate            setresultdata (null) through Abortbroadcast ();}        }}    
Complete the "Password function" Setting yourself
Mobile phone anti-theft interface
    1. The first time you enter the dialog box, let the user set the password
    2. Once the setting is complete, the dialog box pops up and enters the password.
Implementing the custom dialog box
    • Style.xml the implementation of a custom dialog box frame
<?xml version= "1.0" encoding= "Utf-8"?><resources>    <style name= "Mydialog" parent= "@android: Style /theme.dialog ">        <item name=" Android:windowbackground "> @drawable/title_background</item>        <item name= "Android:windownotitle" >true</item>    </style></resources>
    • First_entry_dialog.xml layout content in the Customize dialog box
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" 300dip "android:layout_height=" 280dip "android:gravity=" Center_horizontal "Android oid:orientation= "vertical" > <textview android:layout_width= "wrap_content" android:layout_height= "W Rap_content "android:text=" Set Password "android:textsize=" 24sp "/> <linearlayout android:layout_wid Th= "300dip" android:layout_height= "180dip" android:background= "#ffc8c8c8" android:orientation= "vertic Al "> <textview android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" set the password for mobile phone theft "android:textcolor=" #ff000000 "/> <edittext androi D:id= "@+id/et_first_entry_pwd" android:layout_width= "300dip" android:layout_height= "Wrap_content"/&        Gt            <textviewAndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Enter password Again"            Android:textcolor= "#ff000000"/> <edittext android:id= "@+id/et_first_entry_pwd_confirm" Android:layout_width= "300dip" android:layout_height= "wrap_content"/> </LinearLayout> &lt ;        LinearLayout android:layout_width= "300dip" android:layout_height= "50dip" android:gravity= "center" android:orientation= "Horizontal" > <button android:layout_width= "140dip" Android:la yout_height= "40dip" android:background= "@drawable/button_background" android:text= "OK" an Droid:textcolor= "#ffffffff"/> <button android:layout_width= "140dip" Android:layout_hei            ght= "40dip" android:layout_marginleft= "3dip" android:background= "@drawable/button_background" Android:text= "Cancel"/> </LinearLayout></LinearLayout> 
    • Button_background.xml the background of the button
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android"    android:shape= "Rectangle" >    <!--bezel--    <stroke        android:width= "0.5dip"        android: Color= "#ff107048"/>    <!--Specify corner-to-    <corners android:radius= "2dip"/>    <!--fade-to-    <gradient        android:centercolor= "#ff107048"        android:endcolor= "#ff085830"        android: Startcolor= "#ff109860"/></shape>
    • Lostprotectedactivity.java
Package Com.liuhao.mobilesafe.ui;import Com.liuhao.mobilesafe.r;import Android.app.activity;import Android.app.dialog;import Android.content.context;import Android.content.sharedpreferences;import Android.os.bundle;import Android.util.log;public class Lostprotectedactivity extends Activity {private static final St    Ring TAG = "Lostprotectedactivity";    Private Sharedpreferences sp;    Private Dialog Dialog;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        SP = getsharedpreferences ("config", context.mode_private);        To interpret whether the user has set the password if (Ispwdsetup ()) {log.i (TAG, "Set Password, popup Enter password dialog box");            } else {log.i (TAG, "No password set, popup Setup Password Dialog");        Showfirstentrydialog (); }}/** * the dialog box style for setting the password that pops up the first time you enter the program */private void Showfirstentrydialog () {dialog = new Dialo        G (this, r.style.mydialog); Dialog.setcontentview (R.layout.first_entry_dialog);//Set to displayThe contents of the Dialog.show (); }/** * Check sharedpreference for password settings * * @return */Private Boolean ispwdsetup () {String pas        Sword = sp.getstring ("password", null);        if (password = = null) {return false;            } else {if ("". Equals (password)) {return false;            } else {return true; }        }    }}
Final effect:

Mobile security defender 06-Mobile Anti-Theft custom dialog box

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.