|
Android for mobile phone shake
1. Add vibrate permissions in Androidmanifest.xml
<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Uni.vibrator"
Android:versioncode= "1"
Android:versionname= "1.0" >
<USES-SDK android:minsdkversion= "8"/>
<application android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". Vibratordemoactivity "
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> <uses-permission android:name= "Android.permission.VIBRATE"/>
</manifest>
2. Achieve Vibration
Package uni.vibrator;
Import android.app.Activity;
Import Android.content.Context;
Import Android.os.Bundle;
Import Android.os.Vibrator;
public class Vibratordemoactivity extends Activity {
Private vibrator Vibrator;
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
/*
* To set the vibration size can be set by changing the pattern, if the opening time is too short, the vibration effect may not feel
* */
Vibrator = (Vibrator) getsystemservice (Context.vibrator_service);
Long [] pattern = {100,400,100,400}; Stop Open Stop Open
Vibrator.vibrate (pattern,2); Repeat two times the above pattern if you want to shake only once, index is set to 1
/*
Such as: Only want to shake once, time is 200ms, can write as
Long [] pattern = {0,200}; Stop Open
Vibrator.vibrate (pattern,-1); Repeat two times above pattern if only want to shake once, index set-1
*/
}
public void OnStop () {
Super.onstop ();
Vibrator.cancel (); Stop shaking
}
}
|