Android 传感器
加速度传感器、步伐传感器、仿微信摇一摇
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/777ec0881bd18a7e06d4c41725189cf0.gif)
Java代码(主类)
package bw.com.Unit8;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import bw.com.Unit1.R;
public class SensorActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sensor);
initView();
}
private void initView() {
findViewById(R.id.btn_Aeec).setOnClickListener(this);
findViewById(R.id.btn_Step).setOnClickListener(this);
findViewById(R.id.btn_yao).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
switch (v.getId()){
case R.id.btn_Aeec:
intent.setClass(this,AeecActivity.class);
break;
case R.id.btn_Step:
intent.setClass(this,StepActivity.class);
break;
case R.id.btn_yao:
intent.setClass(this,YaoActivity.class);
break;
}
startActivity(intent);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bw.com.Unit8.SensorActivity">
<Button
android:id="@+id/btn_Aeec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加速度传感器"
/>
<Button
android:id="@+id/btn_Step"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="步伐传感器"
/>
<Button
android:id="@+id/btn_yao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="仿微信摇一摇"
/>
</LinearLayout>
加速度传感器
package bw.com.Unit8;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import bw.com.Unit1.R;
public class AeecActivity extends AppCompatActivity implements SensorEventListener {
private TextView textView;
private SensorManager manager;
private Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aeec);
initView();
initSensor();
}
@Override
protected void onResume() {
super.onResume();
manager.registerListener(this,sensor,200);
}
@Override
protected void onPause() {
super.onPause();
manager.unregisterListener(this);
}
private void initSensor() {
manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
private void initView() {
textView = findViewById(R.id.tv_Aeec);
}
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
textView.setText("x轴加速度="+x+";y轴加速度="+y+";z轴加速度="+z);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
步伐传感器
package bw.com.Unit8;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import bw.com.Unit1.R;
public class StepActivity extends AppCompatActivity implements SensorEventListener {
private TextView textView;
private SensorManager sensorManager;
private Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_step);
initView();
initSensor();
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,sensor,200);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
private void initSensor() {
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
}
private void initView() {
textView = findViewById(R.id.tv_Step);
}
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
float num = values[0];
textView.setText("步数为:"+num);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
仿微信摇一摇
package bw.com.Unit8;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import bw.com.Unit1.R;
public class YaoActivity extends AppCompatActivity implements SensorEventListener {
private ImageView imageView1;
private ImageView imageView2;
private SensorManager sensorManager;
private Sensor sensor;
private Vibrator vibrator;
private SoundPool soundPool;
private TranslateAnimation translateAnimationup;
private TranslateAnimation translateAnimationdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_yao);
initView();
initSensor();
}
private void initSensor() {
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC,1);
soundPool.load(this,R.raw.wx,1);
translateAnimationup = new TranslateAnimation(0,0,0,-100);
translateAnimationdown = new TranslateAnimation(0,0,0,100);
}
private void initView() {
imageView1 = findViewById(R.id.iv_up);
imageView2 = findViewById(R.id.iv_down);
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(this,sensor,200);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
if (Math.abs(x)>15 || Math.abs(y)>15 || Math.abs(z)>15){
long[] patten = {300,500};
vibrator.vibrate(patten,-1);
soundPool.play(1,1,1,1,1,1);
translateAnimationup.setDuration(2000);
translateAnimationdown.setDuration(2000);
imageView1.startAnimation(translateAnimationup);
imageView2.startAnimation(translateAnimationdown);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
百度地图
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/45fb220136decf5957ced32b99e541f7.png)
清单文件
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a2f130bdcf9833ac8fc826a2af0c3cdb.png)
Java代码(主类)
package bw.com.baidu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRoutePlanOption;
import com.baidu.mapapi.search.route.WalkingRouteResult;
public class MainActivity extends AppCompatActivity {
private MapView mMapView = null;
private RoutePlanSearch mSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.bmapView);
mSearch = RoutePlanSearch.newInstance();
mSearch.setOnGetRoutePlanResultListener(listener);
PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", "西二旗地铁站");
PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", "百度科技园");
mSearch.walkingSearch((new WalkingRoutePlanOption())
.from(stNode)
.to(enNode));
}
OnGetRoutePlanResultListener listener = new OnGetRoutePlanResultListener() {
@Override
public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
}
@Override
public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
}
@Override
public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
}
@Override
public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
}
@Override
public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {
}
@Override
public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
}
};
@Override
protected void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
mSearch.destroy();
}
}
Application类
package bw.com.baidu;
import android.app.Application;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SDKInitializer.initialize(this);
SDKInitializer.setCoordType(CoordType.BD09LL);
}
}