Vibratorは携帯の振動をコントロールします.
16909 ワード
Vibratorは携帯の振動をコントロールします.
効果図
ソース
ダウンロード住所(Android Studioプロジェクト):http://download.csdn.net/detail/q4878802/9049755
権限を追加
例:
親の方法
効果図
ソース
ダウンロード住所(Android Studioプロジェクト):http://download.csdn.net/detail/q4878802/9049755
権限を追加
<!-- -->
<uses-permission android:name="android.permission.VIBRATE" />
ツールクラスpackage com.kongqw.kqwvibrator.engine;
import android.content.Context;
import android.os.Vibrator;
/** * Created by kongqw on 2015/8/26. */
public class KqwVibrator {
private static KqwVibrator mKqwVibrator;
private Context mContext;
private Vibrator mVibrator;
private KqwVibrator(Context context) {
mContext = context;
//
mVibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
}
public static KqwVibrator getInstance(Context context) {
if (null == mKqwVibrator) {
mKqwVibrator = new KqwVibrator(context);
}
return mKqwVibrator;
}
/** * * * @param time */
public void vibrate(long time) {
mVibrator.vibrate(time);
}
/** * */
public void cancle() {
mVibrator.cancel();
}
/** * */
public void repeatVibrate() {
// 1 , 2 , 1 , 3
long[] pattern = {1000, 2000, 1000, 3000};
//-1 , -1, 1, long 1 .
mVibrator.vibrate(pattern, -1);
}
}
テストクラスpackage com.kongqw.kqwvibrator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.kongqw.kqwvibrator.engine.KqwVibrator;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** * 500 * * @param view */
public void button1(View view) {
KqwVibrator.getInstance(this).vibrate(500);
}
/** * * * @param view */
public void button2(View view) {
KqwVibrator.getInstance(this).repeatVibrate();
}
/** * * * @param view */
public void button3(View view) {
KqwVibrator.getInstance(this).cancle();
}
}
ページレイアウト<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="button1"
android:text=" 500 " />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:onClick="button2"
android:text=" " />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2"
android:onClick="button3"
android:text=" " />
</RelativeLayout>
説明mVibrator.vibrate(pattern, -1);
これは間欠的な振動の方法であり、最初のパラメータはlongタイプの配列(ミリ秒)であり、シングル(偶数の角標)は待ち時間であり、偶数(シングルの角標)は振動時間である.例:
// 1 , 2 , 1 , 3
long[] pattern = {1000, 2000, 1000, 3000};
第二のパラメータは循環のパラメータで、次のいくつかの角形から循環するかを表します.-1は循環しないことを表します.親の方法
/**
* Vibrate with a given pattern.
*
* <p>
* Pass in an array of ints that are the durations for which to turn on or off
* the vibrator in milliseconds. The first value indicates the number of milliseconds
* to wait before turning the vibrator on. The next value indicates the number of milliseconds
* for which to keep the vibrator on before turning it off. Subsequent values alternate
* between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
* </p><p>
* To cause the pattern to repeat, pass the index into the pattern array at which
* to start the repeat, or -1 to disable repeating.
* </p>
* <p>This method requires the caller to hold the permission
* {@link android.Manifest.permission#VIBRATE}.
*
* @param pattern an array of longs of times for which to turn the vibrator on or off.
* @param repeat the index into pattern at which to repeat, or -1 if
* you don't want to repeat.
*/
public void vibrate(long[] pattern, int repeat) {
vibrate(pattern, repeat, null);
}
/**
* Vibrate with a given pattern.
*
* <p>
* Pass in an array of ints that are the durations for which to turn on or off
* the vibrator in milliseconds. The first value indicates the number of milliseconds
* to wait before turning the vibrator on. The next value indicates the number of milliseconds
* for which to keep the vibrator on before turning it off. Subsequent values alternate
* between durations in milliseconds to turn the vibrator off or to turn the vibrator on.
* </p><p>
* To cause the pattern to repeat, pass the index into the pattern array at which
* to start the repeat, or -1 to disable repeating.
* </p>
* <p>This method requires the caller to hold the permission
* {@link android.Manifest.permission#VIBRATE}.
*
* @param pattern an array of longs of times for which to turn the vibrator on or off.
* @param repeat the index into pattern at which to repeat, or -1 if
* you don't want to repeat.
* @param attributes {@link AudioAttributes} corresponding to the vibration. For example,
* specify {@link AudioAttributes#USAGE_ALARM} for alarm vibrations or
* {@link AudioAttributes#USAGE_NOTIFICATION_RINGTONE} for
* vibrations associated with incoming calls.
*/
public void vibrate(long[] pattern, int repeat, AudioAttributes attributes) {
vibrate(Process.myUid(), mPackageName, pattern, repeat, attributes);
}