1.同一个android.mk文件如何编译多个CPP文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -lm -llog
LOCAL_MODULE := JniLib
LOCAL_SRC_FILES =: JniLib.cpp ads1256.cpp
include $(BUILD_SHARED_LIBRARY)
主要是:LOCAL_SRC_FILES:= a.cpp b.cpp ....
2.切换界面
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
3.安卓APP实现全屏
在protected void onCreate(Bundle savedInstanceState) 函数上面的位置加上下面这个函数
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
View decorView = getWindow().getDecorView();
if(hasFocus){
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
}
4.隐藏顶部标题栏和底部导航栏
在super.onCreate(savedInstanceState)函数上面加上两行代码
@Override
protected void onCreate(Bundle savedInstanceState) {
//加上下面两行代码
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
5.在开发Anroid的时候,当你打开一个界面的时候,屏幕的焦点会自动停留在第一个EditText中,Android的软键盘默认会自动弹出,用户第一眼连界面都没有看清楚,软键盘就弹出来了,这就影响到了用户体验,我们需要设置打开界面的时候,当EditText获取焦点的时候,不弹出软键盘,其实也很简单,代码如下:
// 默认软键盘不弹出 getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
在OnCreate()函数中,加上即可,OK,搞定。
@Override
protected void onCreate(Bundle savedInstanceState) {
//加上下面两行代码,隐藏标题和导航栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加上下面一行代码,防止启动界面弹出键盘
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
6.字符串与数字转换
1 字串 String 转换成整数 int
A. 有两个方法:
1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
注: 字串转成 Double, Float, Long 的方法大同小异.
2 整数 int 转换成字串 String
A. 有叁种方法:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
注: Double, Float, Long 转成字串的方法大同小异.
7.使用ADB将安卓的TXT文件拷贝到电脑
adb root
adb remount
adb pull /system/etc/hosts hosts
8.保存与读取Json格式的数据
public class UtilObjectIO {
private Context mContext;
private String mFilename;
public UtilObjectIO(Context c, String f){
mContext = c;
mFilename = f;
}
//保存数据到文件中
public void saveCarResultJsons(ArrayList<CarResultJson> CarResultJsons) throws JSONException,IOException {
JSONArray array = new JSONArray();
for (CarResultJson c : CarResultJsons)
array.put(c.toJSON());
Writer writer = null;
try {
/**
* Context.openFileOutput(...)方法接收文件名(不能是目录即不能包含/)以及文件操作模式参数,会自动将传入的文件名附加到应用沙盒文件
* (每个应用的沙盒文件目录都是设备/data/data/目录的子目录,且默认以应用包命名。如:/data/data/com.huangfei.criminalintent。
* 只有在设备被root了,其他应用和用户才能随意获取沙盒中的文件。)
* 目录路径之后,然后在新路径下创建并打开文件,等待数据写入。
*/
Log.d("TextView", "已经存在该文件夹!");
//追加方式保存数据
OutputStream out = mContext.openFileOutput(mFilename,Context.MODE_APPEND);
writer = new OutputStreamWriter(out);
Log.d("TextView", array.toString());
writer.append(array.toString()+",");
} finally {
if (writer != null) {
writer.close();
}
}
}
//保存数据
public ArrayList<CarResultJson> loadCarResultJsons() throws IOException, JSONException {
ArrayList<CarResultJson> CarResultJsons = new ArrayList<CarResultJson>();
BufferedReader reader = null;
try {
InputStream in = mContext.openFileInput(mFilename);
reader = new BufferedReader(new InputStreamReader(in));
StringBuilder jsonString = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
jsonString.append(line);
}
/**
* 注意,上面使用MODE_APPEND方式保存数据时,每次都会添加[]号,
* 会导致转换为Json数组时始终认为只要一个JSON对象,所以需要把
* 中间的[]都去掉,}{之间需要用,隔开
*/
// Log.d("TextView", jsonString.toString());
for(int i=1;i<(jsonString.length());i++)
{
if((jsonString.charAt(i)==']')||(jsonString.charAt(i)=='['))
{
jsonString=jsonString.deleteCharAt(i);
i=1;
}
if((jsonString.charAt(i)==']')||(jsonString.charAt(i)=='['))
{
jsonString=jsonString.deleteCharAt(i);
i=1;
}
}
if(jsonString.charAt(jsonString.length()-1)==',')jsonString=jsonString.deleteCharAt(jsonString.length()-1);
jsonString.append("]");
// Log.d("TextView", jsonString.toString());
JSONArray array = (JSONArray) new JSONTokener(jsonString.toString()).nextValue();
for (int i = 0; i < array.length(); i++) {
CarResultJsons.add(new CarResultJson(array.getJSONObject(i)));
}
Log.d("TextView", "数组大小是:"+CarResultJsons.size());
} catch (FileNotFoundException e) {
} finally {
if (reader != null) {
reader.close();
}
}
return CarResultJsons;
}
}
9.获取本地日期
Calendar calendar = Calendar.getInstance();
//获取系统的日期
//年
int year = calendar.get(Calendar.YEAR);
//月
int month = calendar.get(Calendar.MONTH)+1;
//日
int day = calendar.get(Calendar.DAY_OF_MONTH);
//获取系统时间
//小时
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//分钟
int minute = calendar.get(Calendar.MINUTE);
//秒
int second = calendar.get(Calendar.SECOND);
time2.setText("Calendar获取当前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);
10.获取路径的方法
getCacheDir():/data/data/你的应用的包名/cache
getFilesDir():/data/data/你的应用的包名/files
getExternalFilesDir():SDCard/Android/data/你的应用的包名/files/
getExternalCacheDir():SDCard/Android/data/你的应用包名/cache/