1. 若要在應用程式內使用到震動這個服務,需先在AndroidManifest.xml允許存取android.permission.VIBRATE這個權限。


2. MainActivity.java

01.package org.me.android_vibrate;
02.import android.app.Activity;
03.import android.app.Service;
04.import android.os.Bundle;
05.import android.os.Vibrator;
06.import android.view.View;
07.import android.widget.AdapterView;
08.import android.widget.ArrayAdapter;
09.import android.widget.Spinner;
10.import android.widget.Toast;
11. 
12.public class MainActivity extends Activity {
13. 
14.    @Override
15.    public void onCreate(Bundle icicle) {
16.        super.onCreate(icicle);
17.        setContentView(R.layout.main);
18.        Spinner spinner = (Spinner) findViewById(R.id.spinnner);
19.        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this,android.R.layout.simple_spinner_item,new String[]{"長震動","短震動","連續短震動"});
20.        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
21.        spinner.setAdapter(adapter);
22. 
23.        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
24.            public void onItemSelected(AdapterView adapterView, View view, int position, long id){
25.                //取得震動服務
26.                Vibrator myVibrator = (Vibrator) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
27.                switch(position) {
28.                    case 0:
29.                        //震動3秒
30.                        myVibrator.vibrate(3000);
31.                        break;
32.                    case 1:
33.                        //震動0.1秒
34.                        myVibrator.vibrate(100);
35.                        break;
36.                    case 2:
37.                        //停0.01秒之後震動0.1秒(重覆三次)
38.                        myVibrator.vibrate(new long[]{101001010010100}, -1);
39.                        break;
40.                    default:
41.                        break;
42.                }
43.            }
44.            public void onNothingSelected(AdapterView arg0) {
45.                Toast.makeText(MainActivity.this"您沒有選擇任何項目", Toast.LENGTH_LONG).show();
46.            }
47.        });
48.    }
49.}
50.</string></string>


3. main.xml(Layout)

01.<?xml version="1.0" encoding="UTF-8"?>
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03.    android:orientation="vertical"
04.    android:layout_width="fill_parent"
05.    android:layout_height="fill_parent">"
06.    <Spinner
07.        android:id="@+id/spinnner"
08.        android:layout_width="wrap_content"
09.        android:layout_height="wrap_content">
10.    </Spinner>
11.</LinearLayout>


4. AndroidManifest.xml

01.<?xml version="1.0" encoding="UTF-8"?>
02.<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03.     package="org.me.android_vibrate">
04.    <application>
05.         <activity android:name=".MainActivity" android:label="Vibrate">
06.            <intent-filter>
07.                <action android:name="android.intent.action.MAIN"/>
08.                <category android:name="android.intent.category.LAUNCHER"/>
09.            </intent-filter>
10.        </activity>
11.    </application>
12.    <uses-permission android:name="android.permission.VIBRATE" />
13.</manifest>


5. 執行之後的畫面。

 

 

 

創作者介紹
創作者 小犬 的頭像
小犬

正妹 定期更新中.....

小犬 發表在 痞客邦 留言(0) 人氣()