Phone shake shake function volume is the same as the system volume

Source: Internet
Author: User

SOURCE Free Download

Layout text file: Main.xml

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:gravity= "Center"
android:background= "#ffffff" >
<textview
Android:id= "@+id/mytextview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "Please shake the phone"
Android:textsize= "30DP"
Android:textcolor= "#000000"
android:gravity= "Center"
/>
</LinearLayout>

Java file: Shakelistener.java

Package com.example.myShake;


Import Android.content.Context;
Import Android.hardware.Sensor;
Import android.hardware.SensorEvent;
Import Android.hardware.SensorEventListener;
Import Android.hardware.SensorManager;
Import Android.util.Log;


/**
* A monitor that detects phone shake
*/
public class Shakelistener implements Sensoreventlistener {
//Speed threshold, which acts when the shaking speed reaches this value
private static final int speed_shreshold = 3000;
//Two time interval of detection
private static final int uptate_interval_time = 70;
//Sensor manager
Private Sensormanager Sensormanager;
//Sensor
Private sensor sensor;
//Gravity sensor listener
Private Onshakelistener Onshakelistener;
//Context
Private Context Mcontext;
//Cell phone position when the gravity sensor coordinates
private float lastx;
private float lasty;
private float Lastz;
//Last detection time
Private long lastupdatetime;


//Builder
Public Shakelistener (Context c) {
//Get Listener Object
Mcontext = C;
Start ();
}


//Start
public void Start () {
//Get the Sensor manager
Sensormanager = (Sensormanager) mcontext
. Getsystemservice (Context.sensor_service);
if (Sensormanager! = null) {
//Get gravity sensor
Sensor = Sensormanager.getdefaultsensor (Sensor.type_accelerometer);
}
//Registration
if (sensor! = NULL) {
Sensormanager.registerlistener (this, sensor,
Sensormanager.sensor_delay_game);
}


}


//Stop detection
public void Stop () {
Sensormanager.unregisterlistener (this);
}


//Set Gravity Sensor listener
public void Setonshakelistener (Onshakelistener listener) {
Onshakelistener = listener;
}


//gravity sensor sensing for change data
public void Onsensorchanged (Sensorevent event) {
//Current detection time
Long currentupdatetime = System.currenttimemillis ();
//Two time interval of detection
Long timeinterval = Currentupdatetime-lastupdatetime;
//Determine if the detection interval is reached
if (TimeInterval < uptate_interval_time)
Return
//The time now becomes last time
LastUpdateTime = Currentupdatetime;


Get x, y coordinates
float x = event.values[0];
Float y = event.values[1];
float z = event.values[2];


//Get change values for x, Y, z
float deltax = x-lastx;
float DeltaY = y-lasty;
float Deltaz = Z-lastz;


//Turn the current coordinates into the last coordinates
LASTX = x;
Lasty = y;
Lastz = Z;


Double speed = math.sqrt (DeltaX * deltax + deltay * deltay + Deltaz
* Deltaz)
/timeinterval * 10000;
LOG.V ("Thelog", "===========log===================");
//To reach the speed threshold, send a hint
if (speed >= speed_shreshold) {
Onshakelistener.onshake ();
}
}


public void onaccuracychanged (sensor sensor, int accuracy) {


}


//Shake Monitoring interface
Public interface Onshakelistener {
public void Onshake ();
}


}

Java file: Myactivity.java

Package com.example.myShake;


Import android.app.Activity;
Import Android.app.Service;
Import Android.content.Context;
Import Android.media.AudioManager;
Import Android.media.SoundPool;
Import Android.os.Bundle;
Import Android.os.Vibrator;
Import Android.util.Log;
Import Android.widget.TextView;


public class MyActivity extends Activity {
Private Audiomanager Audiomanager;//class for managing audio
Private Soundpool Soundpool;//Declare a soundpool
private int music, music2;//;to set the Suondid
Private Shakelistener Mshakelistener=null;
public static long Grouplastupdatetime;
Private TextView Mytextview;
/**
* Called when the activity is first created.
*/
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
mytextview= (TextView) Findviewbyid (R.id.mytextview);
Mshakelistener = new Shakelistener (this);
Setlisteners ();
Initstate ();


}
//Add a shake sound
private void Initstate () {
Soundpool = new Soundpool (Ten, Audiomanager.stream_system, 5);The first parameter is the maximum number of simultaneous data streams, the second data stream type, and the third is the sound quality
Music = Soundpool.load (This, R.raw.shake_sound_male, 1);//Put your sound material in the Res/raw, the 2nd parameter is the resource file, and the 3rd is the priority of the music
Music2 = Soundpool.load (This, r.raw.shake_sound, 1);
}
//Add Shake to monitor
private void Setlisteners () {
Mshakelistener.setonshakelistener (New Shakelistener.onshakelistener () {
public void Onshake () {
System.out.println ("<<<<<<" + "Group");
Long currentupdatetime = System.currenttimemillis ();
Long timeinterval = Currentupdatetime-grouplastupdatetime;
//Set to shake once every three seconds
if (TimeInterval < 3000)
Return
Grouplastupdatetime = Currentupdatetime;
//Mobile phone shake effect
Vibrator VVi = (vibrator) myactivity.this
. Getsystemservice (Service.vibrator_service);

//Here error, what security anomalies, small Five also do not know why, look master pointing
//Vvi.vibrate (+);
//Play audio, can set the left and right volume, can set priority, number of cycles and rate
Rates up to 2, minimum 0.5, normal 1
float volumenum = (float) getvolume ()/7;
int streamid = Soundpool.play (music, 1, 1, 0, 0, (float) 1.4);
Soundpool.setvolume (Streamid, Volumenum, volumenum);
Soundpool.play (music, 1, 1, 0, 0, 1);//Play sound
Thread Thread=new Thread (new Runnable () {
@Override
public void Run () {
try {


Thread.Sleep (2000);
float volumenum = (float) getvolume ()/7;
int streamid = Soundpool.play (Music2, 1, 1, 0, 0, (float) 1.4);
Soundpool.setvolume (Streamid, Volumenum, volumenum);//Play sound
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
});
Thread.Start ();
Mytextview.settext ("Hello world!!!");


}
});
}
//Get current system volume 0~7
private int Getvolume () {
int volume =-1;
Audiomanager = (Audiomanager) getsystemservice (Context.audio_service);
Volume = Audiomanager.getstreamvolume (audiomanager.stream_ring);
LOG.I ("stream_ring", "" "+ volume);
return volume;
}
}

SOURCE Free

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.