वाइब्रेशन सर्विस का उपयोग
Android में वाइब्रेटर सर्विस का उपयोग करके डिवाइस को वाइब्रेट किया जा सकता है। यह उदाहरण दर्शाता है कि Android डिवाइस को प्रोग्रामेटिकली कैसे वाइब्रेट करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML जोड़ें
निम्न कोड को res/layout/activity_main.xml में जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:background="#33FFFF00"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:textSize="18sp"
android:text="Click here to vibrate"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ऊपर के कोड में, हमने एक TextView लिया है, जिस पर क्लिक करने पर डिवाइस वाइब्रेट होगा।
चरण 3: MainActivity जावा कोड जोड़ें
निम्न कोड को src/MainActivity.java में जोड़ें:
package com.example.andy.myapplication;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
final LinearLayout parent = findViewById(R.id.parent);
textView = findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(500);
}
}
});
}
}
वाइब्रेशन कोड समझाया गया
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
vibrator.vibrate(500);
}
ऊपर के कोड में हमने 500ms दिया है, इसलिए यह 500ms तक लगातार वाइब्रेट करेगा।
चरण 4: मैनिफेस्ट में परमिशन जोड़ें
निम्न कोड को AndroidManifest.xml में जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.myapplication">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऊपर के कोड में हमने वाइब्रेट यूजर परमिशन दी है, बिना परमिशन के यह वाइब्रेट नहीं कर सकता।
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें:

ऊपर के परिणाम में, जब आप TextView पर क्लिक करते हैं, तो यह 500ms के लिए वाइब्रेट करेगा।