[Learn Android while working on projects] mobile security guard 06-custom mobile phone anti-theft dialog box, android06-
Modify the titleBar of the main interface
You can modify the corresponding configuration in the AndroidManifest. xml file of the system to change the theme of the main interface (set to no titleBar style)
The style of the current main interface 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 is as follows:
Add a custom title directly at the top of the main interface layout. Add the corresponding text as follows:
<LinearLayout android: layout_width = "match_parent" android: layout_height = "40dip" android: background = "@ drawable/title_background" android: gravity = "dependency | center_vertical" android: orientation = "vertical"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: textColor = "@ color/textcolor" android: textSize = "22sp" android: text = "shanzhai mobile guard"/> </LinearLayout>
Here, a background of title_background is added:
<? 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 the corner --> <corners android: radius = "2dip"/> <! -- Gradient --> <gradient android: startColor = "# ff505050" android: centerColor = "# ff383030" android: endColor = "# ff282828"/> </shape>
Effect after adding:
Click the activation diagram on the main interface to switch to the active mobile phone anti-theft interface.
@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Log. I (TAG, "click position" + position); switch (position) {case 0: Log. I (TAG, "entering mobile phone anti-theft"); Intent lostIntent = new Intent (MainActivity. this, LostProtectedActivity. class); startActivity (lostIntent); break ;}}
After the icon is hidden, you can call a number on the dial-up interface to access the mobile phone anti-theft interface (knowledge point: broadcast) to obtain the broadcast sent by the system.
CallPhoneReceiver: in the broadcast receiver, handle the received message accordingly.
Configure the receiver AndroidManifest. xml:
<receiver android:name="com.liuhao.mobilesafe.receiver.CallPhoneReceiver" > <intent-filter android:priority="1000"> <action android:name="android.intent.action.NEW_OUTGOING_CALL"/> </intent-filter> </receiver>
- Configure permission: android. permission. process_outgoing_cils, and reset the outbound call path.
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Exception Handling:
The target Activity cannot be activated, and the program ends abnormally.
Unable to start extends er com. liuhao. mobilesafe. aggreger. callPhoneReceiver: android. util. androidRuntimeException: Calling startActivity () from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. is this really what you want?
StartActivity () must be called outside an Activity stack. The FLAG_ACTIVITY_NEW_TASK flag must be specified for Intent.
Analysis: we activate an Activity in a broadcast receiver, and the Activity runs in the task stack, while the broadcast receiver is not in the task stack. Therefore, if an Activity is activated in a broadcast receiver or a service, the FLAG_ACTIVITY_NEW_TASK flag must be specified to run the Activity in its own task stack.
<pre name="code" class="java">lostIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- CallPhoneReceiver. java 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); // specify the Activity to be activated to run lostIntent in its own job stack. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); context. startActivity (lostIntent); // terminate this dial-up // you cannot terminate setResultData (null) through abortBroadcast );}}}
Customize the mobile phone anti-theft interface dialog box
- Style. xml implements a custom dialog box framework.
<?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>
- Layout content in the first_entry_dialog.xml custom 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: orientation = "vertical"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "set password" android: textSize = "24sp"/> <LinearLayout android: layout_width = "300dip" android: layout_height = "180dip" android: background = "# ffc8c8c8" android: orientation = "vertical"> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "password for setting mobile phone theft" android: textColor = "# ff000000"/> <EditText android: id = "@ + id/et_first_entry_pwd" android: layout_width = "300dip" android: layout_height = "wrap_content"/> <TextView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "enter the password again" android: textColor = "# ff000000"/> <EditText android: id = "@ + id/et_first_entry_pwd_confirm" android: layout_width = "300dip" android: layout_height = "wrap_content"/> </LinearLayout> <LinearLayout android: layout_width = "300dip" android: layout_height = "50dip" android: gravity = "center" android: orientation = "horizontal"> <Button android: layout_width = "140dip" android: layout_height = "40dip" android: background = "@ drawable/button_background" android: text = "OK" android: textColor = "# ffffffff"/> <Button android: layout_width = "140dip" android: layout_height = "40dip" android: layout_marginLeft = "3dip" android: background = "@ drawable/button_background" android: text = "cancel"/> </LinearLayout>
- Background of the button_background.xml button
<? 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 = "# ff107048"/> <! -- Specify the corner --> <corners android: radius = "2dip"/> <! -- Gradient --> <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 String TAG = "LostProtectedActivity"; private SharedPreferences sp; private Dialog dialog; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); sp = getSharedPreferences ("config", Context. MODE_PRIVATE); // determines whether the user has set the password if (isPwdSetup () {Log. I (TAG, "set password, pop-up password input dialog box");} else {Log. I (TAG, "no password is set, the Set Password dialog box is displayed"); showFirstEntryDialog ();}} /*** custom dialog box style */private void showFirstEntryDialog () {Dialog = new dialog (this, R. style. myDialog); dialog. setContentView (R. layout. first_entry_dialog); // sets the dialog of the content to be displayed. show ();}/*** check whether the sharedpreference has a password setting ** @ return */private boolean isPwdSetup () {String password = sp. getString ("password", null); if (password = null) {return false;} else {if ("". equals (password) {return false;} else {return true ;}}}}
Final effect: