基于Android重力传感器的编程 -- 摇一摇打电话+旋转锁屏功能的实现
jopen
11年前
最后的效果图为:
1、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/b" > <Button android:id="@+id/bt_shake" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/shake" android:text="晃动打电话" android:onClick="goShake" /> <Button android:id="@+id/bt_rotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rotate" android:text="旋转锁屏" android:onClick="goRotate" /> </LinearLayout>
2、shake.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/a"> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello_world" /> </LinearLayout>
3、rotate.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/c"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello_world"/> </LinearLayout>
-------------------以上UI的编写,接下来是逻辑功能的实现
4、MainActivity
package com.njupt.zzt_love_hjd; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button bt_shake; private Button bt_rotate; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt_shake = (Button) findViewById(R.id.bt_shake); bt_rotate = (Button) findViewById(R.id.bt_rotate); } public void goShake(View v){ Intent i1 = new Intent(getApplicationContext(),ShakeActivity.class); startActivity(i1); } public void goRotate(View v){ Intent i2 = new Intent(getApplicationContext(),RotateActivity.class); startActivity(i2); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
5、ShakeActivity
package com.njupt.zzt_love_hjd; import android.app.Activity; import android.app.Service; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Vibrator; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.Toast; public class ShakeActivity extends Activity implements SensorEventListener { private Button clear; private SensorManager mSensorManager; private Vibrator vibrator; private int counter = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shake); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); clear = (Button) findViewById(R.id.clear); clear.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { clear.setText("现在给button赋值喽...."); } }); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); } @Override protected void onStop() { mSensorManager.unregisterListener(this); super.onStop(); } @Override protected void onPause() { mSensorManager.unregisterListener(this); super.onPause(); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); float[] values = event.values; if(sensorType == Sensor.TYPE_ACCELEROMETER){ if(Math.abs(values[0] )> 14 &&Math.abs(values[1] )> 14 &&Math.abs(values[2] )> 14 ){ clear.setText("测试成功"+(counter++)); vibrator.vibrate(500); // Toast.makeText(this, "您晃动了手机", 1).show(); Intent intent = new Intent(); intent.setAction(Intent.ACTION_DIAL); // intent.setData(Uri.parse("tel:" + number)); startActivity(intent); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
6、MyAdmin(用于锁屏中的获取管理员权限)
package com.njupt.zzt_love_hjd; import android.app.admin.DeviceAdminReceiver; public class MyAdmin extends DeviceAdminReceiver { }
7、RotateActivity
package com.njupt.zzt_love_hjd; import android.app.Activity; import android.app.Service; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.Vibrator; import android.view.Menu; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class RotateActivity extends Activity implements SensorEventListener { private Button clear; private TextView tv1; private SensorManager mSensorManager; private Vibrator vibrator; private int counter = 1; private DevicePolicyManager devicePolicyManager; private boolean isAdminActive; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rotate); mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE); // clear = (Button) findViewById(R.id.clear); // clear.setOnClickListener(new Button.OnClickListener() { // // @Override // public void onClick(View v) { // clear.setText("现在给button赋值喽...."); // } // }); // tv1 = (TextView) findViewById(R.id.tv1); devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); // 申请权限 ComponentName componentName = new ComponentName(this, MyAdmin.class); // 判断该组件是否有系统管理员的权限 isAdminActive = devicePolicyManager .isAdminActive(componentName); if(!isAdminActive){ Intent intent = new Intent(); //指定动作 intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); //指定给那个组件授权 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); startActivity(intent); } } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); } @Override protected void onStop() { mSensorManager.unregisterListener(this); super.onStop(); } @Override protected void onPause() { mSensorManager.unregisterListener(this); super.onPause(); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); float[] values = event.values; float x = values[0]; float y = values[1]; if (sensorType == Sensor.TYPE_ACCELEROMETER) { tv1.setText("现在的x轴是: " + x + " y轴是: " + y); if (Math.abs(x) > 9.0 || Math.abs(y) > 9.0) { // Toast.makeText(this, "现在的垂直方向已经超过了90度,将进行锁屏", 1).show(); vibrator.vibrate(500); System.out.println("...............isAdminActive: " + isAdminActive); if(isAdminActive){ // Toast.makeText(this, "具有权限,将进行锁屏....", 1).show(); devicePolicyManager.lockNow(); devicePolicyManager.resetPassword("123321", 0); } } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
8、在AndroidManifest.xml文件加上
<uses-permission android:name="android.permission.VIBRATE"/> <activity android:name="com.njupt.zzt_love_hjd.ShakeActivity"/> <activity android:name="com.njupt.zzt_love_hjd.RotateActivity"/> <receiver android:name="com.njupt.zzt_love_hjd.MyAdmin" > <meta-data android:name="android.app.device_admin" android:resource="@xml/my_admin"/> <intent-filter > <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/> </intent-filter> </receiver>
9、在res中新建xml/my_admin.xml文件
<?xml version="1.0" encoding="utf-8"?> <device-admin xmlns:android="http://schemas.android.com/apk/res/android"> <!-- limit-password 设置密码的规则 watch-login 监控屏幕解锁尝试次数 reset-password 更改屏幕解锁密码 force-lock 设备自动锁屏 wipe-data 删除全部的数据 --> <uses-policies> <limit-password/> <watch-login /> <reset-password /> <force-lock /> <wipe-data /> </uses-policies> </device-admin>
这时便能实现摇一摇打电话+旋转锁屏的功能了..........