Lucas Bailey
Lucas Bailey

Reputation: 717

How to determine which activity started a service?

I am making an Android app plug in. I am trying to make it as universal as possible, and this is creating an issue for me. I am designing the plug in so that any future apps that I make can easily implement the plug in. The issue that I have is that the plug in contains a service that periodically will alert the user via notifications. When the user opens the notification bar and touches the notification, it needs to open the activity that originally started the service.

The service is started using this code:

    startService(new Intent(this, myService.class));

And the service launches the notification using this code:

    Intent notificationIntent = new Intent(this, myClass.class);

Currently, myClass.class is hardcoded into the notification. This means that no matter how many different apps I make, if I use this plug-in, I have to always name the class that launches the service "myClass".

I would like a universal way to get the original activity that started the service.

Upvotes: 2

Views: 1055

Answers (4)

Lucas Bailey
Lucas Bailey

Reputation: 717

Okay, i figured it out. I'm not sure if this is a little "hackish" but what I didn't know before posting the original question is that you can create a generic intent, and then set the class information using strings of the path to the package and class. This is a copy of the code that I used.

On the activity starting the service, before actually starting the service:

    serviceIntent.putExtra("PackagePath",this.getPackageName());
    serviceIntent.putExtra("ClassPath", this.getPackageName() + "." + this.getLocalClassName());

And inside the service onStart event:

    packagePath = intent.getExtras().getString("PackagePath");
    classPath = intent.getExtras().getString("ClassPath");

And finally when setting up the notification in the service:

   Intent notificationIntent = new Intent();
   notificationIntent.setClassName(packagePath, classPath);

An example of what would be contained in the two string variables:

    packagePath = "myApp.main"
    classPath = "myApp.main.mainActivity"

Special thanks to CommonsWare for pointing me on the path of using Parceables because searching for examples of how to pass a Parceable via intent brought me to

Upvotes: 2

anticafe
anticafe

Reputation: 6892

Try this code,

if(this instanceof Activity1) {
    //is Acitivty 1
}
else if (this instanceof Acitivity2){
    //is Activity2
}

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007218

I would like a universal way to get the original activity that started the service.

Put your desired information in Intent extras in the Intent used with startService(). There is no way for a service to determine what activity started it, in large part because a service does not have to be started by an activity...

Upvotes: 3

Dheeraj Vepakomma
Dheeraj Vepakomma

Reputation: 28737

Perhaps you can use one of the putExtra() methods?

Upvotes: 0

Related Questions