程序主界面:
1. 程序的主界面上半部分用于放置广告,下半部分放置一个九宫格GridView,九宫格也是需要通过适配器adapter来填充数据的
2. 把一个xml布局转换成一个view对象,需要通过布局填充器来实现
Inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.main_item, null);
3.主界面的颜色选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/item_background_selected" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/item_background_selected" /> <!-- focused -->
<item android:drawable="@drawable/item_background_normal" /> <!-- default -->
</selector>
3. GridView有一个bug就是不上有几个宫格就调用几次getview(),而是先试着调用,然后才真正的加载数据,所以会比这个宫格的数多,listview没有这个问题
4. 优化GridView的显示效率
减少变量的声明次数:把getview()里的iv,tv做成全局的static变量
5. sp的使用:
sp = getSharedPreferences("config", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putBoolean("autoupdate", true);
editor.commit();
拿到sp里持久话的数据:boolean autoupdate = sp.getBoolean("autoupdate", true);
6.自定义dialog的布局view
AlertDialog.Builder builder = new Builder(this);
View view = View.inflate(this, R.layout.normal_entry_dialog, null);
builder.setView(view);
dialog = builder.create();
dialog.show();
6. trim只会trim掉前面和后面的空格
7.Md5加密:
package cn.itcast.mobilesafe.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Utils {
public static String encode(String password){
try {
MessageDigest digest = MessageDigest.getInstance("md5");
byte[] result = digest.digest(password.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
String str = Integer.toHexString((0xFF & result[i]));
if (str.length() == 1) {
sb.append("0" + str);
} else {
sb.append(str);
}
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
7. 通过广播接收者快速的进入手机防盗防盗界面
先是在清单文件里配置:
<receiver android:name=".receiver.CallPhoneReceiver" >
<intent-filter android:priority="1000" >
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
然后定义一个receiver:
package cn.itcast.mobilesafe.receiver;
import cn.itcast.mobilesafe.activities.LostProtectedActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
public class CallPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String number = getResultData();
if("20122012".equals(number)){
Intent lostintent = new Intent(context,LostProtectedActivity.class);
lostintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(lostintent);
setResultData(null);
}
}
}
8. 手机防盗名称的修改:
在手机防盗界面出来menu点击事件,弹出dialog对话框,然后更改标题名称,把更改的数据持久化到sp中,然后在gridview的adapter里的getview处理一下,从sp中获取更改的数据,设置到tv中。
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_lost:
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("设置更改的标题名称");
final EditText et = new EditText(this);
builder.setView(et);
builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String newname = et.getText().toString().trim();
Editor editor = sp.edit();
editor.putString("newname", newname);
editor.commit();
}
});
builder.create().show();
break;
}
return true;
}
9. 拦截外拨电话需要一个权限:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />