I'm a fairly new developer to android, so I get the feeling there's some property of services that I am overlooking. Please let me know.
Everything seems to be working just fine as far as services go. I have an IntentService class that looks like this:
public class MyIntentService extends IntentService
{
myClass myObject;
int test;
public MyIntentService()
{
super("MyIntentService");
test = 12;
myObject = new myClass(this, R.raw.file);
}
#Override
public IBinder onBind(Intent intent)
{
return new LocalBinder<MyIntentService>(this);
}
#Override
protected void onHandleIntent(Intent intent) {
}
The constructor for myClass accepts a Context, and then an int for the raw resource. If I call the constructor in an Activity class it works just fine. The service itself also seems to be working because if I comment out the object stuff, I can retrieve the test int no problem.
So what am I missing here?
Thank you very much!
Related
Is it a good practice to inject a Singleton to a BroadcastReceiver?
More specifically lets assume I have singleton like the following:
#Singleton
public class UnitProvider {
private boolean mIsUsingCelsius = false;
protected SharedPreferences mSharedPrefs;
#Inject
public UnitProvider(SharedPreferences sharedPrefs) {
mSharedPrefs = sharedPrefs;
mIsUsingCelsius = isUsingCelsiusPref(Locale.getDefault());
}
public void refreshCelsius() {
if (!mSharedPrefs.contains(SharedPreferencesConstants.SP_KEY_USE_CELSIUS)) {
mIsUsingCelsius = isUsingCelsiusBasedOnLocale(Locale.getDefault());
}
}
}
And there is a broadcast receiver:
public class DummyReceiver extends BroadcastReceiver {
#Inject protected UnitProvider mUnitProvider;
#Override
public void onReceive(Context context, Intent intent) {
DependencyInjectionService.inject(this);
mUnitProvider.refreshCelsius();
}
}
Actually it works but I am not sure about the performance and possible memory leak that situation may cause. Is there any idea about performance and possible lags that injection may cause?
This should be OK. A BroadcastReceiver instance is only alive for as long as it takes it to return from onReceive. It will be eligible for garbage collection after that as long as you didn't do something silly like hold a reference to it.
Also, you can't really "leak" a singleton object because basically they are expected to last forever!
I would like to inject a bound service into my activity via Dagger2.
My Service is declared as follow:
Public class MyService extends Service{
private final IBinder mBinder = new LocalBinder();
...
public class LocalBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public void doBackgroundTask() {
...
}
}
My Activity:
public abstract class MainActivity extends AppCompatActivity {
#Inject
MyService service;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
service.doBackgroundTask();
...
}
How would you achieve that ? Where will you put the ServiceConnection (into a base class, inside the module) ?
Thanks a lot.
I don't think that injecting the service that way (via injected field) will work because you don't control the instantiation of the service's object.
If your service contains fields that need to be injected you will have to inject in onCreate() the same way you inject your activities, i.e. calling DaggerMyAppComponent.inject(this).
About the ServiceConnection: you will have to do it the usual way as described in http://developer.android.com/guide/components/bound-services.html
I was dealing with the same concept/problem and like #Ognyan says - you won't have control over creating service.
I think this: How to access service functions from fragment, which is bound to parent activity in Android? might help you.
You may instantiate service in Application/Activity and communicate with it as described in the attached link.
You may also think of putting the interface (which is communicating with service) in base abstract class (BaseActivity or BaseFragment) which Fragment/Activity inherits and then easily reach the interface in any fragment or activity you need.
Hope it solves your issue.
I have an Object that i use in all my activities which holds a lot of complex data in it, it would be quite an hassle to use Android framework of saving the object state and passing it around from activity to activity, so i thought it would be possible to make a Singleton that manages this object and makes it live as long as the application lives.
Tried to use regular Java Singleton scheme, with normal class and normal static instance, but the instance becomes null after a while (which is very confusing, why would an Object that is still referenced be turned to null and garbage collected?). so i decided to flow with Android designers and created a Service to manage this Object, the Service looks something like that :
public class DataService extends Service {
private Data data;
private static DataService instance;
#Override
public void onCreate() {
instance = this;
data= new Data(...);
instance.data.addProgressListener(listener);
(new Thread() {
#Override
public void run() {
data.doInitProgress();
listener = null;
};
}).start();
}
public static void listenToInitDataProcess(final ProgressBar progressBar,final Runnable onDone) {
listener = new ProgressListener() {
private int progress;
private int max;
#Override
public void onUpdateProgress(final long i) {
progressBar.setProgress(progress+=i);
}
#Override
public void onProgressEndComputed(final long n) {
progressBar.setMax(max=(int) n);
}
#Override
public void onDone() {
progressBar.setProgress(max);
onDone.run();
}
};
if (instance!=null) instance.data.addProgressListener(listener);
}
public static Data getData() {
return instance.data;
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
now the problem with that is that after a while that the app is on i get NPE caused by instance is null... notice that i was listenning to the data object creation and i was trying to get it only after it was once inited, so no way that instance was suppose to be null...
how to do this right then?
If you want an Object that lives as long as your application lives (i.e. as long as its process is not killed by OS) you can extend android.app.Application, put your 'global' data there and use that subclass as your app context (needs to be declared in manifest)
However many argue that singletons provide essentially the same result as custom context e.g.
Singletons vs. Application Context in Android?
but the instance becomes null after a while
First, understand Effective Java's singleton recommendations, and how something wouldn't become null:
// Singleton with public final field
public class Elvis {
public static final Elvis INSTANCE = new Elvis();
private Elvis() { ... }
public void leaveTheBuilding() { ... }
}
But a Service is different, since there is no public constructor there. So, first check if the Service is running (as referenced in this post: Check if Service is running from a Broadcast Receiver):
private boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DataService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Then define some methods in your Service to return your needed state variables.
I want to implement AsyncTaskLoader for my custom data source:
public class DataSource {
public interface DataSourceObserver {
void onDataChanged();
}
...
}
DataSource will keep list of registered observers and will notify them about changes. CustomLoader will implement DataSourceObserver. The question is how to properly notify CustomLoader since Loader.onContentChanged() must be called from UI thread but in my case DataSource operations (and calls to DataSourceObserver.onDataChanged()) will be done from background threads.
Updated with idea from Selvin tip:
public class CustomLoader extends AsyncTaskLoader<...> implements DataSource.DataSourceObserver {
private final Handler observerHandler;
public CustomLoader(Context context) {
super(context);
observerHandler = new Handler()
}
#Override
public void onDataChanged() {
observerHandler.post(new Runnable() {
#Override
public void run() {
onContentChanged();
}
});
}
}
I've had a lot of success using Local Broadcasts in a case that's very similar to yours. The method involves an AsyncTaskLoader implementation that will register a BroadcastReceiver listening for a particular String that describes what's changed. This BroadcastReceiver keeps a reference to the Loader and calls onContentChanged. When the data needs a refresh, make the Local Broadcast with the aforementioned String and the BroadcastReceiver will hear it and trigger the load. Here's some example code, it may not work perfectly if you drop it in, I've generalized some class names, but hopefully you'll get the idea:
Broadcast Receiver to be used in your Loader Implmentation:
public class LoaderBroadcastReceiver extends BroadcastReceiver
{
private Loader loader;
public LoaderBroadcastReceiver(Loader loader)
{
this.loader = loader;
}
#Override
public void onReceive(Context context, Intent intent)
{
loader.onContentChanged();
}
}
Loader Implementation registers the Receiver in onStartLoading()
private LoaderBroadcastReceiver loaderBroadcastReceiver = null;
#Override
protected void onStartLoading()
{
//... some code here
if(loaderBroadcastReceiver == null)
{
loaderBroadcastReceiver = new LoaderBroadcastReceiver(this);
LocalBroadcastManager.getInstance(getContext()).registerReceiver(loaderBroadcastReceiver, new IntentFilter("NEWDATASTRING"));
}
//... some more code here
}
Finally, here's how onDataChanged in DataSource will make the Broadcast. It'll need a Context to help send the Broadcast. Since this can be called from an arbitrary Thread, I'd use your ApplicationContext, since an Context from an Activity could cause problems if the Activity is destroyed.
public class DataSource
{
public interface DataSourceObserver
{
void onDataChanged(Context applicationContext)
{
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("NEWDATASTRING"));
}
}
...
}
You'll probably want to play with it a bit to see how it works for you. You can use different Strings to differentiate different data that needs loading. You'll also want to unregister the Receiver at some point, perhaps in onReset(). Let me know if any of this in unclear in the comments, I'll try my best to clarify.
In android i am using two services and i want to move some details from one to another service. Becouse of this i create new class Settings with set and get methods but i don't know how to connect two services with this one class file that i can move details from one service to another and remember each one.
for example:
i want to transfer property Boolean from service 1 to service 2 and then in service 2 check if this property is true and if is true then i execute some code in this seocnd service... hope is better explanation
class example:
public class Settings {
private int currentAudioManager;
private Boolean isChanged;
public int getCurrentAudioManager() {
return currentAudioManager;
}
public void setCurrentAudioManager(int currentAudioManager) {
this.currentAudioManager = currentAudioManager;
}
........
Hope you understand what i want.
You can register broadcast receiver and send broadcasts parceable data between your services.
public class Service1 extends Service {
#Override
public void onCreate() {
super.onCreate();
registerReceiver(recevier1, new IntentFilter("YOUR_SERVICE_ACTION1"));
// if something happen in your service"
sendBroadcast(new Intent("YOUR_SERVICE_ACTION2")); // send to second service
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
unregisterReceiver(recevier1);
};
Service1Receiver recevier1 = new Service1Receiver();
private class Service1Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
intent.getExtra("me.SERVICE");// handle your data
}
}
}