摇一摇


<?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="com.example.cgq.MainActivity">
    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="传感器使用介绍"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="摇晃切换图片"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="摇一摇"
        android:onClick="onClick"/>
    <Button
        android:id="@+id/btn4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="光传感器"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/btn5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="远程服务客户端"
        android:onClick="onClick"/>

</LinearLayout>



package com.example.cgq;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.cgq.app_client.ClientActivity;
import com.example.cgq.demo01.AcceleActivity;
import com.example.cgq.demo02.AcceleChangeImgActivity;
import com.example.cgq.demo03.ShakeActivity;
import com.example.cgq.demo04.SensorKindActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.btn1:
                intent.setClass(this,AcceleActivity.class);
                break;
            case R.id.btn2:
                intent.setClass(this,AcceleChangeImgActivity.class);
                break;
            case R.id.btn3:
                intent.setClass(this,ShakeActivity.class);
                break;
            case R.id.btn4:
                intent.setClass(this,SensorKindActivity.class);
                break;
            case R.id.btn5:
                intent.setClass(this,ClientActivity.class);
                break;
        }
        startActivity(intent);
    }
}
package com.example.cgq;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Created by 帅比浩宇 on 2018/5/18.
 */
public class Book implements Parcelable {
    public int bookId;
    public String bookName;

    protected Book(Parcel in) {
        bookId
                = in.readInt();
        bookName = in.readString();
    }

    public static final Creator<Book> CREATOR = new Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel in) {
            return new Book(in);
        }

        @Override
        public Book[] newArray(int size) {
            return new Book[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(bookId);
        dest.writeString(bookName);
    }
}
// IMyAidlInterface.aidl
package com.example.cgq;

// Declare any non-default types here with import statements

interface IMyAidlInterface {
       int getValues(int a,int b);
       String getInfo();
}
package com.example.cgq.app_client;


import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.example.cgq.IMyAidlInterface;
import com.example.cgq.R;

public class ClientActivity extends AppCompatActivity {
    private IMyAidlInterface iMyAidlInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);
    }
//    服务连接对象
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_bind:
                Intent intent = new Intent();
                intent.setAction("com.example.app_server");
                intent.setPackage("com.example.app_server");
                bindService(intent,conn,BIND_AUTO_CREATE);
                break;
            case R.id.btn_receive:
                try {
                    int values = iMyAidlInterface.getValues(23, 67);
                    Log.i("Client", "values==== "+values);
                    String info = iMyAidlInterface.getInfo();
                    Log.i("Client", "info===="+info);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:fromXDelta="0"
    android:toYDelta="140"
    android:toXDelta="0"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:duration="1000">

</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="-140"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:duration="1000">

</translate>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.cgq.app_client.ClientActivity">
    <Button
        android:id="@+id/btn_bind"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="绑定服务"
        android:onClick="onClick" />

    <Button
        android:id="@+id/btn_receive"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取服务端数据"
        android:onClick="onClick"/>
</LinearLayout>
package com.example.cgq.demo01;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.cgq.R;


/**
 * 开发传感器的应用步骤如下:
 * 1.调用contextgetsystemservicecontext.SENSOR_SERVICE)方法获取SensorManager对象,
 * SensorManager对象代表系统的传感器管理服务。
 * 2.调用SensorManagergetDefaultSensor(int type)方法来获取指定类型的传感器。
 * 3.通常选择在activityonResume()方法中调用SensorManagerregisterListener()方法为
 * 指定的传感器注册监听器,程序通过实现监听器即可获取传感器传回来的数据。
 * SensorManager提供的注册传感器的方法为:registerListener(......);方法中的参数有如下的说明。
 * Listener:监听传感器事件的监听器,该监听器需要实现SensorEventListener接口。
 * 4.activityonPause()方法中调用SensorManagerunregisterListener()方法注销监听器。
 *
 * 学习: 如何使用传感器,传感器在android当中使用的步骤。
 */
public class AcceleActivity extends AppCompatActivity {

    private TextView tv;
    private SensorManager manager;
    private Sensor sensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accele);
        tv = (TextView) findViewById(R.id.tv_acclete);
//        1.获取传感器管理者对象
        manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//        2.获取指定类型的传感器
        sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }

    //     创建传感器监听器对象
    SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
//          当传感器的值发生变化时会回调的方法
            float[] values = event.values;      //传感器获取到的值
            tv.setText("加速度传感器:\nX轴方向的加速度为:"+values[0]+"\ny轴方向的加速度为:"
            +values[1]+"\nz轴方向的加速度为:"+values[2]);
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
//          当传感器的精度发生变化时会回调的方法
        }
    };
    //    获取焦点的方法
    @Override
    protected void onResume() {
        super.onResume();
//        3.注册传感器和传感器监听器
        manager.registerListener(listener,sensor,manager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        4.注销监听器
        manager.unregisterListener(listener);
    }


}
<?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"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv_acclete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<?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"
    android:orientation="vertical"
    android:background="@mipmap/loading1"
    android:id="@+id/change_layout">

</LinearLayout>
package com.example.cgq.demo02;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;

import com.example.cgq.R;


public class AcceleChangeImgActivity extends AppCompatActivity {

    private LinearLayout layout;
    private SensorManager manager;
    private Sensor sensor;
    private int what = 0;
//    获取震动服务对象
    private Vibrator vibrator;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what==0) {
                layout.setBackgroundResource(R.mipmap.loading1);
            }else if (msg.what==1) {
                layout.setBackgroundResource(R.mipmap.loading2);
            }else if(msg.what==2){
                layout.setBackgroundResource(R.mipmap.loading3);
            }else if (msg.what==3){
                layout.setBackgroundResource(R.mipmap.meinv);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accele_change_img);
        layout = (LinearLayout) findViewById(R.id.change_layout);
//        1.获取传感器服务管理对象
        manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//        2.获取传感器对象
        sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//        3.注册传感器监听器
        manager.registerListener(listener,sensor,manager.SENSOR_DELAY_NORMAL);
//        震动服务对象
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    }

    //    创建监听器对象
    SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            float x = values[0];
            float y = values[1];
            float z = values[2];

            int coll = 18;   //作为一个标准值
//            判断在什么情况下要切换图片
            if (Math.abs(x)>coll|Math.abs(y)>coll|Math.abs(z)>coll) {
                /**
                 * 300:摇晃了300毫秒之后,开始震动
                 * 500:震动持续的时间,震动持续了500毫秒。
                 * */
                long[]pattern = {300,500};
                vibrator.vibrate(pattern,-1);
                what++;
                if (what>3) {
                    what = 0;
                }
                handler.sendEmptyMessage(what);
            }
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

//    4.注销传感器监听器
    @Override
    protected void onPause() {
        super.onPause();
        manager.unregisterListener(listener);
    }
}
package com.example.cgq.demo03;

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.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.example.cgq.R;


public class ShakeActivity extends AppCompatActivity {

    private ImageView iv_flower;
    private ImageView iv_up;
    private ImageView iv_down;
    private SensorManager manager;
    private Sensor sensor;
    private Vibrator vibrator;
    private Animation upAnimation;
    private Animation downanimation;
//  声明背景音乐的音效池对象
    private SoundPool pool;
    private int loadId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shake);
        iv_flower =   findViewById(R.id.shake_img);
        iv_up =   findViewById(R.id.img_up);
        iv_down =   findViewById(R.id.img_down);
        manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //传感器管理者
        sensor = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //获取传感器对象
        manager.registerListener(listener,sensor,manager.SENSOR_DELAY_NORMAL);
        vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        initAnimation();  //初始化动画
//        向音效池当中加载音乐
        pool = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
        loadId = pool.load(this, R.raw.r,4);
    }

    SensorEventListener listener = 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轴加速度
            int coll = 15;
            if (Math.abs(x)>coll||Math.abs(y)>coll||Math.abs(z)>coll) {
//                开始震动
                long []pattern = {300,500};
                vibrator.vibrate(pattern,-1);
//                播放背景音乐
                pool.play(loadId,1.0f,1.0f,1,0,1.0f);
//                播放动画
                iv_down.startAnimation(downanimation);
                iv_up.startAnimation(upAnimation);
            }
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

//    4.注销传感器监听器
    @Override
        protected void onPause() {
        super.onPause();
        manager.unregisterListener(listener);
    }

//    5.初始化动画
    private void initAnimation(){
         upAnimation = AnimationUtils.loadAnimation(this, R.anim.up_tran);
        downanimation = AnimationUtils.loadAnimation(this, R.anim.down_tran);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="com.example.cgq.demo03.ShakeActivity"
    android:background="#000000">
    <ImageView
        android:id="@+id/shake_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/shakehideimg_man2"
        android:layout_centerInParent="true"/>

    <LinearLayout
        android:id="@+id/img_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
        <ImageView
            android:id="@+id/img_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/shake_logo_up"/>
        <ImageView
            android:id="@+id/img_down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/shake_logo_down"/>
    </LinearLayout>
</RelativeLayout>
package com.example.cgq.demo04;

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.util.Log;
import android.widget.TextView;


import com.example.cgq.R;

import java.util.List;

public class SensorKindActivity extends AppCompatActivity {

    private TextView tv;
    private SensorManager manager;
    private Sensor sensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor_kind);
        tv = (TextView) findViewById(R.id.kind_tv);
        manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = manager.getDefaultSensor(Sensor.TYPE_LIGHT);  //获取光传感器对象
        manager.registerListener(listener,sensor,manager.SENSOR_DELAY_NORMAL);

//        获取手机当中所有的传感器
        List<Sensor> sensorList = manager.getSensorList(Sensor.TYPE_ALL);
        Log.i("sensor", "当前手机当中传感器的数量: "+sensorList.size());

    }

    SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            tv.setText("光传感器传回的数据:"+values[0]);
        }
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };

    @Override
    protected void onPause() {
        super.onPause();  //注销传感器监听器的操作
        manager.unregisterListener(listener);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.cgq.demo04.SensorKindActivity">
    <TextView
        android:id="@+id/kind_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"/>
</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值