数据存储之Network
Android中发送电子邮件是通过startActivity方法来调用要发送的邮件数据的Intent。我们可以通过putExtra方法来设置邮件的主题、内容、附件等。
发送邮件:
import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.KeyEvent;public class Activity_01 extends Activity {private int mCount = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mCount = 1000; }@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_BACK){//按下退出是保持邮件//发送邮件的地址Uri uri = Uri.parse("mailto:lrc_1986@163.com");//创建IntentIntent intent = new Intent(Intent.ACTION_SENDTO, uri);//设置邮件的主题intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "数据备份");//设置邮件内容intent.putExtra(android.content.Intent.EXTRA_TEXT, "本次计数:" + mCount);//开启startActivity(intent);this.finish();return true;}return super.onKeyDown(keyCode, event);} }
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;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 Activity_01 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); String myString = null; try {//定义我们要访问的地址URL uri = new URL("http://www.baidu.com");//打开这个url链接URLConnection ucon = uri.openConnection();//从上面的链接中取得InputStreamInputStream is = ucon.getInputStream();BufferedInputStream bis = new BufferedInputStream(is);ByteArrayBuffer bab = new ByteArrayBuffer(100);int current = 0;// 一直读到文件结束while((current = bis.read()) != -1){bab.append((byte)current);}myString = new String(bab.toByteArray());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} tv.setText(myString);setContentView(tv); }}