概述
首先通过Activity 界面开启 翻转静音的Service
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;
public class PhoneSensorActivity extends Activity{
Context c = PhoneSensorActivity.this;
public static final String SERVICE_NAME = "com.goso.jamin.TurningService";
private static final String className = "com.goso.jamin.TurningService";
private static String TAG = "PhoneSensorActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ToggleButton serviceSwitch = (ToggleButton) findViewById(R.id.toggleButton1);
final Intent intent = new Intent(PhoneSensorActivity.this , TurningService.class);//用于开启service的Intent
serviceSwitch.setChecked(isServiceRunning (this , SERVICE_NAME));//根据服务是否运行,首先设置toggleButton是否开启
serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked == true){
startService(intent);
Toast.makeText(c, "翻转静音功能 开启", Toast.LENGTH_SHORT).show();
}else{
stopService(intent);
Log.d(TAG, "serviceRun ? = " + isServiceRunning (PhoneSensorActivity.this , SERVICE_NAME));
Toast.makeText(c, "翻转静音功能 关闭", Toast.LENGTH_SHORT).show();
}
}
});
}
private boolean isServiceRunning(PhoneSensorActivity phoneSensorActivity,
String serviceName) {
// TODO Auto-generated method stub
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) c.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(Integer.MAX_VALUE);
if(! (serviceList.size()>0)){
return false;
}
for(int i = 0 ; i < serviceList.size() ; i++){
Log.d(TAG, "第" + i + "个 = " + serviceList.get(i).service.getClassName());
if(serviceList.get(i).service.getClassName().equals(className)){
isRunning = true;
break;
}
}
return isRunning;
}
}
Service里这样写
如此手机翻转静音已经实现~
import java.util.List;
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.media.AudioManager;
import android.os.IBinder;
import android.util.Log;
public class TurningService extends Service {
private static final String TAG = "TurningService====";
private SensorEventListener mListener;
private AudioManager mAudioManager;
private SensorManager mSensorManager;
private int StatusFlag;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d(TAG, "TurningService is start");
//get the sensor service and the audio service
mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
mAudioManager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
//状态标志初始化,静音模式为1,否则为0
StatusFlag = (mAudioManager.getRingerMode()==AudioManager.RINGER_MODE_SILENT)? 1 : 0 ;
/配置感应监听器
mListener = new SensorEventListener() {
//精度改变
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
//
Log.d(TAG, "X = " + String.valueOf(event.values[0]));
//
Log.d(TAG, "Y = " + String.valueOf(event.values[1]));
//
Log.d(TAG, "Z = " + String.valueOf(event.values[2]));
switch(StatusFlag){
case 1:
if(event.values[2] > 8){
//
Log.d(TAG , " change to Ringing mode ");
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
StatusFlag = 0;
break;
}
case 0:
if(event.values[2] < -8){
//
Log.d( TAG , "change to SilentMode");
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mAudioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
StatusFlag = 1;
break;
}
default:
break;
}
}
};
/
List<Sensor> mSensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
int size = mSensorList.size();
for(int i = 0 ; i <size ; i++){
//
Log.d(TAG, "sensor");
//
Log.d(TAG, "mSensorList.size() = " + mSensorList.size());
//
Log.d(TAG, "mSensorList = " + mSensorList);
if(mSensorList!=null && (mSensorList.size()>0)){
Sensor sensor = mSensorList.get(0);
mSensorManager.registerListener(mListener, sensor,SensorManager.SENSOR_DELAY_NORMAL);
}else {
//
Log.d(TAG, "sensor is null");
}
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mSensorManager.unregisterListener(mListener);
}
}
由于Android模拟器没有sensor功能,所以需要手机测试,或者是下载一个sensor simulator
关于sensor simulator的使用
请参看:http://wxmijl.blog.163.com/blog/static/13245928201062734631474/
最后
以上就是感性星星为你收集整理的手机翻转静音的全部内容,希望文章能够帮你解决手机翻转静音所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复