创建服务以检测用户的任何操作
Create service to detect any action from the user
我正在尝试创建一项服务,我想在其中检测有关用户的某些信息,假设当用户将设备放在 table 上时,问题是我检测到了该操作,但我确实有在 MainActivty
上,我想把它放在 Service
上。
问题是,在我的 MainActivity()
上我有我的 registerAction()
和在我的 onResume()
上被调用,在 onPause()
中我从我的 [=19= 中调用 unregisterListener()
],我也有一个 HandlerThread
,我在 onCreate()
上启动它,如何将它更改为 Service
?会有问题吗?我发现没有相同的方法...
我已经创建了我的 Service
并且我得到了 :
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Log.d("CREATE","ONCREATE");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("DESTROY","ONDESTROY");
}
}
还有我的MainActivity
我放了implements SensorEventListener
.
我的 class 骨架是:
public class MainActivity extends Activity implements SensorEventListener {
private HandlerThread mSensorThread;
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorThread = new HandlerThread("sensor_thread");
mSensorThread.start();
}
private void registerSensorListener() {
mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST, new Handler(mSensorThread.getLooper()));
}
@Override
public void onSensorChanged(SensorEvent event) {
//DO stuff
if (isLayed()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("LAY","LAYLAY");
}
});
mSensorManager.unregisterListener(this);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private boolean isLayed() {
return stuff;
}
@Override
protected void onResume() {
super.onResume();
registerSensorListener();
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
编辑
我正在使用 szamani20 代码,但我在使用 runOnUiThread
时遇到问题,因为我也无法从我的 Service
调用,我遇到了这个问题
java.lang.RuntimeException: Unable to start service com.example.developer.qwe.MyService@d8c613b with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
您可以在 OnStartCommand.Also 中的服务中注册 SensorManager 尝试使用 startForeground,因为 android os 会在应用被终止时终止您的服务
首先,您需要决定是否希望用户了解您的 运行 服务。在 Background Execution Limits in android Oreo 上发表评论:
To improve the user experience, Android 8.0 (API level 26) imposes limitations on what apps can do while running in the background.
所以考虑到你的情况,在很多情况下似乎有很多工作要做,使用前台服务会是更好的方法。作为 android document says about foreground services:
A foreground service is a service that the user is actively aware of and is not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
既然你提到你有检测到的动作,我就不会输入你的那部分代码。因此,您需要像之前那样创建 Service
的子类,并使用 startService
方法来调用 onCreate
。您需要注意的一件事是,一旦您第一次在该服务上调用 startService
,就会调用服务的 onCreate
方法,无论您再次调用 startService
多少次 onCreate
方法不会被调用,只有 onStartCommand
被调用。我们利用这一事实,您可以在 intent
中提供一个字符串操作来正确注册和取消注册您的侦听器。
在MainActivity.java
中:
String action = "start"; // Or to unregister listener "stop"!
final Intent intent = new Intent(this, MyService.class);
intent.setAction(action);
startService(intent);
然后在 MyService.java
:
@Override
public void onCreate() {
super.onCreate();
// Do initialization or whatever here (executed once per service lifecycle)
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals("start")) {
// Register your listener or whatever
showForegroundNotification();
}
if (intent.getAction().equals("stop")) {
// Unregister your listener or whatever
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
private void showForegroundNotification() {
Intent myServiceNotificationIntent = new Intent(this, MainActivity.class);
myServiceNotificationIntent.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent
.getActivity(this, MY_SERVICE_REQUEST_CODE,
myServiceNotificationIntent, MY_SERVICE_FLAG);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(MY_SERVICE_NOTIFICATION_CONTENT_TITLE)
.setTicker(MY_SERVICE_NOTIFICATION_TICKER)
.setContentText(MY_SERVICE_NOTIFICATION_CONTENT_TEXT)
.setSmallIcon(R.drawable.ic_whatever)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(MY_SERVICE_NOTIFICATION_ID, notification);
}
最后不要忘记在 onDestroy
中注销您的监听器,以防 android 终止您的服务(这种情况非常罕见):
@Override
public void onDestroy() {
super.onDestroy();
// Unregister your listener
}
我正在尝试创建一项服务,我想在其中检测有关用户的某些信息,假设当用户将设备放在 table 上时,问题是我检测到了该操作,但我确实有在 MainActivty
上,我想把它放在 Service
上。
问题是,在我的 MainActivity()
上我有我的 registerAction()
和在我的 onResume()
上被调用,在 onPause()
中我从我的 [=19= 中调用 unregisterListener()
],我也有一个 HandlerThread
,我在 onCreate()
上启动它,如何将它更改为 Service
?会有问题吗?我发现没有相同的方法...
我已经创建了我的 Service
并且我得到了 :
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
Log.d("CREATE","ONCREATE");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("DESTROY","ONDESTROY");
}
}
还有我的MainActivity
我放了implements SensorEventListener
.
我的 class 骨架是:
public class MainActivity extends Activity implements SensorEventListener {
private HandlerThread mSensorThread;
private SensorManager mSensorManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorThread = new HandlerThread("sensor_thread");
mSensorThread.start();
}
private void registerSensorListener() {
mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST, new Handler(mSensorThread.getLooper()));
}
@Override
public void onSensorChanged(SensorEvent event) {
//DO stuff
if (isLayed()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("LAY","LAYLAY");
}
});
mSensorManager.unregisterListener(this);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private boolean isLayed() {
return stuff;
}
@Override
protected void onResume() {
super.onResume();
registerSensorListener();
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
}
编辑
我正在使用 szamani20 代码,但我在使用 runOnUiThread
时遇到问题,因为我也无法从我的 Service
调用,我遇到了这个问题
java.lang.RuntimeException: Unable to start service com.example.developer.qwe.MyService@d8c613b with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getAction()' on a null object reference
您可以在 OnStartCommand.Also 中的服务中注册 SensorManager 尝试使用 startForeground,因为 android os 会在应用被终止时终止您的服务
首先,您需要决定是否希望用户了解您的 运行 服务。在 Background Execution Limits in android Oreo 上发表评论:
To improve the user experience, Android 8.0 (API level 26) imposes limitations on what apps can do while running in the background.
所以考虑到你的情况,在很多情况下似乎有很多工作要做,使用前台服务会是更好的方法。作为 android document says about foreground services:
A foreground service is a service that the user is actively aware of and is not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.
既然你提到你有检测到的动作,我就不会输入你的那部分代码。因此,您需要像之前那样创建 Service
的子类,并使用 startService
方法来调用 onCreate
。您需要注意的一件事是,一旦您第一次在该服务上调用 startService
,就会调用服务的 onCreate
方法,无论您再次调用 startService
多少次 onCreate
方法不会被调用,只有 onStartCommand
被调用。我们利用这一事实,您可以在 intent
中提供一个字符串操作来正确注册和取消注册您的侦听器。
在MainActivity.java
中:
String action = "start"; // Or to unregister listener "stop"!
final Intent intent = new Intent(this, MyService.class);
intent.setAction(action);
startService(intent);
然后在 MyService.java
:
@Override
public void onCreate() {
super.onCreate();
// Do initialization or whatever here (executed once per service lifecycle)
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals("start")) {
// Register your listener or whatever
showForegroundNotification();
}
if (intent.getAction().equals("stop")) {
// Unregister your listener or whatever
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
private void showForegroundNotification() {
Intent myServiceNotificationIntent = new Intent(this, MainActivity.class);
myServiceNotificationIntent.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent
.getActivity(this, MY_SERVICE_REQUEST_CODE,
myServiceNotificationIntent, MY_SERVICE_FLAG);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(MY_SERVICE_NOTIFICATION_CONTENT_TITLE)
.setTicker(MY_SERVICE_NOTIFICATION_TICKER)
.setContentText(MY_SERVICE_NOTIFICATION_CONTENT_TEXT)
.setSmallIcon(R.drawable.ic_whatever)
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
startForeground(MY_SERVICE_NOTIFICATION_ID, notification);
}
最后不要忘记在 onDestroy
中注销您的监听器,以防 android 终止您的服务(这种情况非常罕见):
@Override
public void onDestroy() {
super.onDestroy();
// Unregister your listener
}