短信发送器
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.smssender" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.SEND_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima28.smssender.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入手机号" /> <EditText android:id="@+id/et_number" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv_number" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et_number" android:text="请输入短信内容" /> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="200px" android:gravity="top" android:layout_below="@id/tv_content" /> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/et_content" android:text="发送短信" /> </RelativeLayout>
package com.itheima28.smssender; import android.os.Bundle; import android.app.Activity; import android.telephony.SmsManager; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener { private EditText etNumber; private EditText etContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etNumber = (EditText) findViewById(R.id.et_number); etContent = (EditText) findViewById(R.id.et_content); Button button = (Button) findViewById(R.id.btn_send); button.setOnClickListener(this); } @Override public void onClick(View v) { // 号码 String number = etNumber.getText().toString(); // 内容 String content = etContent.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage( number, // 收件人 null, // 短信中心号码 content, // 内容 null, null); } }
布局演示(LinearLayout, RelativeLayout)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="按钮1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right|center_vertical" android:text="按钮2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮3" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="按钮2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" an<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:text="按钮1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right|center_vertical" android:text="按钮2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮3" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按钮1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="按钮2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" android:text="按钮3" /> </LinearLayout> </LinearLayout>droid:visibility="gone" android:text="按钮3" /> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="进攻" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="左勾拳" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="右勾拳" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="逃跑" /> <Button android:id="@+id/btn_bisha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="大绝招" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/btn_bisha" android:layout_alignTop="@id/btn_bisha" android:text="左" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/btn_bisha" android:layout_centerHorizontal="true" android:text="上" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/btn_bisha" android:layout_alignBaseline="@id/btn_bisha" android:text="右" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_bisha" android:layout_centerHorizontal="true" android:text="下" /> </RelativeLayout>
布局演示和android下单位
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="1dp" android:layout_y="253dp" android:text="按钮" /> </AbsoluteLayout>
在制作播放器的时候,按钮是层次的。可以使用这种布局方式。
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_width="300px" android:layout_height="300px" android:layout_gravity="center" android:text="最底部" /> <Button android:layout_width="150px" android:layout_height="150px" android:layout_gravity="center" android:text="中间" /> <Button android:layout_width="50px" android:layout_height="50px" android:layout_gravity="center" android:text="顶部" /> </FrameLayout>
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:shrinkColumns="0" android:collapseColumns="0" > <TableRow android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一行, 0列" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一行, 1列" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一行, 2列" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一行, 3列" /> </TableRow> <TableRow android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二行, 0列" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="2" android:layout_span="2" android:text="第二行, 1列" /> </TableRow> </TableLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:layout_width="160px" android:layout_height="wrap_content" android:text="单位是px" android:textSize="18px" /> <Button android:layout_width="160dp" android:layout_height="wrap_content" android:text="单位是sp" android:textSize="18sp" /> </LinearLayout>
android下Junit
冒烟测试:
adb shell monkey -p <程序的包名> -v <事件的数量>
android下单元测试:
在AndroidManifest.xml文件中配置一下信息:
在manifest节点下:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.itheima28.junittest" />
在application节点下配置下面信息:
<uses-library android:name="android.test.runner" />
测试时, 定义一个类继承AndroidTestCase
使用另外一个工程进行单元测试
保存数据到手机内存
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <EditText android:id="@+id/et_number" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入QQ号" /> <EditText android:id="@+id/et_password" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword" /> <CheckBox android:id="@+id/cb_remerber_pwd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="记住密码" /> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout>
package com.itheima28.qqlogin.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.text.TextUtils; public class Utils { /** * 保存用户信息 * @param number * @param password * @return true 成功 */ public static boolean saveUserInfo(String number, String password) { try { String path = "/data/data/com.itheima28.qqlogin/itheima28.txt"; FileOutputStream fos = new FileOutputStream(path); // 307966990##123123 String data = number + "##" + password; fos.write(data.getBytes()); fos.flush(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 保存用户信息 * @param number * @param password * @return true 成功 */ public static boolean saveUserInfo(Context context, String number, String password) { try { // String path = "/data/data/com.itheima28.qqlogin/itheima28.txt"; // File filesDir = context.getFilesDir(); File filesDir = context.getCacheDir(); File f = new File(filesDir, "itheima28.txt"); FileOutputStream fos = new FileOutputStream(f); // 307966990##123123 String data = number + "##" + password; fos.write(data.getBytes()); fos.flush(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 返回用户信息 * @return */ public static Map<String, String> getUserInfo() { try { String path = "/data/data/com.itheima28.qqlogin/itheima28.txt"; FileInputStream fis = new FileInputStream(path); // 字符流对象 BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); // 307966990##123123 String text = reader.readLine(); if(!TextUtils.isEmpty(text)) { String[] split = text.split("##"); Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("number", split[0]); userInfoMap.put("password", split[1]); return userInfoMap; } } catch (Exception e) { e.printStackTrace(); } return null; } /** * 返回用户信息 * @return */ public static Map<String, String> getUserInfo(Context context) { try { // String path = "/data/data/com.itheima28.qqlogin/itheima28.txt"; // File filesDir = context.getFilesDir(); File filesDir = context.getCacheDir(); File f = new File(filesDir, "itheima28.txt"); FileInputStream fis = new FileInputStream(f); // 字符流对象 BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); // 307966990##123123 String text = reader.readLine(); if(!TextUtils.isEmpty(text)) { String[] split = text.split("##"); Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("number", split[0]); userInfoMap.put("password", split[1]); return userInfoMap; } } catch (Exception e) { e.printStackTrace(); } return null; } }
package com.itheima28.qqlogin; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; import com.itheima28.qqlogin.utils.Utils; import com.itheima28.qqlogin.utils.UtilsOfSharedPreferences; public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "MainActivity"; private EditText etNumber; private EditText etPassword; private CheckBox cbRemerberPWD; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etNumber = (EditText) findViewById(R.id.et_number); etPassword = (EditText) findViewById(R.id.et_password); cbRemerberPWD = (CheckBox) findViewById(R.id.cb_remerber_pwd); Button btnLogin = (Button) findViewById(R.id.btn_login); btnLogin.setOnClickListener(this); // 回显数据 Map<String, String> userInfoMap = UtilsOfSharedPreferences.getUserInfo(this); if(userInfoMap != null) { etNumber.setText(userInfoMap.get("number")); etPassword.setText(userInfoMap.get("password")); } } @Override public void onClick(View v) { // 执行登录的操作 // 1. 取出号码和密码 String number = etNumber.getText().toString(); String password = etPassword.getText().toString(); if(TextUtils.isEmpty(number) || TextUtils.isEmpty(password)) { // 弹出吐司 Toast.makeText(this, "请正确输入", Toast.LENGTH_SHORT).show(); return; } // 2. 判断记住密码是否被选中, 如果被选中, 存起来 if(cbRemerberPWD.isChecked()) { // 当前需要记住密码 Log.i(TAG, "记住密码: " + number + ", " + password); boolean isSuccess = UtilsOfSharedPreferences.saveUserInfo(this, number, password); if(isSuccess) { Toast.makeText(this, "保存成功", 0).show(); } else { Toast.makeText(this, "保存失败", 0).show(); } } // 3. 登陆成功 Toast.makeText(this, "登录成功", 0).show(); } }
保存数据到sd卡
package com.itheima28.qqlogin.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.os.Environment; import android.text.TextUtils; public class UtilsOfSDCard { /** * 保存用户信息到sd卡 * @param number * @param password * @return true 成功 */ public static boolean saveUserInfo(Context context, String number, String password) { try { // 判断当前的手机是否有sd卡 String state = Environment.getExternalStorageState(); if(!Environment.MEDIA_MOUNTED.equals(state)) { // 已经挂载了sd卡 return false; } File sdCardFile = Environment.getExternalStorageDirectory(); File file = new File(sdCardFile, "itheima28.txt"); FileOutputStream fos = new FileOutputStream(file); String data = number + "##" + password; fos.write(data.getBytes()); fos.flush(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 到sd卡获取用户信息 * @return */ public static Map<String, String> getUserInfo(Context context) { try { // 判断当前的手机是否有sd卡 String state = Environment.getExternalStorageState(); if(!Environment.MEDIA_MOUNTED.equals(state)) { // 已经挂载了sd卡 return null; } File sdCardFile = Environment.getExternalStorageDirectory(); File file = new File(sdCardFile, "itheima28.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String text = br.readLine(); br.close(); if(!TextUtils.isEmpty(text)) { Map<String, String> userInfoMap = new HashMap<String, String>(); String[] split = text.split("##"); userInfoMap.put("number", split[0]); userInfoMap.put("password", split[1]); return userInfoMap; } } catch (Exception e) { e.printStackTrace(); } return null; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.qqlogin" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <!-- 写入sd卡的权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima28.qqlogin.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
获得内存状态
获取sd卡的剩余空间;
package com.itheima28.memorydemo; import java.io.File; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.os.StatFs; import android.text.format.Formatter; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tvMemoryInfo = (TextView) findViewById(R.id.tv_memory_info); // 获得sd卡的内存状态 File sdcardFileDir = Environment.getExternalStorageDirectory(); String sdcardMemory = getMemoryInfo(sdcardFileDir); // 获得手机内部存储控件的状态 File dataFileDir = Environment.getDataDirectory(); String dataMemory = getMemoryInfo(dataFileDir); tvMemoryInfo.setText("SD卡: " + sdcardMemory + "\n手机内部: " + dataMemory); } /** * 根据路径获取内存状态 * @param path * @return */ private String getMemoryInfo(File path) { // 获得一个磁盘状态对象 StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); // 获得一个扇区的大小 long totalBlocks = stat.getBlockCount(); // 获得扇区的总数 long availableBlocks = stat.getAvailableBlocks(); // 获得可用的扇区数量 // 总空间 String totalMemory = Formatter.formatFileSize(this, totalBlocks * blockSize); // 可用空间 String availableMemory = Formatter.formatFileSize(this, availableBlocks * blockSize); return "总空间: " + totalMemory + "\n可用空间: " + availableMemory; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tv_memory_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
android下权限
文件存储:
this.getFilesDir(); // /data/data/包名/files
this.getCacheDir(); // /data/data/包名/cache
openFileOutput("aa.txt", 0); /data/data/包名/files/aa.txt
权限相关:
1. 私有文件
2. 可读文件
3. 可写文件
4. 可读可写文件.
写数据_权限相关
package com.itheima28.writedata; import java.io.FileNotFoundException; import java.io.FileOutputStream; import android.app.Activity; import android.content.Context; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 写数据 // 私有文件 writeToLocal("private.txt", Context.MODE_PRIVATE); // 可读文件 writeToLocal("readable.txt", Context.MODE_WORLD_READABLE); // 可写文件 writeToLocal("writeable.txt", Context.MODE_WORLD_WRITEABLE); // 可读可写文件 writeToLocal("readable_writeable.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE); } private void writeToLocal(String fileName, int mode) { try { FileOutputStream fos = openFileOutput(fileName, mode); fos.write(("第一个程序写的数据: " + fileName).getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }
读数据_权限相关
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/btn_read_private" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读私有文件" /> <Button android:id="@+id/btn_write_private" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写私有文件" /> <Button android:id="@+id/btn_read_readable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可读文件" /> <Button android:id="@+id/btn_write_readable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可读文件" /> <Button android:id="@+id/btn_read_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可写文件" /> <Button android:id="@+id/btn_write_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可写文件" /> <Button android:id="@+id/btn_read_readable_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读可读可写文件" /> <Button android:id="@+id/btn_write_readable_writeable" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="写可读可写文件" /> </LinearLayout>
package com.itheima28.readdata; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private String basicPath = "/data/data/com.itheima28.writedata/files/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_read_private).setOnClickListener(this); findViewById(R.id.btn_write_private).setOnClickListener(this); findViewById(R.id.btn_read_readable).setOnClickListener(this); findViewById(R.id.btn_write_readable).setOnClickListener(this); findViewById(R.id.btn_read_writeable).setOnClickListener(this); findViewById(R.id.btn_write_writeable).setOnClickListener(this); findViewById(R.id.btn_read_readable_writeable).setOnClickListener(this); findViewById(R.id.btn_write_readable_writeable).setOnClickListener(this); } /** * 哪一个控件被点击, v对象就代表被点击的对象 */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_read_private: readFile("private.txt"); break; case R.id.btn_write_private: writeFile("private.txt"); break; case R.id.btn_read_readable: readFile("readable.txt"); break; case R.id.btn_write_readable: writeFile("readable.txt"); break; case R.id.btn_read_writeable: readFile("writeable.txt"); break; case R.id.btn_write_writeable: writeFile("writeable.txt"); break; case R.id.btn_read_readable_writeable: readFile("readable_writeable.txt"); break; case R.id.btn_write_readable_writeable: writeFile("readable_writeable.txt"); break; default: break; } } /** * 读文件 * @param fileName */ private void readFile(String fileName) { try { String path = basicPath + fileName; BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path))); String text = reader.readLine(); reader.close(); Toast.makeText(this, "读取成功: " + text, 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "读取失败: " + fileName, 0).show(); } } private void writeFile(String fileName) { try { String path = basicPath + fileName; FileOutputStream fos = new FileOutputStream(path); fos.write("哈哈, 被我给黑了".getBytes()); fos.flush(); fos.close(); Toast.makeText(this, "写入成功: " + fileName, 0).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "写入失败: " + fileName, 0).show(); } } }
SharedPreferences使用
SharedPreferences存储路径:/data/data/包名/shared_prefs/
package com.itheima28.qqlogin.utils; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.text.TextUtils; public class UtilsOfSharedPreferences { /** * 保存用户信息到sd卡 * @param number * @param password * @return true 成功 */ public static boolean saveUserInfo(Context context, String number, String password) { try { // /data/data/包名/shared_prefs/itheima28 SharedPreferences sp = context.getSharedPreferences("itheima28", Context.MODE_PRIVATE); // 获得一个编辑对象 Editor edit = sp.edit(); // 存数据 edit.putString("number", number); edit.putString("password", password); // 提交, 数据真正存储起来了. edit.commit(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * 到sd卡获取用户信息 * @return */ public static Map<String, String> getUserInfo(Context context) { SharedPreferences sp = context.getSharedPreferences("itheima28", Context.MODE_PRIVATE); String number = sp.getString("number", null); String password = sp.getString("password", null); if(!TextUtils.isEmpty(number) && !TextUtils.isEmpty(password)) { Map<String, String> userInfoMap = new HashMap<String, String>(); userInfoMap.put("number", number); userInfoMap.put("password", password); return userInfoMap; } return null; } }
xml解析和序列化
Person.java
package com.itheima28.xmldemo; public class Person { private int id; private String name; private int age; @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } public Person(int id, String name, int age) { super(); this.id = id; this.name = name; this.age = age; } public Person() { super(); // TODO Auto-generated constructor stub } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
TestCase.java
package com.itheima28.xmldemo.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import android.os.Environment; import android.test.AndroidTestCase; import android.util.Log; import android.util.Xml; import com.itheima28.xmldemo.Person; public class TestCase extends AndroidTestCase { public void test() { // writeXmlToLocal(); List<Person> personList = parserXmlFromLocal(); for (Person person : personList) { Log.i("TestCase", person.toString()); } } /** * 写xml文件到本地 */ private void writeXmlToLocal() { List<Person> personList = getPersonList(); // 获得序列化对象 XmlSerializer serializer = Xml.newSerializer(); try { File path = new File(Environment.getExternalStorageDirectory(), "persons.xml"); FileOutputStream fos = new FileOutputStream(path); // 指定序列化对象输出的位置和编码 serializer.setOutput(fos, "utf-8"); serializer.startDocument("utf-8", true); // 写开始 <?xml version='1.0' encoding='utf-8' standalone='yes' ?> serializer.startTag(null, "persons"); // <persons> for (Person person : personList) { // 开始写人 serializer.startTag(null, "person"); // <person> serializer.attribute(null, "id", String.valueOf(person.getId())); // 写名字 serializer.startTag(null, "name"); // <name> serializer.text(person.getName()); serializer.endTag(null, "name"); // </name> // 写年龄 serializer.startTag(null, "age"); // <age> serializer.text(String.valueOf(person.getAge())); serializer.endTag(null, "age"); // </age> serializer.endTag(null, "person"); // </person> } serializer.endTag(null, "persons"); // </persons> serializer.endDocument(); // 结束 } catch (Exception e) { e.printStackTrace(); } } private List<Person> getPersonList() { List<Person> personList = new ArrayList<Person>(); for (int i = 0; i < 30; i++) { personList.add(new Person(i, "wang" + i, 18 + i)); } return personList; } private List<Person> parserXmlFromLocal() { try { File path = new File(Environment.getExternalStorageDirectory(), "persons.xml"); FileInputStream fis = new FileInputStream(path); // 获得pull解析器对象 XmlPullParser parser = Xml.newPullParser(); // 指定解析的文件和编码格式 parser.setInput(fis, "utf-8"); int eventType = parser.getEventType(); // 获得事件类型 List<Person> personList = null; Person person = null; String id; while(eventType != XmlPullParser.END_DOCUMENT) { String tagName = parser.getName(); // 获得当前节点的名称 switch (eventType) { case XmlPullParser.START_TAG: // 当前等于开始节点 <person> if("persons".equals(tagName)) { // <persons> personList = new ArrayList<Person>(); } else if("person".equals(tagName)) { // <person id="1"> person = new Person(); id = parser.getAttributeValue(null, "id"); person.setId(Integer.valueOf(id)); } else if("name".equals(tagName)) { // <name> person.setName(parser.nextText()); } else if("age".equals(tagName)) { // <age> person.setAge(Integer.parseInt(parser.nextText())); } break; case XmlPullParser.END_TAG: // </persons> if("person".equals(tagName)) { // 需要把上面设置好值的person对象添加到集合中 personList.add(person); } break; default: break; } eventType = parser.next(); // 获得下一个事件类型 } return personList; } catch (Exception e) { e.printStackTrace(); } return null; } }