سؤال

I'm writing an app that makes use of the fileobserver in android. When I file is modified in anyway (creation, etc.) it should upload. In order to implement a queue and keep from running out of memory by using threads and such, I want to use an IntentService to upload the files one at a time, and keep from executing when it fails so that when the user regains connectivity, it uploads. But here is the problem:

All of the given constructors for Intent...

Intent();
Intent(Intent );
Intent(String );
Intent(Context, Class<?>); //The one I always use
Intent(String, Uri );
Intent(String, Uri, Context, Class<?> );

...wont work. Because I'm working in an extension of FileObserver, there is no acceptable context from which I can feed the Intent. Is there anyway I can use just the String one to start the IntentService? I don't need it to return anything, I just need to feed it a few strings for it to work (filename, etc.) Or is there another way I can start the IntentService. This one's really got me stumped.

Thanks for any help in advanced!

Edit: Alright. I've tried many contexts but none see to work. The intent service isn't being fired by application or base contexts from the service class being passed through the constructors as they're called. Here is the basic layout of my app:

Login Activity goes to Menu Activity, Menu Activity starts Service, Service Starts Service Handler (see the example of a service in the android docs to see what it is) and passes the context through the constructor. Service Handler starts FileSync and passes context through the constructor. FileSync holds onto the context in a class variable. FileSync then waits until a file is modified and then uses the context to start the IntentService to upload the file requested through the Intent. I know it's really complicated, but it's really the only way I can do it at the moment.

هل كانت مفيدة؟

المحلول

You can create a static field of you Application by overriding Application. In oncreate

sInstance = this;

Use

MyApplication.sInstance.startActivity(intent);

Edit: On second thought you could use PendingIntent if all you need to do is start a service without passing any information from FileObservver, you can create a PendingIntent and set it at the time of creation of FileObserver and use

pendingintent.send()

نصائح أخرى

Because I'm working in an extension of FileObserver, there is no acceptable context from which I can feed the Intent.

This FileObserver cannot exist in a vacuum. It needs to be managed by an activity or service, and that will give you your Context.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top