jenzz / rxappstate Goto Github PK
View Code? Open in Web Editor NEW[DEPRECATED] A simple Android library that monitors app state changes (background / foreground).
License: MIT License
[DEPRECATED] A simple Android library that monitors app state changes (background / foreground).
License: MIT License
On device HUAWEI P9 ONLY,
right after the application is created and subscribes to the monitor observable,
got state FOREGROUND and right after that - got state BACKGROUND,
so the app is foreground but according to the monitor my project think that the app is in background.
Potential fix:
Hi,
First of all Thanks for providing such useful library. It is really gone useful to many of the developers.
I have added this library to my project. It is working fine so far. But somehow after some instance, it is not calling stop() method or may be it is not working as expected.
I am calling stop() method when I am going to open camera or gallery to select the Image. After selecting a camera, when I came back, it is getting called by foreground method.
FYI, I have extends MultidexApplication to my application class. And the activity which is calling startActivityForResult is after the 3-4 activity of the MainActivity.
Can you please check for this issue and let me know the possible solution for it.
Thanks.
Sincerely,
Shreyash
when i start other app the front app is also in Foreground Unless the other app goto next Activity why ? please help me !!
Hello team,
I am in integrating your library in my live project.
I am facing below an issue in Android 7.0.
I am using below code in my Service onStartCommand()
Notification notification = new NotificationCompat.Builder(this) .setContentTitle(getString(R.string.app_name)).setContentText(getString(R.string.kiosk_running_0 + Utils.getSelectedLaunguage())).setSmallIcon(R.drawable.hw_app_icon).build(); startForeground(1, notification);
I am not able to get a call back from AppStateMonitor.
Looking for your support as soon as possible.
Please help me.
Hello, I'm testing your lib on app debug and It works but when I create release apk using proguard I just get foreground status but not background.
Can you add some proguard rules to add to my config file?
Thank you.
Hello guys,
First of all, Thank you so much for your library.
I got a crash when clicked button Pick Image again.
But I want to use the whole app, so I comment getAppStateMonitor().start(); and getAppStateMonitor().stop(); in MainActivity class . It work very well.
Tks.
05-12 21:49:40.447 3591-3591/com.jenzz.appstate.sample E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jenzz.appstate.sample, PID: 3591
java.lang.IllegalArgumentException: Receiver not registered: com.jenzz.appstate.internal.DefaultAppStateRecognizer$ScreenOffBroadcastReceiver@ebff3db
at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:1007)
at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1330)
at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:608)
at com.jenzz.appstate.internal.DefaultAppStateRecognizer.stop(DefaultAppStateRecognizer.java:62)
at com.jenzz.appstate.AppStateMonitor.stop(AppStateMonitor.java:54)
at com.jenzz.appstate.sample.MainActivity$1.onClick(MainActivity.java:37)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
package com.jenzz.appstate.sample;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.jenzz.appstate.AppStateMonitor;
import java.io.FileNotFoundException;
import java.io.InputStream;
import static android.content.Intent.ACTION_GET_CONTENT;
public class MainActivity extends AppCompatActivity {
private static final int RESULT_CODE_PICK_IMAGE = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View pickImageButton = findViewById(R.id.pick_image);
//noinspection ConstantConditions
pickImageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAppStateMonitor().stop();
//pickImage();
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
1);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getAppStateMonitor().start();
if (resultCode == RESULT_OK && requestCode == RESULT_CODE_PICK_IMAGE) {
showImage(data.getData());
}
}
private void pickImage() {
Intent intent = new Intent(ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, RESULT_CODE_PICK_IMAGE);
}
private void showImage(Uri selectedImage) {
try {
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
ImageView imageView = (ImageView) findViewById(R.id.image_result);
//noinspection ConstantConditions
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// Ouch!
}
}
private AppStateMonitor getAppStateMonitor() {return ((SampleApplication) getApplication()).getAppStateMonitor();}
}
Hi,
Thank you for this library, this is super useful !
I was wondering if there was a reason behind the fact that AppState.FOREGROUND
is not send after the initialisation.
In AppStateRecognizer
the method onAppDidEnterForeground()
is called only of the current state is AppState.BACKGROUND
which is not the case immediately since the value is initialised with null.
One of my managers is registering with the asObservable() method but does not receive the first value which is expected to launch a polling method.
Many thanks
It's working fine when I have the following code before onStart() method gets code, i.e. in onCreate method. But if I have the following code added after onStart() in Activity, then app enters background onAppDidEnterForeground will not get fired at the first time. If I come back to foreground and re-enter background, then it works as expected.
AppStateMonitor appStateMonitor = AppStateMonitor.create(application);
appStateMonitor.addListener(this);
appStateMonitor.start();
Hello,
When are you guys going to officially release the code that is currently on the develop branch ? Since I updated my Android Studio RxAppState 3.0.1 does not work.
I have tested your develop branch using JitPack and it does resolve the issue I had (NoClassDefFound exception on RxAppState class). I think it is related to the d28deb5 commit.
Thanks for your work :-)
Thanks for contributing this library!
I have an application that launches a Foreground Service under certain circumstances. I need to know if one of the application's activities is visible to the user, independently of whether the foreground service is running or not. However, what I noticed is that I never receive a BACKGROUND callback when I background the application if the foreground service is running. Is this expected? Looking at your code, it seems that onTrimMemory
is not getting called in this case.
When I try to include RxAppState into the project I had JitPack added and add the library dependency. I just get:
'Could not find com.jenzz:RxAppState:1.1.0'
compile "com.jenzz.appstate:appstate:$rxappstate_version"
gives:
Error:Failed to resolve: com.jenzz.appstate:appstate:3.0.0
Phone: RedMi Note 4
Android: 6.0.1 MMB29M
It just works with this steps:
Step 1: Open App
Step 2: Press Home on device
Step 3: Open app again from the main screen of device ( List all app in device )
It doesn't work with this steps:
Step 1: Open App
Step 2: Press Home on device
Step 3: Press Overview button
Step 4: click App
=> RxAppState can't detect this case.
Even though it is pretty much standard (?) in Android world nowadays and the name of the library implies it,
this library should probably not depend on RxJava.
Instead an additional, reactive adapter could be provided, e.g. com.jenzz:appstate-rx:2.0.0
Toggling the phone screen on/off does not emit any app state events
because the system does not (!) fire the TRIM_MEMORY_UI_HIDDEN
component callback in this case.
Maybe resort to (or additionally use) classic Activity onStart()
/ onStop()
tracking using a configurable delay?
The AppStateRecognizer
is missing a default app state (appState
field is null
).
Hence there is the possibility of both RxAppState.isAppInForeground()
and RxAppState.isAppInBackground()
returning false
if the app has not yet transitioned into background for the first time.
Maybe default to AppState.FOREGROUND
?
Turning the screen off issues a BACKGROUND event every time, no matter whether the app was previously on FOREGROUND or BACKGROUND.
This can be easily workarounded using Rx distinctUntilChanged()
method though.
RxAppStateMonitor.monitor(this).subscribe(new Action1<AppState>() {
@Override
public void call(AppState appState) {
switch (appState) {
case FOREGROUND:
// Hocus Pocus...
break;
case BACKGROUND:
// Abracadabra!
break;
}
}
});
RxSample doesn't work.
I think it is not invoked start method, right?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.