android 数据存储----android短信发送器之文件的读写(手机+SD卡)
本文實踐知識點有有三:
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>l2.布局文件里使用字符以及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.操作文件的功能實現最好新建一個業務類
注意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卡)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Hadoop] 启动HDFS缺少服务
- 下一篇: 洛谷 - P2444 - 病毒 - AC