一、SQLite数据库操作
网上有很多封装SQLite使用的例子,我喜欢这样做——直接执行SQL语句,未提供封装,好在易用:
public static SQLiteDatabase phoneDatabase;
public static final String DB_NAME = "Text.db";
public static final String TB_PWD = "password";
private static Cursor curpwd;
private String str;
1、 创建或打开数据库:
phoneDatabase=this.openOrCreateDatabase(DB_NAME, MODE_WORLD_READABLE, null);
2、 创建表:
phoneDatabase.execSQL("CREATE TABLE IF NOT EXISTS " +TB_PWD + " (password VARCHAR);");
3、 插入记录:
phoneDatabase.execSQL("INSERT INTO "+TB_PWD+" (password) VALUES('"+str+"');");
4、 更新记录:
phoneDatabase.execSQL("UPDATE "+TB_PWD+" SET password = '"+newStr+"' WHERE password = '"+str+"';");
5、 删除表中所有记录:
//表的结构依然在
phoneDatabase.execSQL("DELETE FROM "+TB_PWD+";");
6、 查询表中数据:
curpwd = phoneDatabase.rawQuery("SELECT * FROM "+TB_PWD, null);
if(curpwd.getCount() == 0) {
//表中没有任何记录,注意条件不能为curpwd == null
}
else {
curpwd.moveToFirst(); //得首行记录
int pwdIndex = curpwd.getColumnIndex("password");
String getPwd = curpwd.getString(pwdIndex);
}
7、 关闭数据库:
phoneDatabase.close(); //关闭和打开要对应好
二、findViewById小陷阱
在Activity中用惯了findViewById,一不小心就会直接用它在Dialog中获取View,这时就会事与愿违——程序会提示空指针错误,正确做法:
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView=factory.inflate(R.layout._dialog, null);
//在对话框按钮的单击事件里,这样获取View
EditText setPwdView = (EditText)textEntryView.findViewById(R.id. _edit);
不解释,参考findViewById.
三、锁屏和恢复出厂设置
Android2.2提供了lockNow()和wipeData(0),使得锁屏和恢复出厂设置更简单了,用法示例:
DevicePolicyManager mDPM;
ComponentName mDeviceAdmin;
mDPM =(DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
mDeviceAdmin = new ComponentName(context, ActivityConfig.class);
boolean active = mDPM.isAdminActive(mDeviceAdmin);
if(active){
mDPM.lockNow();
mDPM.wipeData(0);
}
相应在manifest文件中增加如下内容:
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
device_admin.xml内容如下:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
<wipe-data />
</uses-policies>
</device-admin>
但这种封装也不不便之处,就在于这个active,它需要调用系统activity进行控制,如图:
上面这段代码不必在ActivityConfig中,但ActivityConfig必须继承DeviceAdminReceiver, 一般可用ActivityConfig来关连上图所示系统页面,具体可参ApiDemos里 的 DeviceAdminSample.java
四、 换卡自动发短信
网上Down下的代码,原网址丢失,未能注明出处,在此感谢原作者:
public class SimChangeReceiver extends BroadcastReceiver {
private static final String KEY_FIRST_RUN = "FIRST_RUN";
private static final String KEY_SIM_SERIAL_NUMBER = "ICCID";
private static String SMS_ADDRESS = “10086”;
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
TelephonyManager mTelephonyMgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
int SimState = mTelephonyMgr.getSimState();
if (SimState == TelephonyManager.SIM_STATE_READY) {
String simSerialNumber = mTelephonyMgr.getSimSerialNumber();
SharedPreferences sp = context.getSharedPreferences("SimInfo",
Context.MODE_PRIVATE);
boolean isFirstRun = sp.getBoolean(KEY_FIRST_RUN, true);
if (isFirstRun) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean(KEY_FIRST_RUN, true);
editor.putString(KEY_SIM_SERIAL_NUMBER, simSerialNumber);
editor.commit();
} else {
String hostSimSerialNumber = sp.getString(
KEY_SIM_SERIAL_NUMBER, "Unknown");
if (!simSerialNumber.equals(hostSimSerialNumber)) {
// Send Message
PendingIntent mPI = PendingIntent.getBroadcast(context,
0, new Intent(), 0);
SmsManager smsManager = SmsManager.getDefault();
String smsMessage = simSerialNumber;
smsManager.sendTextMessage(SMS_ADDRESS, null,
smsMessage, mPI, null);
}
}
}
}
}
}
修改manifest文件:
<application>
<receiver android:name="com.XXXX.SimChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
五、小结
基于上述内容,可实现SIM卡变换时自动发短信到监控方手机,如为非法换卡,监控方手机可发短信指令实现远程锁屏并恢复出厂设置。