Android 应用开发之vibrator

来源:互联网 发布:ieee编程语言排行榜 编辑:程序博客网 时间:2024/06/08 02:55

http://blog.csdn.net/shaojie519/article/details/6915998  --example

http://mobile.csdn.net/a/20110301/292785.html   ---系统架构

http://www.cnblogs.com/wt616/archive/2011/06/20/2085222.html ----button使用方法

http://www.2cto.com/kf/201111/112226.html



http://developer.android.com/reference/android/widget/Button.html ---button

http://developer.android.com/reference/android/os/Vibrator.html  ---vibrator




一。  VibratorTest.java

package com.android.vibratortest;

import android.app.Activity;
import android.os.Vibrator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Context;

public class VibratorActivity extends Activity {
    /** Called when the activity is first created. */
    private Vibrator vib;
    private Button btnOk;
    private Button btnCancel;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
    init();
    btnOk.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            vib.vibrate(10000);
            
            showToast("ok");
        }
    }
    );
    
    btnCancel.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            vib.cancel();
            showToast("cancel");
        }
    }
    );
    
    }
    
    
    private void  init(){  
        btnOk = (Button) findViewById(R.id.btnOk);
        btnCancel = (Button) findViewById(R.id.btnCancel);
        vib = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);  
    }
    
    private void showToast(String msg){  
        Toast.makeText(this, msg, 1).show();  
    }  

}


二。manifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.vibratortest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".VibratorActivity" >
            <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>



三。 string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">VibratorTest</string>
    <string name="btnOk">开始震动</string>
    <string name="btnCancel">停止震动</string>
</resources>


四。 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnOk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnOk" />


    <Button
        android:id="@+id/btnCancel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnCancel" />

</LinearLayout>


原创粉丝点击