- 全屏窗口
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); - Preference Activity
Layout\crc.xml
<?xml version="1.0" encoding="utf-8" ?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="CRC Test">
<PreferenceScreen
android:title="1. BIN CRC"
android:summary="Code Area CRC"
android:key="bin_crc">
</PreferenceScreen>
<PreferenceScreen
android:title="2. EFS CRC Detail"
android:summary="EFS CRC Detail"
android:key="efs_crc">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.dhz.testapp"
android:targetClass="com.dhz.testapp.efscrc"/>
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen> - Preference 添加单击处理
PreferenceScreen BINCRC = (PreferenceScreen)getPreferenceScreen().findPreference("bin_crc");
BINCRC.setOnPreferenceClickListener(new Preference.OnPreferenceClickListene r() {
public boolean onPreferenceClick(Preference preference) {
BINCRC.setSummary("Detect a click");
return true;
}
}); - 运行一个命令或程序获取结果/正则表达式查找匹配/分隔字符串
Process process1 = new ProcessBuilder("cat", "/opl/etc/system.prop").start();
BufferedReader br1 = new BufferedReader(new InputStreamReader(process1.getInputStream()));
Pattern pattern = Pattern.compile("apps.setting.platformversion");
String line;
while ((line = br1.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if(matcher.find(){
String[] temp = line.split(" = ");
if(temp.length > 1)
mRver.setSummary(temp[1]);
else
mRver.setSummary("/opl/etc/system.prop not configed well.");
}
}
if(process1 != null)
process1.destroy();
if(br1 != null)
br1.close(); - 计算一个文件的CRC32值
import java.util.zip.CRC32;
FileInputStream inStream = null;
BufferedInputStream in = null;
CRC32 crc32 = new CRC32();
inStream = new FileInputStream(filePath);
in = new BufferedInputStream(inStream,size_8k);
for(int i;(i=in.read())!=-1;)
{
crc32.update(i);
}
Log.d(TAG, filePath +" CRC32: "+Long.toHexString(crc32.getValue()));
if(inStream != null)
inStream.close();
if(in != null)
in.close(); - 显示一个模态对话框,带yes/no按钮
new AlertDialog.Builder(efscrc.this)
.setMessage("Work Done! Show results?")
.setCancelable(false)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
showResult();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int whichButton) {
dialog.dismiss();
}
})
.show(); - 判断是否有SDCard
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
copyToSDCard(logFilePath);
}
else {
Toast.makeText(FPRITest.this,"No SDCard! Please insert SDCard. ",Toast.LENGTH_SHORT).show();
} - 结果集排序/List遍历
List<String> resultList = new ArrayList<String>();
resultList.add(str1);
......
resultList.add(strn);
Collections.sort(resultList);
FileOutputStream outStream = new FileOutputStream(efsCRCResultFile);
Iterator itr = resultList.iterator();
while (itr.hasNext()) {
outStream.write(((String)itr.next()).getBytes());
} - 非递归方法枚举目录下所有文件(含子目录)
private void getFileList(String dirPath)
{
LinkedList<File> mDirList = new LinkedList<File>();
File dir = new File(dirPath);
mDirList.add(dir);
File files[] = null;
int count = 0;
String filePath = null;
File tmp = null;
while (!mDirList.isEmpty()) {
tmp = mDirList.removeFirst();
//handle files in one directory.
files = tmp.listFiles();
if (files == null)
continue;
for (int i = 0; i < files.length; i++)
{
if (files[i].isDirectory())
mDirList.add(files[i]);
else
{
Log.d(TAG,files[i].getAbsolutePath() + " " + count);
count ++;
}
}
}
Log.d(TAG,"getFileList finish,file count: " + count);
} - 添加秘密程序,通过拨号启动
AndroidManifest.xml
<receiver android:name=".secret">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" android:host="1234"/>
</intent-filter>
</receiver>
secret.java
public class secret extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
String host = intent.getData() != null ? intent.getData().getHost() : null;
if (Intents.SECRET_CODE_ACTION.equals(action) && "1234".equals(host)) {
//start an acitivity
}
}
}
SECRET_CODE_ACTION
public static final String SECRET_CODE_ACTION Broadcast Action: A "secret code" has been entered in the dialer. Secret codes are of the form *#*##*#*. The intent will have the data URI:
android_secret_code://<code> - 格式化SD卡/卸载SD卡
private IMountServicemMountService;
mMountService = getMountService();
private synchronized IMountService getMountService() {
if (mMountService == null) {
IBinder service = ServiceManager.getService("mount");
if (service != null) {
mMountService = IMountService.Stub.asInterface(service);
} else {
Log.e(TAG, "Can't get mount service");
}
}
return mMountService;
}
//format sdcard
mMountService.formatMedia(Environment.getExternalStorageDirectory().toString());
//unmount sdcard
mMountService.unmountMedia(Environment.getExternalStorageDirectory().toString()); - 保持屏幕常亮
PowerManager.WakeLock wakeLock;
启用屏幕常亮功能
wakeLock = ((PowerManager)getSystemService(POWER_SERVICE)).
newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "MyActivity");
wakeLock.acquire();
//PowerManager.SCREEN_DIM_WAKE_LOCK 这个可维持在低明状态,还有其他的标志,可参考Andoid文档
关闭屏幕常亮功能
if (wakeLock != null) {
wakeLock.release();
}
需要权限
<uses-permission android:name="android.permission.WAKE_LOCK" />
为了不对其他程序造成影响,启用和关闭屏幕常亮的代码一般被放在 Activity 的 onResume() 和 onPause() 事件中。 - 调用setting的校准屏幕功能
Intent intent = new Intent();
intent.setAction("android.intent.action.MAIN");
intent.setComponent(new ComponentName("com.android.settings","com.android.settings.Calibration"));
startActivity(intent);
- DOM解析XML文档
DOM为XML 文档的解析定义了一组接口。解析器读入整个文档,然后构建一个驻留内存的树结构,然后代码就可以使用 DOM 接口来操作这个树结构。
优点:整个文档树在内存中,便于操作;支持删除、修改、重新排列等多种功能;
缺点:将整个文档调入内存(包括无用的节点),浪费时间和空间;
使用场合:一旦解析了文档还需多次访问这些数据;硬件资源充足(内存、CPU)。
public void parserXml(String fileName) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
NodeList employees = document.getChildNodes();
for (int i = 0; i < employees.getLength(); i++) { };
NodeList paramList = document.getElementsByTagName_r("yourTag");
for(int i=0; i<paramList.getLength(); i++){
Node paramNode = paramList.item(i);
NamedNodeMap attrs = paramNode.getAttributes();
for(int j=0; j<attrs.getLength();j++){
String itemName = attrs.item(j).getNodeName();
String itemVal = attrs.item(j).getNodeValue();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
项目中用到的Android代码整理
最新推荐文章于 2022-12-03 09:50:24 发布