Android该系统提供的服务–Vibrator(振子)

Android该系统提供的服务–Vibrator(振子)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Android该系统提供的服务–Vibrator(振子)

                                                          ——转载请注明出处:coder-pig



Vibrator简单介绍与相关方法:


Android该系统提供的服务--Vibrator(振子)




简单demo——设置频率不同的振动器


对于Vibrator用的最广泛的莫过于所谓的手机按摩器类的app,在app市场一搜,一堆,笔者随便下了几个下来瞅瞅

,都是大同小异的,这点小玩意居然有8W多的下载量…好吧,好像也不算多,只是普遍功能都是切换振动频率来完毕

所谓的按摩效果,是否真的有效就不得而知了,那么接下来

我们就来实现一个简单的按摩器吧!

核心事实上就是vibrate()中的数组的參数,依据自己需求写一个数组就能够了!

由于模拟器不会振动的,所以须要在手机上执行才会有效果哦!

效果图:

Android该系统提供的服务--Vibrator(振子)

代码也非常easy,布局的话就四个简单的button而已

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.jay.example.vibratordemo.MainActivity" >

    <Button
        android:id="@+id/btnshort"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="短振动" />

    <Button
        android:id="@+id/btnlong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="长振动" />
    
    <Button
        android:id="@+id/btnrhythm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="节奏振动" />  
        
    <Button
        android:id="@+id/btncancle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消振动" />       
</LinearLayout>


接着就是MainActivity的编写了,这里和上一节的写法是一样的,让Activity类实现OnClickListener接口

重写onClick方法,依据不同的view的id就能够推断是哪个button点击了,这样的写法的优点是对于组件较多的

情况这样写能够简化代码!

MainActivity.java:

package com.jay.example.vibratordemo;

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener{

	private Button btnshort;
	private Button btnlong;
	private Button btnrhythm;
	private Button btncancel;
	private Vibrator myVibrator;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		btnshort = (Button) findViewById(R.id.btnshort);
		btnlong = (Button) findViewById(R.id.btnlong);
		btnrhythm = (Button) findViewById(R.id.btnrhythm);
		btncancel = (Button) findViewById(R.id.btncancle);
		
		btnshort.setOnClickListener(this);
		btnlong.setOnClickListener(this);
		btnrhythm.setOnClickListener(this);
		btncancel.setOnClickListener(this);
		
		
		//获得系统的Vibrator实例:
		myVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnshort:
			myVibrator.cancel();
			myVibrator.vibrate(new long[]{100,200,100,200}, 0);
			Toast.makeText(getApplicationContext(), "短振动", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btnlong:
			myVibrator.cancel();
			myVibrator.vibrate(new long[]{100,100,100,1000}, 0);
			Toast.makeText(getApplicationContext(), "长振动", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btnrhythm:
			myVibrator.cancel();
			myVibrator.vibrate(new long[]{500,100,500,100,500,100}, 0);
			Toast.makeText(getApplicationContext(), "节奏振动", Toast.LENGTH_SHORT).show();
			break;
		case R.id.btncancle:
			myVibrator.cancel();
			Toast.makeText(getApplicationContext(), "取消振动", Toast.LENGTH_SHORT).show();
		}
		
	}
}


最后别忘了在AndroidManifest.xml文件里加入权限哦!

<uses-permission android:name="android.permission.VIBRATE"/>


好了,基本使用方法事实上也是非常easy的,这里就不多说了,另外上面也说了,虚拟机是没有震动效果的,所以

须要把应用公布到手机上才干检測效果了!參考代码下载:vibratorDemo.rar

为了方便各位,直接把apk导出来吧,直接下载安装到手机就能够測试效果了,当然仅仅是个小demo,不会

推送广告,乱发短信什么的=-=!安装时能够查看所须要的权限!apk下载:vibratorDemo.apk









版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117757.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 什么是跨域及怎么解决跨域问题?[通俗易懂]

    什么是跨域及怎么解决跨域问题?[通俗易懂]什么是跨域?这篇博文解释的挺清楚,我直接引用https://blog.csdn.net/lambert310/article/details/51683775跨域,指的是浏览器不能执行其他网站的脚本。它是由浏览器的同源策略造成的,是浏览器施加的安全限制。所谓同源是指,域名,协议,端口均相同,只要有一个不同,就是跨域。不明白没关系,举个栗子:http://www.123.com/i…

    2022年4月29日
    43
  • 双系统 Win10下安装Linux(单/双硬盘)

    双系统 Win10下安装Linux(单/双硬盘)双系统Win10下安装Linux(单/双硬盘)

    2022年7月24日
    36
  • burpsuite简单抓包教程[通俗易懂]

    burpsuite简单抓包教程[通俗易懂]一.配置浏览器(以火狐为例)1.打开菜单,找到选项,点击翻到最下面点击设置,将配置的代理服务器改为手动代理配置,HTTP代理设为127.0.0.1,端口设置为8080(为了burpsuite能截到浏览器发送出来的请求),点击确定。二.burpsuite的设置点击proxy选择Options观察图中的IP地址及端口,如果不是127.0.0.1:8080,则点击add,添加端口和IP地…

    2022年6月14日
    127
  • SMTP设置_搭建邮件服务器的方法

    SMTP设置_搭建邮件服务器的方法2019独角兽企业重金招聘Python工程师标准>>>…

    2022年10月4日
    3
  • mysql查看查询慢的语句_sql慢查询如何优化

    mysql查看查询慢的语句_sql慢查询如何优化Mysql慢查询设置分析MySQL语句查询性能的方法除了使用EXPLAIN输出执行计划,还可以让MySQL记录下查询超过指定时间的语句,我们将超过指定时间的SQL语句查询称为“慢查询”。=========================================================方法一:这个方法我正在用,呵呵,比较喜欢这种即时性的。Mysql5.0以上的版本可以支持将执行…

    2022年10月14日
    3
  • pycharm如何打开原来的项目_terminal怎么打开

    pycharm如何打开原来的项目_terminal怎么打开Pycharm的下方工具栏中有两个窗口:PythonConsole和Terminal(如下图)其中,PythonConsole叫做Python控制台,即Python交互模式;Terminal叫做终端,即命令行模式。Python交互模式主要有两种:CPython用>>>作为提示符,而IPython用In[序号]:作为提示符。Python交互式模式可以直接输入代码,然后执行,并立刻得到结果,因此Python交互模式主要是为了调试Python代码用的。命..

    2022年8月28日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号