bkshukla
bkshukla

Reputation: 179

Can we run service without creating activity ? if yes why ? or if not then why not?

1.i just wanted to know can i start the the service without creating activity in android. if yes why ? or if not then why not?

  1. can i put my service creation code in oncreate method directaly?
  2. Difference between Polymorphism and interface in java?

Upvotes: 1

Views: 1952

Answers (2)

Mr.S
Mr.S

Reputation: 94

  1. You can do it like in the answer of "droidhot". You can call it wherever you want (p.e. onReceive, onCreate, onResume). If you want to use the same code multiple times, i would make an own method and call it in the onResume or onCreate method like this:

    @Override
    public void onResume(){
        callService();
    }
    
    public void callService(){
        Intent service = new Intent(getBaseContext(), myServiceClass.class);
        getBaseContext().startService(service);
    }
    

    Or do you mean to make service without even a class?

2. For the Difference between Polymorphism and interface in java I can recommend "http://java.sys-con.com/node/37695".

Upvotes: 0

Athul Harikumar
Athul Harikumar

Reputation: 2491

ofcourse you can start a service without activity (like something in a receiver class)

eg

    public class MyStartServiceReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("", "in start service");
    Intent service = new Intent(context, rs.class);
    context.startService(service);
}
  } 

and an interface is a class that is not fully defined ie: there wont be any concrete methods in it something like abstract class but an abstract class can contain concrete methods where as an interface cannot

Upvotes: 1

Related Questions