Computer >> Máy Tính >  >> Lập trình >> Android

Làm cách nào để đặt thiết bị Android rung?

Trong Android, sử dụng dịch vụ rung, chúng ta có thể rung điện thoại di động Android. Ví dụ này minh họa về cách làm cho thiết bị Android rung

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào 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>

Trong đoạn mã trên, chúng tôi đã sử dụng textview, khi bạn nhấp vào textview. nó sẽ rung.

Bước 3 - Thêm mã sau vào 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);
            }
         }
      });
   }
}

Trong đoạn mã trên, chúng tôi đã sử dụng dịch vụ rung, nó là dịch vụ hệ thống như hình dưới đây -

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

Trong đoạn mã trên, chúng tôi đã đưa ra 500ms để nó sẽ rung 500ms liên tục.

Bước 4 - Thêm mã sau vào tệp kê khai.java

<?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>

Trong đoạn mã trên, chúng tôi đã cấp quyền cho người dùng rung, nếu không có quyền, nó không thể rung.

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào Chạy Làm cách nào để đặt thiết bị Android rung? biểu tượng từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm cách nào để đặt thiết bị Android rung?

Trong kết quả trên, khi bạn nhấp vào chế độ xem văn bản. nó sẽ rung trong 500ms.