banner



How To Get Phone To Vibrate

How to make an Android device vibrate?


In android, using vibrate service, we can vibrate android mobile. This case demonstrate nigh how to brand an Android device vibrate

Step ane − Create a new projection in Android Studio, go to File ⇒ New Project and fill up all required details to create a new projection.

Step 2 − Add the following lawmaking to res/layout/activity_main.xml.

<?xml version="i.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/parent"    xmlns:tools="http://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>

In the above code, nosotros take taken textview, when you click on textview. information technology will vibrate.

Footstep 3 − Add together the post-obit code to src/MainActivity.java

package com.example.andy.myapplication;  import android.content.Context; import android.content.Intent; import android.os.Build; import android.bone.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);             }          }       });    } }

In the above lawmaking, we accept taken vibrate service, it is system service as shown below -

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); }

In the in a higher place code we take given 500ms so it going to vibrate 500ms continuously.

Step four − Add the post-obit code to manifest.java

<?xml version="1.0" encoding="utf-eight"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"    packet="com.example.andy.myapplication">    <uses-permission android:name="android.permission.VIBRATE" />    <awarding       android:allowBackup="true"       android:icon="@mipmap/ic_launcher"       android:characterization="@string/app_name"       android:roundIcon="@mipmap/ic_launcher_round"       android:supportsRtl="true"       android:theme="@style/AppTheme">       <activity          android:proper noun=".MainActivity"          android:screenOrientation="portrait">          <intent-filter>             <action android:name="android.intent.activeness.MAIN" />             <category android:proper noun="android.intent.category.LAUNCHER" />          </intent-filter>       </activity>    </awarding> </manifest>

In the above code we have given vibrate user permission, without permission it can't vibrate.

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open up one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above result, when you click on text view. it will vibrate for 500ms.

Click here to download the project code

raja

Updated on 30-Jul-2019 22:30:25

  • Related Questions & Answers
  • How to make an Android device vibrate programmatically?
  • How to make an Android device vibrate programmatically using Kotlin?
  • How to brand a call in an Android device using Kotlin?
  • How to make iPhone vibrate using Swift?
  • How to make an alert dialog fill 50% of screen size on Android device?
  • How to find calls in an android device?
  • How to detect Spyware on an Android device?
  • How to check if a bluetooth device is connected with an Android device using Kotlin?
  • How to admission unique Android device ID?
  • How to get programmatically android device name?
  • How to get device id in android?
  • How to become device info in android?
  • How to get device location in android?
  • How to lock the Android device programmatically?
  • How to check if a bluetooth device is connected with Android device?

How To Get Phone To Vibrate,

Source: https://www.tutorialspoint.com/how-to-make-an-android-device-vibrate

Posted by: williamsstectint98.blogspot.com

0 Response to "How To Get Phone To Vibrate"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel