Skip to content

Commit

Permalink
hzuapps#420 实验报告5-6
Browse files Browse the repository at this point in the history
  • Loading branch information
BBsan13 committed Dec 5, 2020
1 parent f75b147 commit 74ddb4a
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 0 deletions.
Binary file added students/net1814080903139/lab5.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions students/net1814080903139/lab5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
## 一、实验目标

1. 了解Android的存储手段
2. 掌握Android的文件存储
3. 掌握Android的数据库存储

## 二、实验内容

1. 将应用产生的数据保存到文件存储中;
2. 说明使用的文件存储方式:内部 or 外部;
3. 将运行结果截图。

# 三、实验步骤

1. 保存闹钟

```java
private void saveAlarmList() {
Editor editor = getContext().getSharedPreferences(
AlarmView.class.getName(), Context.MODE_PRIVATE).edit();

StringBuffer sb = new StringBuffer();

for (int i = 0; i < adapter.getCount(); i++) {
sb.append(adapter.getItem(i).getTime()).append(",");
}
if (sb.length() > 1) {
String content = sb.toString().substring(0, sb.length() - 1);
editor.putString(KEY_ALARM_LIST, content);

System.out.println(content);
} else {
editor.putString(KEY_ALARM_LIST, null);
}
editor.commit();
}

private void readSaveAlarmList() {
SharedPreferences sp = getContext().getSharedPreferences(
AlarmView.class.getName(), Context.MODE_PRIVATE);
String content = sp.getString(KEY_ALARM_LIST, null);

if (content != null) {
String[] timeStrings = content.split(",");
for (String string : timeStrings) {
adapter.add(new AlarmData(Long.parseLong(string)));
}
}
}
```

2. 删除闹钟

```java
private void deleteAlarm(int position) {
AlarmData ad = adapter.getItem(position);
adapter.remove(ad);
saveAlarmList();

alarmManager.cancel(PendingIntent.getBroadcast(getContext(),
ad.getId(), new Intent(getContext(), AlarmReceiver.class), 0));
}
```

# 四、运行结果及截图

如图所示:添加闹钟-->删除闹钟

![lab5](D:\Desktop\Markdown\移动应用开发\lab5.PNG)



# 五、心得体会

​ 通过本次的实验,我学会了如何使用内部存储。这次实验对我来说难度较大,刚开始是在菜鸟教程学习如何使用,但是这个过程中一直报错,最后在百度上找到一些博客解决了我的问题。
Binary file added students/net1814080903139/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.
121 changes: 121 additions & 0 deletions students/net1814080903139/lab6 .md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
实验六:Android网络编程

## 一、实验目标

1. 掌握Android网络访问方法;
2. 理解XML和JSON表示数据的方法。

```
注意:选择实现的功能,一定要跟你的选题相关!
```

## 二、实验内容

1. 从网络下载一个文件(图片、MP3、MP4);
2. 保存到手机,在应用中使用文件;
3. 将应用运行结果截图。

## 三、实验步骤

1. 在 AndroidManifest.xml 上添加网络权限

```
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
```

```java
<receiver
android:name=".AlarmReceiver"
android:screenOrientation="portrait">
<!--android:enabled="true"-->
<!--android:exported="true"-->

<intent-filter>
<action android:name="android.intent.cation.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
```

2. 在layout创建alarm_play_aty.xml作为闹钟播放界面

```java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Play Sound"/>
</LinearLayout>

```

3. 修改AlarmActivity.java

```
public class AlarmActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceSate){
super.onCreate(savedInstanceSate);
setContentView(R.layout.alarm_play_aty);
mp = MediaPlayer.create(this,R.raw.music);
mp.start();
}
@Override
protected void onPause() {
super.onPause();
finish();
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.stop();
mp.release();
}
private MediaPlayer mp;
}
```

4. 创建AlarmReceive.java

```
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("闹钟执行了");
AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.cancel(PendingIntent.getBroadcast(context,getResultCode(),new Intent(context,AlarmReceiver.class),0));
Intent i = new Intent(context,AlarmActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(i);
}
}
```

4.在resource创建raw文件,引入下载的music.mp3作为闹钟铃声

## 四、实验结果及截图

![lab6(2)](D:\Desktop\Markdown\移动应用开发\lab6(2).PNG)

如下图:闹钟播放界面

![lab6 (1)](D:\Desktop\Markdown\移动应用开发\lab6 (1).PNG)



## 五、实验心得

通过这次实验,我学习了如何使用Android多媒体框架中的一个重要组件MediaPlayer和广播接收器,在这个实验中值得注意的是,在使用广播接收器时一定要在AndroidManifest.xml中注册广播接收器来监听制定的广播意图,无论什么时候Android设备被启动,都将被广播接收器Receiver所拦截,并且在onReceive()中实现的逻辑将被执行。
Binary file added students/net1814080903139/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.

0 comments on commit 74ddb4a

Please sign in to comment.