Android 小项目之--数据存储【Network】(附源码)
将数据保存发送到电子邮件中备份,首要前提应该先在模拟器中设置好电子邮件帐户,设置如下:
- 第一步,启动模拟器,打开“菜单”,选择“电子邮件”项,填写相应帐号和密码。
- 第二步,点击NEXT,程序自动配置电子邮件相关信息。
- 第三步,配置完成后,输入相应的名称信息,即可设置完成,接下来程序会默认帮你导入你输入邮箱帐号的相关信息
发送信息保存到邮箱
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
public class networkActivity extends Activity {
private EditText myEditText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
myEditText = (EditText)findViewById(R.id.myEditText4);
}
@Override
public boolean onKeyDown( int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// 发送邮件地址
Uri uri = Uri.parse( " mailto:285735942@qq.com " );
// 创建intent
Intent it = new Intent(Intent.ACTION_SENDTO,uri);
it.putExtra(android.content.Intent.EXTRA_SUBJECT, " 网络存储 " );
it.putExtra(android.content.Intent.EXTRA_TEXT, myEditText.getText());
startActivity(it);
this .finish();
return true ;
}
return super .onKeyDown(keyCode, event);
}
}
读取XML代码参考
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class readxmlActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super .onCreate(savedInstanceState);
setContentView(R.layout.readxml);
TextView tv = (TextView)findViewById(R.id.TextView01);
String msg = "" ;
try {
URL url = new URL( " http://www.az1314.com/txt/00.txt " ); // 要访问的数据文件
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer( 100 );
int current = 0 ;
while ((current = bis.read()) != - 1 ) {
baf.append(( byte )current);
}
msg = new String(baf.toByteArray());
} catch (Exception e) {
// TODO: handle exception
msg = e.getMessage();
}
tv.setText(msg);
}
}
- URL(String spec)
通过传进来的字符串分析,创建一个新的URL实例。 - URL(URL context, String spec)
通过传进来的字符串分析,创建一个新的URL实例。需要一个URL的参数 - URL(URL context, String spec, URLStreamHandler handler)
通过传进来的字符串分析,创建一个新的URL实例 - URL(String protocol, String host, String file)
使用给定的参数创建一个URL实例,需要指定协议,主机文件名 - URL(String protocol, String host, int port, String file)
使用给定的参数创建一个URL实例,需要指定协议,主机,端口和文件名 - URL(String protocol, String host, int port, String file, URLStreamHandler handler)
使用给定的参数创建一个URL实例,需要指定协议,主机,端口、文件名和处理程序
- 用以来实现提供一个具有特定协议类的连接源。
- getInputStream 主要用来读取一个URLConnection的资源数据,返回一个InputStream,本文将使用这个方法获取数据
android:versionName="1.0">
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".networkActivity"
android:label ="@string/app_name" >
</ activity >
< activity android:name =".readxmlActivity" >< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter ></ activity >
</ application >
< uses-permission android:name ="android.permission.INTERNET" />