Android 震动 和 停止 代码
<span style="font-size:18px;">MainActivity.java</span>
package com.Utils.vibrate; import android.app.Activity; import android.app.ActionBar; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.os.Build; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button)findViewById(R.id.vibrate_btn); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //VibatorUtils.Vibrate(MainActivity.this,5000); long a[] = { 0,2000 } ; VibatorUtils.Vibrate(MainActivity.this,a,true); } }); } }
VibratorUtils.java (此处转自:http://blog.csdn.net/h7870181/article/details/8166987 )
package com.Utils.vibrate; import android.app.Activity; import android.app.Service; import android.os.Vibrator; public class VibatorUtils { /** * final Activity activity :调用该方法的Activity实例 * long milliseconds :震动的时长,单位是毫秒 * long[] pattern :自定义震动模式 。数组中数字的含义依次是[静止时长,震动时长,静止时长,震动时长。。。]时长的单位是毫秒 * boolean isRepeat : 是否反复震动,如果是true,反复震动,如果是false,只震动一次 * 关闭震动,只需调用 Vibrator.cancel()函数即可 */ public static void Vibrate(final Activity act, long ms){ Vibrator vib = (Vibrator) act.getSystemService(Service.VIBRATOR_SERVICE); vib.vibrate(ms); } public static void Vibrate(final Activity act, long[] pattern,boolean isRepeat){ Vibrator vib = (Vibrator) act.getSystemService(Service.VIBRATOR_SERVICE); vib.vibrate(pattern, isRepeat ? 1 : -1); } }
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.Utils.vibrate.MainActivity" tools:ignore="MergeRootFrame" > <Button android:id="@+id/vibrate_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/vibate" /> </FrameLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Utils.vibrate" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.VIBRATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.Utils.vibrate.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
string.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">vibrate</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="vibate">震动</string> </resources>
代码已在MX2 (Android 4.4) 上测试成功