Android User Activity Recognition in Background Task
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
My goal is to listen to the user activites and on change do something in the background like log the event in a database. When the app is in the foreground the events are fired correctly. When the app is closed (but my service is running) then no event is fired. How could I get these events?
General Background Service:
public class AppService extends Service {
ActivitySensor activitySensor;
@Override
public void onCreate() {
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
activitySensor = new ActivitySensor(getApplicationContext());
activitySensor.start();
return START_STICKY;
}
@Override
public void onDestroy() {
Log.d("Service", "onDestroy");
activitySensor.stop();
super.onDestroy();
}
ActivitySensor:
public class ActivitySensor {
private static final long DETECTION_INTERVAL_IN_MILLISECONDS = 3000;
private ActivityRecognitionClient mActivityRecognitionClient;
private Intent mIntentService;
private PendingIntent mPendingIntent;
private Context context;
public ActivitySensor(Context _context){
this.context = _context;
mIntentService = new Intent(context, DetectedActivitiesIntentService.class);
mPendingIntent = PendingIntent.getService(context, 1, mIntentService, PendingIntent.FLAG_UPDATE_CURRENT);
mActivityRecognitionClient = new ActivityRecognitionClient(context);
}
public void start(){
Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(
DETECTION_INTERVAL_IN_MILLISECONDS,
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(context,
"Successfully requested activity updates",
Toast.LENGTH_SHORT)
.show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context,
"Requesting activity updates failed to start",
Toast.LENGTH_SHORT)
.show();
}
});
}
public void stop() {
Task<Void> task = mActivityRecognitionClient.removeActivityUpdates(
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(context,
"Removed activity updates successfully!",
Toast.LENGTH_SHORT)
.show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context, "Failed to remove activity updates!",
Toast.LENGTH_SHORT).show();
}
});
}
}
DetectedActivitiesIntentService:
public class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = DetectedActivitiesIntentService.class.getSimpleName();
String valueNames = {"activity", "confidence"} ;
AppDatabase db;
public DetectedActivitiesIntentService() {
// Use the TAG to name the worker thread.
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
this.db = ((BaseApp)getApplication()).getDatabase();
}
@Override
protected void onHandleIntent(Intent intent) {
//never fired
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(DetectedActivitiesIntentService.this.getApplicationContext(), "Activity detected: "+ actS + " "+ values[1], Toast.LENGTH_SHORT).show();
}
});
}
}
android activity-recognition
add a comment |
My goal is to listen to the user activites and on change do something in the background like log the event in a database. When the app is in the foreground the events are fired correctly. When the app is closed (but my service is running) then no event is fired. How could I get these events?
General Background Service:
public class AppService extends Service {
ActivitySensor activitySensor;
@Override
public void onCreate() {
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
activitySensor = new ActivitySensor(getApplicationContext());
activitySensor.start();
return START_STICKY;
}
@Override
public void onDestroy() {
Log.d("Service", "onDestroy");
activitySensor.stop();
super.onDestroy();
}
ActivitySensor:
public class ActivitySensor {
private static final long DETECTION_INTERVAL_IN_MILLISECONDS = 3000;
private ActivityRecognitionClient mActivityRecognitionClient;
private Intent mIntentService;
private PendingIntent mPendingIntent;
private Context context;
public ActivitySensor(Context _context){
this.context = _context;
mIntentService = new Intent(context, DetectedActivitiesIntentService.class);
mPendingIntent = PendingIntent.getService(context, 1, mIntentService, PendingIntent.FLAG_UPDATE_CURRENT);
mActivityRecognitionClient = new ActivityRecognitionClient(context);
}
public void start(){
Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(
DETECTION_INTERVAL_IN_MILLISECONDS,
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(context,
"Successfully requested activity updates",
Toast.LENGTH_SHORT)
.show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context,
"Requesting activity updates failed to start",
Toast.LENGTH_SHORT)
.show();
}
});
}
public void stop() {
Task<Void> task = mActivityRecognitionClient.removeActivityUpdates(
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(context,
"Removed activity updates successfully!",
Toast.LENGTH_SHORT)
.show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context, "Failed to remove activity updates!",
Toast.LENGTH_SHORT).show();
}
});
}
}
DetectedActivitiesIntentService:
public class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = DetectedActivitiesIntentService.class.getSimpleName();
String valueNames = {"activity", "confidence"} ;
AppDatabase db;
public DetectedActivitiesIntentService() {
// Use the TAG to name the worker thread.
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
this.db = ((BaseApp)getApplication()).getDatabase();
}
@Override
protected void onHandleIntent(Intent intent) {
//never fired
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(DetectedActivitiesIntentService.this.getApplicationContext(), "Activity detected: "+ actS + " "+ values[1], Toast.LENGTH_SHORT).show();
}
});
}
}
android activity-recognition
add a comment |
My goal is to listen to the user activites and on change do something in the background like log the event in a database. When the app is in the foreground the events are fired correctly. When the app is closed (but my service is running) then no event is fired. How could I get these events?
General Background Service:
public class AppService extends Service {
ActivitySensor activitySensor;
@Override
public void onCreate() {
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
activitySensor = new ActivitySensor(getApplicationContext());
activitySensor.start();
return START_STICKY;
}
@Override
public void onDestroy() {
Log.d("Service", "onDestroy");
activitySensor.stop();
super.onDestroy();
}
ActivitySensor:
public class ActivitySensor {
private static final long DETECTION_INTERVAL_IN_MILLISECONDS = 3000;
private ActivityRecognitionClient mActivityRecognitionClient;
private Intent mIntentService;
private PendingIntent mPendingIntent;
private Context context;
public ActivitySensor(Context _context){
this.context = _context;
mIntentService = new Intent(context, DetectedActivitiesIntentService.class);
mPendingIntent = PendingIntent.getService(context, 1, mIntentService, PendingIntent.FLAG_UPDATE_CURRENT);
mActivityRecognitionClient = new ActivityRecognitionClient(context);
}
public void start(){
Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(
DETECTION_INTERVAL_IN_MILLISECONDS,
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(context,
"Successfully requested activity updates",
Toast.LENGTH_SHORT)
.show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context,
"Requesting activity updates failed to start",
Toast.LENGTH_SHORT)
.show();
}
});
}
public void stop() {
Task<Void> task = mActivityRecognitionClient.removeActivityUpdates(
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(context,
"Removed activity updates successfully!",
Toast.LENGTH_SHORT)
.show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context, "Failed to remove activity updates!",
Toast.LENGTH_SHORT).show();
}
});
}
}
DetectedActivitiesIntentService:
public class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = DetectedActivitiesIntentService.class.getSimpleName();
String valueNames = {"activity", "confidence"} ;
AppDatabase db;
public DetectedActivitiesIntentService() {
// Use the TAG to name the worker thread.
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
this.db = ((BaseApp)getApplication()).getDatabase();
}
@Override
protected void onHandleIntent(Intent intent) {
//never fired
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(DetectedActivitiesIntentService.this.getApplicationContext(), "Activity detected: "+ actS + " "+ values[1], Toast.LENGTH_SHORT).show();
}
});
}
}
android activity-recognition
My goal is to listen to the user activites and on change do something in the background like log the event in a database. When the app is in the foreground the events are fired correctly. When the app is closed (but my service is running) then no event is fired. How could I get these events?
General Background Service:
public class AppService extends Service {
ActivitySensor activitySensor;
@Override
public void onCreate() {
super.onCreate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
activitySensor = new ActivitySensor(getApplicationContext());
activitySensor.start();
return START_STICKY;
}
@Override
public void onDestroy() {
Log.d("Service", "onDestroy");
activitySensor.stop();
super.onDestroy();
}
ActivitySensor:
public class ActivitySensor {
private static final long DETECTION_INTERVAL_IN_MILLISECONDS = 3000;
private ActivityRecognitionClient mActivityRecognitionClient;
private Intent mIntentService;
private PendingIntent mPendingIntent;
private Context context;
public ActivitySensor(Context _context){
this.context = _context;
mIntentService = new Intent(context, DetectedActivitiesIntentService.class);
mPendingIntent = PendingIntent.getService(context, 1, mIntentService, PendingIntent.FLAG_UPDATE_CURRENT);
mActivityRecognitionClient = new ActivityRecognitionClient(context);
}
public void start(){
Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(
DETECTION_INTERVAL_IN_MILLISECONDS,
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(context,
"Successfully requested activity updates",
Toast.LENGTH_SHORT)
.show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context,
"Requesting activity updates failed to start",
Toast.LENGTH_SHORT)
.show();
}
});
}
public void stop() {
Task<Void> task = mActivityRecognitionClient.removeActivityUpdates(
mPendingIntent);
task.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void result) {
Toast.makeText(context,
"Removed activity updates successfully!",
Toast.LENGTH_SHORT)
.show();
}
});
task.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(context, "Failed to remove activity updates!",
Toast.LENGTH_SHORT).show();
}
});
}
}
DetectedActivitiesIntentService:
public class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = DetectedActivitiesIntentService.class.getSimpleName();
String valueNames = {"activity", "confidence"} ;
AppDatabase db;
public DetectedActivitiesIntentService() {
// Use the TAG to name the worker thread.
super(TAG);
}
@Override
public void onCreate() {
super.onCreate();
this.db = ((BaseApp)getApplication()).getDatabase();
}
@Override
protected void onHandleIntent(Intent intent) {
//never fired
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(DetectedActivitiesIntentService.this.getApplicationContext(), "Activity detected: "+ actS + " "+ values[1], Toast.LENGTH_SHORT).show();
}
});
}
}
android activity-recognition
android activity-recognition
asked Nov 23 '18 at 15:15
AntheaAnthea
1,77642956
1,77642956
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
standard background services are no longer supported in Android Oreo, due to background execution limitations. you can read more about it here: android docs
I recommend having a look at JobintentService that can be used instead of a standard service and, it might be suitable for you to use an explicit broadcast receiver to invoke that JobIntentService.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449149%2fandroid-user-activity-recognition-in-background-task%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
standard background services are no longer supported in Android Oreo, due to background execution limitations. you can read more about it here: android docs
I recommend having a look at JobintentService that can be used instead of a standard service and, it might be suitable for you to use an explicit broadcast receiver to invoke that JobIntentService.
add a comment |
standard background services are no longer supported in Android Oreo, due to background execution limitations. you can read more about it here: android docs
I recommend having a look at JobintentService that can be used instead of a standard service and, it might be suitable for you to use an explicit broadcast receiver to invoke that JobIntentService.
add a comment |
standard background services are no longer supported in Android Oreo, due to background execution limitations. you can read more about it here: android docs
I recommend having a look at JobintentService that can be used instead of a standard service and, it might be suitable for you to use an explicit broadcast receiver to invoke that JobIntentService.
standard background services are no longer supported in Android Oreo, due to background execution limitations. you can read more about it here: android docs
I recommend having a look at JobintentService that can be used instead of a standard service and, it might be suitable for you to use an explicit broadcast receiver to invoke that JobIntentService.
answered Dec 9 '18 at 14:53
Andrew IrwinAndrew Irwin
165417
165417
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449149%2fandroid-user-activity-recognition-in-background-task%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown