Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1618 from lyRomantic/master
Browse files Browse the repository at this point in the history
#6 #7 实验报告六 实验报告七
  • Loading branch information
zengsn authored Jan 10, 2021
2 parents 69df2ce + 9c70d8f commit b035e1f
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 1 deletion.
2 changes: 1 addition & 1 deletion students/net1812070504101/lab4.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 实验三
# 实验四
## 一、实验目标
1.根据选题要求设计界面布局及控件使用
2.布局xml及界面控制操作代码提交并截图
Expand Down
228 changes: 228 additions & 0 deletions students/net1812070504101/lab6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
# 实验六
## 一.实验目标
1.掌握Android网络访问方法;
2.理解XML和JSON表示数据的方法。

## 二.实验内容
1. 从网络下载一个文件(图片、MP3、MP4);
2. 保存到手机,在应用中使用文件;
3. 将应用运行结果截图
## 三.实验步骤
### 1.创建工具类 fileUtils
```
public class FileUtils {
private String SDCardRoot;
public FileUtils(){
//得到当前外部存储设备的目录
SDCardRoot= Environment.getExternalStorageDirectory()+File.separator;
//File.separator为文件分隔符”/“,方便之后在目录下创建文件
}
//在SD卡上创建文件
public File createFileInSDCard(String fileName,String dir) throws IOException {
File file=new File(SDCardRoot+dir+File.separator+fileName);
file.createNewFile();
return file;
}
//在SD卡上创建目录
public File createSDDir(String dir)throws IOException{
File dirFile=new File(SDCardRoot+dir);
dirFile.mkdir();//mkdir()只能创建一层文件目录,mkdirs()可以创建多层文件目录
return dirFile;
}
//判断文件是否存在
public boolean isFileExist(String fileName,String dir){
File file=new File(SDCardRoot+dir+File.separator+fileName);
return file.exists();
}
//将一个InoutStream里面的数据写入到SD卡中
public File write2SDFromInput(String fileName,String dir,InputStream input){
File file=null;
OutputStream output=null;
try {
//创建目录
createSDDir(dir);
//创建文件
file=createFileInSDCard(fileName,dir);
//写数据流
output=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];//每次存4K
int temp;
//写入数据
while((temp=input.read(buffer))!=-1){
output.write(buffer,0,temp);
}
output.flush();
} catch (Exception e) {
System.out.println("写数据异常:"+e);
}
finally{
try {
output.close();
} catch (Exception e2) {
System.out.println(e2);
}
}
return file;
}
}
```
### 2.创建工具类 HttpDownloader
```
public class FileUtils {
private String SDCardRoot;
public FileUtils(){
//得到当前外部存储设备的目录
SDCardRoot= Environment.getExternalStorageDirectory()+File.separator;
//File.separator为文件分隔符”/“,方便之后在目录下创建文件
}
//在SD卡上创建文件
public File createFileInSDCard(String fileName,String dir) throws IOException {
File file=new File(SDCardRoot+dir+File.separator+fileName);
file.createNewFile();
return file;
}
//在SD卡上创建目录
public File createSDDir(String dir)throws IOException{
File dirFile=new File(SDCardRoot+dir);
dirFile.mkdir();//mkdir()只能创建一层文件目录,mkdirs()可以创建多层文件目录
return dirFile;
}
//判断文件是否存在
public boolean isFileExist(String fileName,String dir){
File file=new File(SDCardRoot+dir+File.separator+fileName);
return file.exists();
}
//将一个InoutStream里面的数据写入到SD卡中
public File write2SDFromInput(String fileName,String dir,InputStream input){
File file=null;
OutputStream output=null;
try {
//创建目录
createSDDir(dir);
//创建文件
file=createFileInSDCard(fileName,dir);
//写数据流
output=new FileOutputStream(file);
byte buffer[]=new byte[4*1024];//每次存4K
int temp;
//写入数据
while((temp=input.read(buffer))!=-1){
output.write(buffer,0,temp);
}
output.flush();
} catch (Exception e) {
System.out.println("写数据异常:"+e);
}
finally{
try {
output.close();
} catch (Exception e2) {
System.out.println(e2);
}
}
return file;
}
}
```

### 3.MianActivty文件
```
import androidx.appcompat.app.AppCompatActivity;
//1,在Manifest文件中注册Internet和读写SDCard的权限
//2,下载不能在主线程中进行,要开分线程
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button but1,but2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1=(Button)findViewById(R.id.but1);
but1.setOnClickListener(this);
but2=(Button)findViewById(R.id.but2);
but2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==but1){
new downloadFileThread().start();
}else if(v==but2){
new downloadMP3Thread().start();
}
}
class downloadFileThread extends Thread{
public void run(){
HttpDownloader httpDownloader=new HttpDownloader();
String fileData=httpDownloader.downloadFiles("http://mystudy.bj.bcebos.com/AndroidDemo_009.xml");
System.out.println(fileData);
}
}
class downloadMP3Thread extends Thread{
public void run(){
HttpDownloader httpDownloader=new HttpDownloader();
int downloadResult=httpDownloader.downloadFiles(
"http://fengkui.bj.bcebos.com/%E8%B6%B3%E9%9F%B3.mp3","BoBoMusic","足音.mp3");
System.out.println("下载结果:"+downloadResult);
}
}
}
```
### 4.activity_main.xml xml文件
```
<Button
android:id="@+id/but1"
android:layout_width="305dp"
android:layout_height="77dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="下载XML文件"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.043" />
<Button
android:id="@+id/but2"
android:layout_width="306dp"
android:layout_height="73dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="下载MP3文件"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.193" />
```
## 四.实验结果
<img src="https://github.com/lyRomantic/android-labs-2020/blob/master/students/net1812070504101/lab6_2.png">
<img src="https://github.com/lyRomantic/android-labs-2020/blob/master/students/net1812070504101/lab6_1.png">

## 五.实验心得
这次实验是做网络编程,实现在网络上下载文件的功能,主要难点,如何查看到自己下载的文件,还是通过powershell 在ADK中
/storage/emulated/BoboMusic目录下看到了自己下载的足音.mp3文件
Binary file added students/net1812070504101/lab6_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/net1812070504101/lab6_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 118 additions & 0 deletions students/net1812070504101/lab7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# 实验七

## 一.实验目标
1.总结所学Android知识点;
2.初步掌握从提出需求到完成App开发的基本过程;
3.练习并掌握撰写移动App开发技术报告的方法

## 二.实验内容
1.实现摇一摇的设备功能;
2.编程实现设备使用。

## 三.实验步骤
1.TestSensorActivity Activity文件
```
public class TestSensorActivity extends Activity {
private SensorManager sensorManager;
private Vibrator vibrator;
private static final String TAG = "TestSensorActivity";
private static final int SENSOR_SHAKE = 10;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
if (sensorManager != null) {// 注册监听器
sensorManager.registerListener(sensorEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
// 第一个参数是Listener,第二个参数是所得传感器类型,第三个参数值获取传感器信息的频率
}
}
@Override
protected void onPause() {
super.onPause();
if (sensorManager != null) {// 取消监听器
sensorManager.unregisterListener(sensorEventListener);
}
}
/**
* 重力感应监听
*/
private SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
// 传感器信息改变时执行该方法
float[] values = event.values;
float x = values[0]; // x轴方向的重力加速度,向右为正
float y = values[1]; // y轴方向的重力加速度,向前为正
float z = values[2]; // z轴方向的重力加速度,向上为正
Log.i(TAG, "x轴方向的重力加速度" + x + ";y轴方向的重力加速度" + y + ";z轴方向的重力加速度" + z);
// 一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。
int medumValue = 19;// 三星 i9250怎么晃都不会超过20,没办法,只设置19了
if (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z) > medumValue) {
vibrator.vibrate(200);
Message msg = new Message();
msg.what = SENSOR_SHAKE;
handler.sendMessage(msg);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SENSOR_SHAKE:
Toast.makeText(TestSensorActivity.this, "检测到摇晃,执行操作!", Toast.LENGTH_SHORT).show();
Log.i(TAG, "检测到摇晃,执行操作!");
break;
}
}
};
}
```
## 四.实验结果
<img src="https://github.com/lyRomantic/android-labs-2020/blob/master/students/net1812070504101/lab7_1.png">
<img src="https://github.com/lyRomantic/android-labs-2020/blob/master/students/net1812070504101/lab7_2.png">

## 五.实验体会
本次实验是关于如何调用android设备的编程,本来是想调用摄像机进行拍照功能,但是总是出现 phone keeps stopping的错误,
多方寻找,都没有解决,无奈下做了一个简单的摇一摇功能

























Binary file added students/net1812070504101/lab7_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added students/net1812070504101/lab7_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b035e1f

Please sign in to comment.