Notes for Android Beginners (2): the simplest code for mobile phone vibration

Beginners are most likely to be confused by the code when they first touch the code, so they should give the minimum generation of core code for beginners, first make clear the core code, and then expand the knowledge, so that the learning effect will be better.

There are a lot of codes on the Internet, which make beginners dizzy. There are four main reasons for the complexity of Android Code:
1. There are too many versions of Android, and the programming environment is relatively complex.
2. The author only talks about the key points, but the non key points will also affect the program execution. There are a few authors who don't talk about the key points, just about the skin, in order to increase the number of downloads.
3. There are too many terms in the author's works, so he can't explain them in the language of life.
4. The code given on the Internet is too complex and has nothing to do with the main functions that beginners want to achieve.

Take mobile phone vibration as an example (Android 5.0 to 7.0 debugging passed):

1. You need to add the permission to use mobile phone vibration in Android manifest.xml. Here is the complete code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jeffersli.myapplication">

    <!-- Turn on mobile phone vibration permission -->
    <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">

            <intent-filter>

              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>
    </application>

</manifest>

2. In the activity main.xml page description file, add two buttons:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jeffersli.myapplication.MainActivity">

    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start shaking"
        tools:layout_editor_absoluteX="135dp"
        tools:layout_editor_absoluteY="50dp" />

    <Button
        android:id="@+id/stopButton"
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="132dp"
        android:layout_marginTop="36dp"
        android:text="Stop shaking"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/startButton" />

</android.support.constraint.ConstraintLayout>

3. Add the following code to the main program MainActivity:

package com.example.jeffersli.myapplication;

import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button startButton,stopButton;
    private Vibrator vibrator;
    private CameraManager m_Camera ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startButton=(Button)findViewById(R.id.startButton);
        stopButton=(Button)findViewById(R.id.stopButton);
        vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Set parameters of vibration
                vibrator.vibrate(new long[]{1000,3000,1000,3000},-1);
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vibrator.cancel();
            }

        });

}
}

This allows the mobile phone to vibrate.

Keywords: Android Mobile xml encoding

Added by my800stuff on Sat, 02 May 2020 06:25:55 +0300