您的位置:首页 > 编程语言

安卓初学者笔记(二):手机震动最简代码

2017-12-29 15:10 260 查看
初学者刚接触代码的时候,最容易被很代码搞晕,所以应当给初学者最小代的核心代码,先搞清楚核心代码,再进行知识扩展,这样学习的效果才会比较好。

网上有很多代码,让初学看的头晕眼花,安卓代码复杂的原因,主要有四个:

1、安卓本身的版本太多,编程环境相对复杂。

2、作者只讲关键点,但是不关键的地方也会影响程序执行。还有少数作者是绕着关键点不讲,只讲皮毛,目的是为了增加下载量。

3、作者术语太多,没有深入浅出,不会用生活化的语言讲解。

4、网上给出的代码太复杂,与初学者想实现的主要功能无关。

以手机震动为例(安卓5.0至7.0调试通过):

1、需要在AndroidManifest.xml增加手机震动的使用权限。以下是完整代码:

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

<intent-filter>

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

</intent-filter>

</activity>
</application>

</manifest>


2、在activity_main.xml页面描述文件中,增加两个按钮:

<?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="开始震动"
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="停止震动"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/startButton" />

</android.support.constraint.ConstraintLayout>


3、在主程序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) {
//设置震动的参数
vibrator.vibrate(new long[]{1000,3000,1000,3000},-1);
}
});

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

});

}
}


这样让手机震动的功能就实现了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: