android 中传感器的后台服务
Background service for sensor in android
我已经实现了一个代码,当我们摇动我们的设备时打开一个应用程序。就我将所有代码保存到 MainActivity and 而没有任何后台服务而言,一切进展顺利。但是当我尝试添加后台服务时它不起作用,因为 我希望它继续工作而不必每次都进入应用程序。 所以有人可以建议我我在做什么 wrong.Thanks !
BackgroundService.java
public class BackgroundService extends Service implements SensorEventListener {
public BackgroundService() {
}
private SensorManager mSensorManager;
private Sensor mSensor;
private long lastUpdate = 0;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 600;
@Override
public void onCreate() {
super.onCreate();
// Get sensor manager on starting the service.
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Registering...
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
// Get default sensor type
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Get sensor manager on starting the service.
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Registering...
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
// Get default sensor type
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not Yet Implemented");
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
Sensor mySensor = sensorEvent.sensor;
mSensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] values = sensorEvent.values;
float x = values[0];
float y = values[1];
float z = values[2];
long curTime = System.currentTimeMillis();
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
float speed
= Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
if (speed > SHAKE_THRESHOLD) {
openWhatsApp();
}
last_x = x;
last_y = y;
last_z = z;
}
// Stop the sensor and service
mSensorManager.unregisterListener(this);
stopSelf();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
public void openWhatsApp() {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
if (launchIntent != null) {
startService(launchIntent);
}
}
}
MainActivity.java
public class MainActivity 扩展 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent i = new Intent(MainActivity.this, BackgroundService.class);
startService(i);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="joshipinak.com.shakeshooksaken">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BackgroundService" />
</application>
</manifest>
在GitHub Android Examples中有一个完整的应用程序。它使用像您的代码一样的命令模式来检查传感器值和 AlarmManager
.
在中是一个使用SensorEventListener
界面的例子。
Android 开发者网站说要在 this page 上使用 IntentService
。
您还可以在 this question on SO.
中阅读 Service 和 IntentService 之间的区别
这些链接也可能对您有所帮助:
- A question on SO about having an Android app running in the background
- Duplicate of previous link with other information
- SO question about communication between Activity and Service
我已经实现了一个代码,当我们摇动我们的设备时打开一个应用程序。就我将所有代码保存到 MainActivity and 而没有任何后台服务而言,一切进展顺利。但是当我尝试添加后台服务时它不起作用,因为 我希望它继续工作而不必每次都进入应用程序。 所以有人可以建议我我在做什么 wrong.Thanks !
BackgroundService.java
public class BackgroundService extends Service implements SensorEventListener {
public BackgroundService() {
}
private SensorManager mSensorManager;
private Sensor mSensor;
private long lastUpdate = 0;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 600;
@Override
public void onCreate() {
super.onCreate();
// Get sensor manager on starting the service.
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Registering...
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
// Get default sensor type
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Get sensor manager on starting the service.
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
// Registering...
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
// Get default sensor type
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
throw new UnsupportedOperationException("Not Yet Implemented");
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
Sensor mySensor = sensorEvent.sensor;
mSensorManager.registerListener(this, mySensor, SensorManager.SENSOR_DELAY_NORMAL);
if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
float[] values = sensorEvent.values;
float x = values[0];
float y = values[1];
float z = values[2];
long curTime = System.currentTimeMillis();
if ((curTime - lastUpdate) > 100) {
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
float speed
= Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;
if (speed > SHAKE_THRESHOLD) {
openWhatsApp();
}
last_x = x;
last_y = y;
last_z = z;
}
// Stop the sensor and service
mSensorManager.unregisterListener(this);
stopSelf();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
public void openWhatsApp() {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
if (launchIntent != null) {
startService(launchIntent);
}
}
}
MainActivity.java
public class MainActivity 扩展 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent i = new Intent(MainActivity.this, BackgroundService.class);
startService(i);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="joshipinak.com.shakeshooksaken">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BackgroundService" />
</application>
</manifest>
在GitHub Android Examples中有一个完整的应用程序。它使用像您的代码一样的命令模式来检查传感器值和 AlarmManager
.
在SensorEventListener
界面的例子。
Android 开发者网站说要在 this page 上使用 IntentService
。
您还可以在 this question on SO.
这些链接也可能对您有所帮助:
- A question on SO about having an Android app running in the background
- Duplicate of previous link with other information
- SO question about communication between Activity and Service