Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owner yyc #32

Merged
merged 8 commits into from
Oct 9, 2020
Merged
8 changes: 4 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".BlowingGame"/>
<activity android:name=".ShakingGame" />
<activity android:name=".About" />
<activity
android:name=".ScreenSaverActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_screen_saver"
android:theme="@style/FullscreenTheme"></activity>
<activity android:name=".CalculateGameActivity" />
<activity android:name=".About" />

<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" />
Expand Down Expand Up @@ -68,10 +69,9 @@
<!-- </activity> -->
<activity android:name=".AnotherActivity" />
<activity android:name=".TestActivity" />

<activity android:name=".StatisticWeekActivity" />
<activity android:name=".StatisticYearActivity" />

</application>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

</manifest>
14 changes: 14 additions & 0 deletions app/src/main/java/com/example/wowtime/AnotherActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
package com.example.wowtime;

import android.os.Bundle;
import android.widget.GridView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.wowtime.adapter.AchievementAdapter;

import java.util.ArrayList;

public class AnotherActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.another);

ArrayList<String> arr=new ArrayList<>();
for (int i=0;i<9;i++){
String title="achievement"+ i;
arr.add(title);
}
AchievementAdapter adapter=new AchievementAdapter(arr,getApplicationContext());
GridView gridView=findViewById(R.id.AchievementTable);
gridView.setAdapter(adapter);
}
}
40 changes: 40 additions & 0 deletions app/src/main/java/com/example/wowtime/BlowingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.wowtime;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.example.wowtime.util.AudioRecordManger;

public class BlowingGame extends AppCompatActivity {
private static final int RECORD = 2; //监听话筒

private void getSoundValues(double values){
//话筒分贝大于50
if (values >50){
Toast.makeText(BlowingGame.this, "你在吹气哦", Toast.LENGTH_SHORT).show();
}
}
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == RECORD) {//监测话筒
double soundValues = (double) msg.obj;
getSoundValues(soundValues);
}
return false;
}
});

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blowing_game);
//调用话筒实现类
AudioRecordManger audioRecordManger = new AudioRecordManger(handler, RECORD); //实例化话筒实现类
audioRecordManger.getNoiseLevel(); //打开话筒监听声音
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/example/wowtime/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
Expand All @@ -14,7 +15,10 @@
import androidx.navigation.ui.NavigationUI;
import com.example.wowtime.databinding.ActivityMainBinding;

import com.example.wowtime.util.SensorManagerHelper;

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/example/wowtime/ShakingGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.wowtime;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

import com.example.wowtime.util.SensorManagerHelper;

public class ShakingGame extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shaking_game);
SensorManagerHelper sensorHelper = new SensorManagerHelper(this);
sensorHelper.setOnShakeListener(() -> Toast.makeText(ShakingGame.this, "你在摇哦", Toast.LENGTH_SHORT).show());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.wowtime.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.wowtime.R;

import java.util.ArrayList;

public class AchievementAdapter extends BaseAdapter {
private ArrayList<String> mData;
private Context mContext;

public AchievementAdapter(ArrayList<String> mData, Context mContext) {

this.mData = mData;
this.mContext = mContext;
}

@Override
public int getCount() {
return mData.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return position;
}

@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {

convertView = LayoutInflater.from(mContext).inflate(R.layout.achievement_item,parent,false);

TextView txt_aName = (TextView) convertView.findViewById(R.id.achievementTitle);

txt_aName.setText(mData.get(position));

return convertView;
}
}
79 changes: 79 additions & 0 deletions app/src/main/java/com/example/wowtime/util/AudioRecordManger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.example.wowtime.util;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class AudioRecordManger {
private static final String TAG = "AudioRecord";//标记
public static final int SAMPLE_RATE_IN_HZ = 8000;//通道配置
public static final int BUFFER_SIZE = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ,
AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);//用于写入声音的缓存
private AudioRecord mAudioRecord;//话筒类
public boolean isGetVoiceRun;//是否录音运行
private Handler mHandler;//句柄
private int mWhat;//动作
public final Object mLock;//对象

public AudioRecordManger(Handler handler, int what) {
mLock = new Object();//同步锁
this.mHandler = handler;//获得句柄
this.mWhat = what;//动作ID
}

public void getNoiseLevel() {
if (isGetVoiceRun) {
Log.e(TAG, "还在录着呢");
return;
}
//创建录音对象,并初始化对象属性
mAudioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
SAMPLE_RATE_IN_HZ, AudioFormat.CHANNEL_IN_DEFAULT,
AudioFormat.ENCODING_PCM_16BIT, BUFFER_SIZE);
//判断话筒对象是否为空
isGetVoiceRun = true;//开启录音

//使用新线程
new Thread(new Runnable() {
@Override
public void run() {
mAudioRecord.startRecording();//录音启动
short[] buffer = new short[BUFFER_SIZE];//设置缓存数组

while (isGetVoiceRun) {
int r = mAudioRecord.read(buffer, 0, BUFFER_SIZE);//r是实际读取的数据长度,一般而言r会小于buffersize
long v = 0;
// 将 buffer 内容取出,进行平方和运算
for (short value : buffer) {
v += value * value;
}
double mean = v / (double) r; // 平方和除以数据总长度,得到音量大小。
double volume = 10 * Math.log10(mean);

// 大概一秒十次,锁
synchronized (mLock) {
try {
mLock.wait(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//声明消息类,句柄发送消息到主窗体函数
Message message = Message.obtain();
message.what = mWhat;
message.obj = volume;
mHandler.sendMessage(message);
}
//话筒对象释放
if (null != mAudioRecord) {
mAudioRecord.stop();
mAudioRecord.release();
mAudioRecord = null;
}
}
}).start();//启动线程
}
}
113 changes: 113 additions & 0 deletions app/src/main/java/com/example/wowtime/util/SensorManagerHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.example.wowtime.util;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class SensorManagerHelper implements SensorEventListener {
// 速度阈值,当摇晃速度达到这值后产生作用
private final int SPEED_SHRESHOLD = 3000;
// 两次检测的时间间隔
private final int UPTATE_INTERVAL_TIME = 50;
// 传感器管理器
private SensorManager sensorManager;
// 传感器
private Sensor sensor;
// 重力感应监听器
private OnShakeListener onShakeListener;
// 上下文对象context
private Context context;
// 手机上一个位置时重力感应坐标
private float lastX;
private float lastY;
private float lastZ;
// 上次检测时间
private long lastUpdateTime;

public SensorManagerHelper(Context context) {
this.context = context;
start();
}

/**
* 开始检测
*/
public void start() {
// 获得传感器管理器
sensorManager = (SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null) {
// 获得重力传感器
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
// 注册
if (sensor != null) {
assert sensorManager != null;
sensorManager.registerListener(this, sensor,
SensorManager.SENSOR_DELAY_GAME);
}
}

/**
* 停止检测
*/
public void stop() {
sensorManager.unregisterListener(this);
}

/**
* 摇晃监听接口
*/
public interface OnShakeListener {
void onShake();
}

/**
* 设置重力感应监听器
*/
public void setOnShakeListener(OnShakeListener listener) {
onShakeListener = listener;
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}

/**
* 重力感应器感应获得变化数据
* android.hardware.SensorEventListener#onSensorChanged(android.hardware
* .SensorEvent)
*/
@Override
public void onSensorChanged(SensorEvent event) {
// 现在检测时间
long currentUpdateTime = System.currentTimeMillis();
// 两次检测的时间间隔
long timeInterval = currentUpdateTime - lastUpdateTime;
// 判断是否达到了检测时间间隔
if (timeInterval < UPTATE_INTERVAL_TIME) return;
// 现在的时间变成last时间
lastUpdateTime = currentUpdateTime;
// 获得x,y,z坐标
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
// 获得x,y,z的变化值
float deltaX = x - lastX;
float deltaY = y - lastY;
float deltaZ = z - lastZ;
// 将现在的坐标变成last坐标
lastX = x;
lastY = y;
lastZ = z;
double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ
* deltaZ)
/ timeInterval * 10000;
// 达到速度阀值,发出提示
if (speed >= SPEED_SHRESHOLD) {
onShakeListener.onShake();
}
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_baseline_phone.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="100dp" android:tint="?attr/colorControlNormal"
android:viewportHeight="24" android:viewportWidth="24"
android:width="100dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M6.62,10.79c1.44,2.83 3.76,5.14 6.59,6.59l2.2,-2.2c0.27,-0.27 0.67,-0.36 1.02,-0.24 1.12,0.37 2.33,0.57 3.57,0.57 0.55,0 1,0.45 1,1V20c0,0.55 -0.45,1 -1,1 -9.39,0 -17,-7.61 -17,-17 0,-0.55 0.45,-1 1,-1h3.5c0.55,0 1,0.45 1,1 0,1.25 0.2,2.45 0.57,3.57 0.11,0.35 0.03,0.74 -0.25,1.02l-2.2,2.2z"/>
</vector>
Loading