本文实践知识点有有三:

1.布局文件,android布局有相对布局,线性布局,绝对布局,表格布局,标签布局等。各个布局能够嵌套的。本文的布局文件就是线性布局的嵌套

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/Telhint"android:textSize="19sp"android:textColor="#FFFFFF"android:background="#000000"/><EditTextandroid:id="@+id/Tel"android:inputType="number"android:layout_width="fill_parent"android:layout_height="75sp"android:textSize="30sp"android:paddingLeft="10sp"android:paddingRight="10sp"android:background="@drawable/text1"/><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/Smshint"android:textSize="19sp"android:textColor="#FFFFFF"android:background="#000000"/><EditTextandroid:id="@+id/Sms"android:inputType="textMultiLine"android:layout_width="fill_parent"android:layout_height="180dip"android:textSize="19sp"android:minLines="10"android:gravity="left"android:background="@drawable/text2"android:paddingTop="19sp"android:paddingBottom="22sp"android:paddingLeft="22sp"android:paddingRight="15sp"/><LinearLayoutandroid:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"><Buttonandroid:layout_marginTop="10sp"android:layout_marginLeft="3sp"android:id="@+id/button0"android:layout_width="wrap_content"android:layout_height="75sp"android:text="@string/send"android:textColor="#7cfc00"android:textSize="19sp"/><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="75sp"android:layout_marginTop="10sp"android:layout_marginLeft="3sp"android:text="@string/savephone"android:textColor="#800080"android:textSize="19sp" /><Buttonandroid:id="@+id/button6"android:layout_marginTop="10sp"android:layout_width="wrap_content"android:layout_height="75sp"android:layout_marginLeft="3sp"android:text="@string/savecache"android:textColor="#800080"android:textSize="19sp" /><Buttonandroid:id="@+id/button10"android:layout_width="wrap_content"android:layout_height="75sp"android:layout_marginTop="5sp"android:layout_marginLeft="1sp"android:layout_gravity="center"android:text="@string/deletecache"android:textColor="#800080"android:textSize="19sp" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Buttonandroid:layout_marginTop="10sp"android:layout_marginLeft="3sp"android:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="75sp"android:text="@string/clear"android:textColor="#ff0000"android:textSize="19sp"/><Buttonandroid:layout_marginTop="10sp"android:layout_marginLeft="3sp"android:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="75sp"android:text="@string/savesdcard"android:textColor="#800080"android:textSize="19sp"/> <Buttonandroid:layout_marginTop="10sp"android:layout_marginLeft="3sp"android:id="@+id/button7"android:layout_width="wrap_content"android:layout_height="75sp"android:text="@string/Readcache"android:textColor="#800080"android:textSize="19sp"/> <Buttonandroid:id="@+id/button11"android:layout_width="wrap_content"android:layout_height="75sp"android:text="@string/future"android:layout_marginTop="2sp"android:layout_marginLeft="1sp"android:gravity="center"android:textColor="#800080"android:textSize="19sp" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Buttonandroid:id="@+id/button4"android:layout_width="90dp"android:layout_height="wrap_content"android:layout_marginTop="10sp"android:layout_marginLeft="1sp"android:text="@string/Readphone"android:textColor="#0000ff"android:textSize="19sp" /><Buttonandroid:id="@+id/button5"android:layout_width="90dp"android:layout_height="wrap_content"android:layout_marginTop="10sp"android:layout_marginLeft="1sp"android:text="@string/Readsdcard"android:textColor="#0000ff"android:textSize="19sp" /><Buttonandroid:id="@+id/button8"android:layout_width="90dp"android:layout_height="wrap_content"android:layout_marginTop="10sp"android:layout_marginLeft="1sp"android:text="@string/deletephone"android:textColor="#0000ff"android:textSize="19sp" /><Buttonandroid:id="@+id/button9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10sp"android:layout_marginLeft="1sp"android:text="@string/deletesd"android:textColor="#0000ff"android:textSize="19sp" /></LinearLayout></LinearLayout>

l
2.布局文件里使用字符以及Java源码中使用的字符最好在string.xml先定义,然后再引用

<?

xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MySMS</string> <string name="Telhint">请输入号码:</string> <string name="Smshint">请输入短信内容:</string> <string name="send">发送</string> <string name="OK">发送成功</string> <string name="savephone">to-手机</string> <string name="savesdcard">to-SD卡</string> <string name="ReadSD">读SD卡信息</string> <string name="clear">清除</string> <string name="action_settings">Settings</string> <string name="Readphone">读手机短信</string> <string name="Readsdcard">读sd卡短信</string> <string name="future"> --NULL-- </string> <string name="savecache">to-cache</string> <string name="Readcache">读cache</string> <string name="deletephone">删手机短信</string> <string name="deletesd">删sd卡短信</string> <string name="deletecache">C-Cache</string> </resources>

3.涉及的安全方面的业务。必需在androidMainifest.xml注冊,如发送短信,拨打电话。读取SD卡数据等

<uses-permission android:name="android.permission.SEND_SMS"/>
   <uses-permission  android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
   <uses-permission  android:name="android.permission.READ_EXTERNAL_STORAGE"/>
       <uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>"

当中当中竖屏翻转,也须要在这里注冊

android:screenOrientation="portrait"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.mysms"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="19" /><uses-permission android:name="android.permission.SEND_SMS"/><uses-permission  android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><uses-permission  android:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>"<applicationandroid:allowBackup="true"android:icon="@drawable/sms"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.mysms.MainActivity"android:label="@string/app_name" android:screenOrientation="portrait"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

4.操作文件的功能实现最好新建一个业务类

package com.example.FileService;import android.content.Context;import android.os.Environment;
import android.util.Log;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Fileservice {private Context context;public Fileservice(Context context) {this.context = context;}private final String tag ="mytag";public void SavefileToPhone(String filename,String content) throws Exception{  Log.e(tag, "hello,start save file to phone internal");FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_PRIVATE);Log.e(tag, "hello,open file is ok");outStream.write(content.getBytes());outStream.close();}public void SavefileToCache(String filename,String content) throws Exception{File cachedir = context.getCacheDir();File file=new File(cachedir,filename);OutputStream os = new FileOutputStream(file);os.write(content.getBytes());os.close();}public void SavefileToSD(String filename,String content) throws Exception{Log.e(tag,"SD card is begint!");File path= Environment.getExternalStorageDirectory();Log.e(tag,"SD card save is start!");File file=new File(path,filename);Log.e(tag,"SD card file  is creat!");OutputStream os = new FileOutputStream(file);Log.e(tag,"SD card outputstream is get!");os.write(content.getBytes());Log.e(tag,"SD card is write!");os.close();}public String ReadfileToSD(String filename) throws Exception{File path= Environment.getExternalStorageDirectory();int len = 0;byte[] buffer = new byte[1024];Log.e(tag, "hello,Read cache file begin");File file=new File(path,filename);Log.e(tag, "hello,get a file string");ByteArrayOutputStream outStream = new ByteArrayOutputStream();Log.e(tag, "hello,get a outstream");FileInputStream InputStream = new FileInputStream(file); Log.e(tag, "hello,get a Inputstream");while((len=InputStream.read(buffer))!=-1){outStream.write(buffer, 0, len);}byte[] data=outStream.toByteArray();InputStream.close();return new String (data);}public String ReadfileFromcache(String filename) throws Exception{ int len = 0;byte[] buffer = new byte[1024];File cachedir = context.getCacheDir();Log.e(tag, "hello,Read cache file begin");File file=new File(cachedir,filename);Log.e(tag, "hello,get a file string");ByteArrayOutputStream outStream = new ByteArrayOutputStream();Log.e(tag, "hello,get a outstream");FileInputStream InputStream = new FileInputStream(file); Log.e(tag, "hello,get a Inputstream");while((len=InputStream.read(buffer))!=-1){outStream.write(buffer, 0, len);}byte[] data=outStream.toByteArray();InputStream.close();return new String (data);}public String ReadfileFromPhone(String file) throws Exception{  int len = 0;byte[] buffer = new byte[1024];ByteArrayOutputStream outStream = new ByteArrayOutputStream();FileInputStream InputStream= context.openFileInput(file);while((len=InputStream.read(buffer))!=-1){outStream.write(buffer, 0, len);}byte[] data=outStream.toByteArray();return new String (data);}
}

注意android操作文件有两种方式:

一。

使用context类,context提供了输入输出流接口,一般用于读写手机内部文件

二。.使用file类。这个是更顶层的类。通过文件路径和文件名称。能够用于读写系统不论什么地方的文件,一般用于读写cache。SD卡。其它路径文件。

5.activity类,用于GUI操作

package com.example.mysms;import java.io.File;
import java.util.ArrayList;import com.example.FileService.Fileservice;import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.os.Environment;public class MainActivity extends ActionBarActivity {EditText text1;EditText text2;Button bt,bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt10;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text1=(EditText)findViewById(R.id.Tel);text2=(EditText)findViewById(R.id.Sms); bt=(Button)findViewById(R.id.button0);bt1=(Button)findViewById(R.id.button1);bt2=(Button)findViewById(R.id.button2);bt3=(Button)findViewById(R.id.button3);bt4=(Button)findViewById(R.id.button4);bt5=(Button)findViewById(R.id.button5);bt6=(Button)findViewById(R.id.button6);bt7=(Button)findViewById(R.id.button7);bt8=(Button)findViewById(R.id.button8);bt9=(Button)findViewById(R.id.button9);bt10=(Button)findViewById(R.id.button10);bt.setOnClickListener(listener);bt1.setOnClickListener(listener);bt2.setOnClickListener(listener);bt3.setOnClickListener(listener);bt4.setOnClickListener(listener);bt5.setOnClickListener(listener);bt6.setOnClickListener(listener);bt7.setOnClickListener(listener);bt8.setOnClickListener(listener);bt9.setOnClickListener(listener);bt10.setOnClickListener(listener);}private OnClickListener listener =new OnClickListener(){String Tel;String Sms;String filename ;private final String Tag=new String("Activity_tag");@Overridepublic void onClick(View v) {Button btx=(Button)v;switch(btx.getId()){case R.id.button0:Tel=text1.getText().toString();Sms = text2.getText().toString();if(Tel.equals("")==true){Toast.makeText(MainActivity.this, "请输入号码!", Toast.LENGTH_LONG).show();return; }if(Sms.equals("")==true){Toast.makeText(MainActivity.this, "请输入信息。", Toast.LENGTH_LONG).show();return;}SmsManager smsm=SmsManager.getDefault();ArrayList<String> texts=smsm.divideMessage(Sms);for(String text:texts){smsm.sendTextMessage(Tel, null, text, null, null);}text1.setText("");text2.setText("");Toast.makeText(MainActivity.this, R.string.OK, Toast.LENGTH_LONG).show();break;case R.id.button1:Tel=text1.getText().toString();Sms = text2.getText().toString();filename = Tel+".txt";Fileservice savetophone = new Fileservice(getApplicationContext());try{savetophone.SavefileToPhone(filename, Sms);Toast.makeText(getApplicationContext(), "保存到手机成功", Toast.LENGTH_SHORT).show();Toast.makeText(getApplicationContext(), "文件路径为"+getApplicationContext().getFilesDir().toString(), Toast.LENGTH_SHORT).show();}catch(Exception e){Toast.makeText(getApplicationContext(), "保存到手机失败", Toast.LENGTH_SHORT).show();}break;case R.id.button2:text2.setText("");Toast.makeText(getApplicationContext(), "清除成功", Toast.LENGTH_SHORT).show();break;case R.id.button3:Tel=text1.getText().toString();Sms = text2.getText().toString();filename = Tel+".txt";Fileservice service3 = new Fileservice(getApplicationContext());try{if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){service3.SavefileToSD(filename, Sms);File path= Environment.getExternalStorageDirectory();String PATH = path.toString();Toast.makeText(getApplicationContext(), "保存SD卡中的短信成功", Toast.LENGTH_SHORT).show();Toast.makeText(getApplicationContext(), "SD卡路径为"+PATH, Toast.LENGTH_SHORT).show();}else{Toast.makeText(getApplicationContext(), "SD卡无法识别", Toast.LENGTH_SHORT).show();}}catch(Exception e){Toast.makeText(getApplicationContext(), "保存SD卡中的短信失败", Toast.LENGTH_SHORT).show();}break;case R.id.button4:Tel=text1.getText().toString();filename = Tel+".txt";Fileservice service = new Fileservice(getApplicationContext());try{   Sms=service.ReadfileFromPhone(filename);text2.setText(Sms); Toast.makeText(getApplicationContext(), "读取手机中的短信成功", Toast.LENGTH_SHORT).show();Toast.makeText(getApplicationContext(),  "文件路径为"+getApplicationContext().getFilesDir().toString(), Toast.LENGTH_SHORT).show();}catch(Exception e){Toast.makeText(getApplicationContext(), "读取手机中的短信失败", Toast.LENGTH_SHORT).show();}break;case R.id.button5:Tel=text1.getText().toString();filename = Tel+".txt";Fileservice service4 = new Fileservice(getApplicationContext());try{if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){text2.setText(service4.ReadfileToSD(filename));Toast.makeText(getApplicationContext(), "读取SD卡中的短信成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(getApplicationContext(), "SD卡无法识别", Toast.LENGTH_SHORT).show();}}catch(Exception e){Toast.makeText(getApplicationContext(), "读取SD卡中的短信失败", Toast.LENGTH_SHORT).show();}break;case R.id.button6:Tel=text1.getText().toString();Sms = text2.getText().toString();filename = Tel+".txt";Fileservice savecache = new Fileservice(getApplicationContext());try{savecache. SavefileToCache(filename, Sms);Toast.makeText(getApplicationContext(), "保存到cache成功", Toast.LENGTH_SHORT).show();Toast.makeText(getApplicationContext(), "文件路径为"+getApplicationContext().getCacheDir().toString(), Toast.LENGTH_SHORT).show();}catch(Exception e){Toast.makeText(getApplicationContext(), "保存到手机失败", Toast.LENGTH_SHORT).show();}case R.id.button7:Tel=text1.getText().toString();filename = Tel+".txt";try{Fileservice service1=new Fileservice(getApplicationContext());text2.setText(service1.ReadfileFromcache(filename));;Toast.makeText(getApplicationContext(), "读取cache中的数据成功", Toast.LENGTH_SHORT).show();}catch(Exception e){Toast.makeText(getApplicationContext(), "读取cache中的数据失败", Toast.LENGTH_SHORT).show();}break;case R.id.button8:Tel=text1.getText().toString();filename = Tel+".txt";getApplicationContext().deleteFile(filename);Toast.makeText(getApplicationContext(), "删除手机短信的短信成功", Toast.LENGTH_SHORT).show();break;case R.id.button9:Tel=text1.getText().toString();filename = Tel+".txt";try{if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){File file=new File(Environment.getExternalStorageDirectory(),filename);file.delete();Toast.makeText(getApplicationContext(), "删除SD卡中的短信成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(getApplicationContext(), "SD卡无法识别", Toast.LENGTH_SHORT).show();}}catch(Exception e){Toast.makeText(getApplicationContext(), "删除SD卡中的短信失败", Toast.LENGTH_SHORT).show();}break;case R.id.button10:Log.i(Tag,"Clear the cache\n");File cachedir = getApplicationContext().getCacheDir();Log.i(Tag,"Get the patch\n");File file=new File(cachedir,filename);Log.i(Tag,"Get the file structor\n");file.delete();Log.i(Tag,"Clear the cache end\n");Toast.makeText(getApplicationContext(), "删除Cache中数据成功", Toast.LENGTH_SHORT).show();break;}}};}

在本实践中还要学会调试方式:

1.採用logcat查看日志调试

2.通过File explorer 导出文件调试

3.单元測试调试

4. 怎样通过google 官方手冊学习API的使用

重要类;context,上下文,一般一个类实例化后就有一个上下文。

測试结果:

转载于:https://www.cnblogs.com/ldxsuanfa/p/10537943.html

android 数据存储----android短信发送器之文件的读写(手机+SD卡)相关推荐

  1. Android SMSC设置失败短信发送失败问题

    1:获得PDU码,, 比如 短信中心号码是  +8613800760500 先2位分组,后面不够的加一个f  那么就是  86 13 80 07 60 50 0f                    ...

  2. android 脚本发短信,Android使用SmsManager实现短信发送功能

    安卓使用SmsManager实现发送短信,供大家参考,具体内容如下 关键代码实现 package com.example.sms; import java.util.ArrayList; import ...

  3. 考勤情况记录数据表、短信发送记录表设计

    考勤管理数据表设计 1.刷卡记录表 字段 类型 约束 说明 work_id varchar(64) not null 工号 username varchar(64) not null 姓名 depar ...

  4. 下列哪个可做 Android 数据存储,Android应用开发试题

    Android应用开发试题 一.选择题(每题2分,共30分) 1. 下列不是手机操作系统的是?()D A. Android B. Window Mobile C. Apple IPhone IOS D ...

  5. 短信发送失败之短信中心号码异常

    发送短信时,短信中心号码默认是从卡里面的 EFsmsp 读到的,有的卡不符合规范,SIM 卡里短信中心号码为空,导致短信发送失败. 排查这种情况,Android手机,可以通过*#*#4636#*#*进 ...

  6. 【瑞吉外卖】day08:短信发送、手机验证码登录

    目录 4. 短信发送​编辑 4.1 短信服务介绍 4.2 阿里云短信服务介绍 4.3 阿里云短信服务准备 4.4 代码开发 5. 手机验证码登录 5.1 需求分析 5.2 数据模型 5.3 前端页面分 ...

  7. SpringBoot——短信发送、手机验证码登录

    目录 一.短信发送 1.1 阿里云短信服务 1.1.1 设置短信签名 1.1.2 模板管理 1.1.3 设置AccessKey 1.2 短信发送--代码开发 1.2.1 导入maven坐标 1.2.2 ...

  8. 手机sd卡恢复工具android版,SD卡数据恢复软件

    SD卡数据恢复软件,SD卡文件数据恢复工具(内存卡数据恢复)是一款功能强大.体积小巧.操作简便的SD卡数据恢复软件,可恢复SD卡上因各种原因导致丢失的文件.增加了预览功能.有需要的用户赶紧来下载试试看 ...

  9. android短信发送和接受

    短信发送 发短信第一件事是在manifest文件中添加权限 <uses-permission android:name="android.permission.SEND_SMS&quo ...

最新文章

  1. 南昌大学计算机专业高数课本,南昌大学高等数学期末考试.pdf
  2. Ant Design 4.0 进行时!
  3. 导航栏下拉菜单效果代码
  4. swift:简单使用翻页控制器UIPageViewController
  5. Azure云端部署Exchange 2016双数据中心—Part6(DAG切换测试)
  6. flex 有关数据类型强制转
  7. 雷军:小米9完全开放购买 全面现货供应
  8. 「CJOJ2573」Snake vs Block
  9. 每天干的啥?(2018.06)
  10. C语言程序设计:现代方法(第2版)第二章全部习题答案
  11. java使用UDP数据报通讯方式QQ聊天系统:服务器、客户端
  12. java-OOP-抽象类和接口
  13. 名字打架小游戏 java_闲暇极品MD5 能用名字打架的小游戏
  14. PPP开源软件GMAP测试记录及原始数据比较
  15. 金融用计算机,CFA考试指定计算器使用攻略
  16. android u盘检测工具,android U盘检测及获取内存储器信息
  17. python随机生成电话号码
  18. Python中._pth文件的作用
  19. blendshapes
  20. 2021年7月最新iOS面试题总结(答案篇)

热门文章

  1. An Introduction to Hashing in the Era of Machine Learning
  2. Android系统中的进程管理:内存的回收
  3. Hyperledger Fabric 交易流程
  4. Android开发工具之Android Studio---版本控制SVN使用三(常规操作)
  5. 谷歌浏览器中文版_GitHub上最励志的计算机自学教程:8个月,从中年Web前端到亚马逊百万年薪软件工程师 | 中文版...
  6. sas table将缺失值计入百分比_SAS:通过数据块填充缺失值
  7. 触摸传感器的电路图符号_光电传感器电路图以及应用和优点
  8. Web 3.0 信息聚合的时代
  9. SVM(支持向量机)综述
  10. [JLOI2011]不重复数字