//1.创建意图
Intent intent = new Intent();
//2.把动作封装至意图中
intent.setAction(Intent.ACTION_CALL);
//3.打电话打给谁
intent.setData(Uri.parse("tel:" + number));
//4.告诉系统,我的动作
startActivity(intent);
//第一种
bt_iq.setOnClickListener(new OnClickListener() {
//v:触发该方法执行的组件对象
@Override
public void onClick(View v) {
System.out.println("智商已-10");
}
});
//第二种
public void click1(View v){
//获取组件的资源id
int id = v.getId();
switch (id) {
case R.id.bt1:
System.out.println("按钮1");
break;
case R.id.bt2:
System.out.println("按钮2");
break;
case R.id.bt3:
System.out.println("按钮3");
break;
}
}
//第三种,实现OnclickListener的接口
//直接使用发送短信的api,无需启动系统的短信应用
SmsManager sm = SmsManager.getDefault();
//把长短信截成若干条短短信
ArrayList<String> sms = sm.divideMessage(content);
for (String string : sms) {
//arg0:目标号码
//arg1:短信中心号码,null表示使用默认
//arg2:短信正文
sm.sendTextMessage(phone, null, string, null, null);
}
}
//写
//返回一个File对象,封装的路径是data/data/com.itheima.rwinrom/cache
File file = new File(getCacheDir(), "info.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
//把账号密码写入本地文件
fos.write((name + "&&" + pass).getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//读
File file = new File(getCacheDir(), "info.txt");
if(file.exists()){
try {
FileInputStream fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//读取文件中的文本
String text = br.readLine();
String s[] = text.split("&&");
//给输入框设置文本
et_name.setText(s[0]);
et_pass.setText(s[1]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
写
public void login(View v){
//获取用户输入的账号密码
String name = et_name.getText().toString();
String pass = et_pass.getText().toString();
CheckBox cb = (CheckBox) findViewById(R.id.cb);
//判断选框是否选中
if(cb.isChecked()){
// File file = new File("sdcard/info.txt");
//MEDIA_REMOVED:sd卡不存在
//MEDIA_UNMOUNTED:sd卡存在,但是没有挂载
//MEDIA_CHECKING:sd卡正在遍历
//MEDIA_MOUNTED:sd卡可用
//MEDIA_MOUNTED_READ_ONLY:sd卡可用,但是只读
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
//返回一个File对象,封装了外部存储的真实路径
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
//把账号密码写入本地文件
fos.write((name + "&&" + pass).getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else{
Toast.makeText(this, "SD卡不可用哟亲(づ ̄ 3 ̄づ", 0).show();
}
}
// System.out.println("登录成功");
//创建吐司对话框
Toast t = Toast.makeText(this, "登录成功", 0);
//显示对话框
t.show();
}
读
private void readAccount() {
// File file = new File("sdcard/info.txt");
File file = new File(Environment.getExternalStorageDirectory(), "info.txt");
if(file.exists()){
try {
FileInputStream fis = new FileInputStream(file);
//把字节流转换成字符流
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//读取文件中的文本
String text = br.readLine();
String s[] = text.split("&&");
//给输入框设置文本
et_name.setText(s[0]);
et_pass.setText(s[1]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize;
long totalBlocks;
long availableBlocks;
//判断当前版本是否是4.3或以上
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
blockSize = stat.getBlockSizeLong();
totalBlocks = stat.getBlockCountLong();
availableBlocks = stat.getAvailableBlocksLong();
}
else{
blockSize = stat.getBlockSize();
totalBlocks = stat.getBlockCount();
availableBlocks = stat.getAvailableBlocks();
}
String text = formatSize(availableBlocks * blockSize);
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(text);
}
private String formatSize(long size) {
return Formatter.formatFileSize(this, size);
}
可读可写权限已经过时,官方不推荐
public void click1(View v){
//路径默认为data/data/com.itheima.permission/files
try {
FileOutputStream fos = openFileOutput("info1.txt", MODE_PRIVATE);
fos.write("hahaha".getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void click2(View v){
//路径默认为data/data/com.itheima.permission/files
try {
@SuppressWarnings("deprecation")
FileOutputStream fos = openFileOutput("info2.txt", MODE_WORLD_READABLE);
fos.write("lalala".getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void click3(View v){
//路径默认为data/data/com.itheima.permission/files
try {
@SuppressWarnings("deprecation")
FileOutputStream fos = openFileOutput("info3.txt", MODE_WORLD_WRITEABLE | MODE_WORLD_READABLE);
fos.write("ohohohohoho".getBytes());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void readAccount() {
//获取sharedpreference
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
String name = sp.getString("name", "");
String pass = sp.getString("pass", "");
et_name.setText(name);
et_pass.setText(pass);
}
public void login(View v){
//获取用户输入的账号密码
String name = et_name.getText().toString();
String pass = et_pass.getText().toString();
CheckBox cb = (CheckBox) findViewById(R.id.cb);
//判断选框是否选中
if(cb.isChecked()){
//获取sharedpreference
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
//获取编辑器
Editor ed = sp.edit();
ed.putString("name", name);
ed.putString("pass", pass);
//提交
ed.commit();
}
//创建吐司对话框
Toast t = Toast.makeText(this, "登录成功", 0);
//显示对话框
t.show();
}
//sms是自定义类
public void click(View v){
//获取xml序列化器
XmlSerializer xs = Xml.newSerializer();
File file = new File("sdcard/sms2.xml");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
//初始化
//xml文件中什么编码生成
xs.setOutput(fos, "utf-8");
//开始生成xml文件
//生成头结点
xs.startDocument("utf-8", true);
//生成开始标签
xs.startTag(null, "smss");
for (Sms sms : smsList) {
xs.startTag(null, "sms");
xs.startTag(null, "body");
xs.text(sms.getBody() + "<body>");
xs.endTag(null, "body");
xs.startTag(null, "type");
xs.text(sms.getType() + "");
xs.endTag(null, "type");
xs.startTag(null, "date");
xs.text(sms.getDate() + "");
xs.endTag(null, "date");
xs.startTag(null, "address");
xs.text(sms.getAddress());
xs.endTag(null, "address");
xs.endTag(null, "sms");
}
//生成结束标签
xs.endTag(null, "smss");
//告知序列化器生成xml结束
xs.endDocument();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void click(View v){
//获取天气信息xml文件
InputStream is = getClassLoader().getResourceAsStream("weather.xml");
//获取xmlpull解析器
XmlPullParser xp = Xml.newPullParser();
try {
//初始化
xp.setInput(is, "gbk");
//获取当前节点的事件类型
int type = xp.getEventType();
City city = null;
while(type != XmlPullParser.END_DOCUMENT){
switch (type) {
case XmlPullParser.START_TAG:
//获取当前节点的名字
if("weather".equals(xp.getName())){
cityList = new ArrayList<City>();
}
else if("city".equals(xp.getName())){
city = new City();
}
else if("name".equals(xp.getName())){
//获取下一个节点的文本
String name = xp.nextText();
city.setName(name);
}
else if("temp".equals(xp.getName())){
//获取下一个节点的文本,把指针移动至当前节点的结束节点
String temp = xp.nextText();
city.setTemp(temp);
}
else if("pm25".equals(xp.getName())){
//获取下一个节点的文本
String pm25 = xp.nextText();
city.setPm25(pm25);
}
break;
case XmlPullParser.END_TAG:
if("city".equals(xp.getName())){
cityList.add(city);
}
break;
}
//把指针移动至下一个节点,并返回该节点的事件类型
type = xp.next();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (City city : cityList) {
System.out.println(city.toString());
}
}
权限
<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.junit" ></instrumentation>
test类代码:
public class Test extends AndroidTestCase {
public void test(){
int result = Tools.damage(8, 5);
//断言:检测预期和实际结果是否一致
assertEquals(3, result);
}
public void test2(){
Tools.showTeXiao();
}
}