Reza_Rg
Reza_Rg

Reputation: 3374

trouble in starting service when click a button

I have following piece of code in my application:

.... tb1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

    // Auto-generated method stub
    startService(Intent this.Main);
}
});
}

public void onStart(Intent intent, int startid) {

    Toast.makeText(context, "yessssss", Toast.LENGTH_LONG).show();
 //and do something//

}

and I want to start service when user click on "tb1" button,

I have tried:

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

and

startService(Main.class);

but none of them started service, what should i do ?

Upvotes: 2

Views: 2928

Answers (4)

user1788155
user1788155

Reputation: 1

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 

is not compiling. Remove arguments to match Intent() is appearing with X mark.

A small change will work.

Intent myIntent = new Intent(MainActivity.this, MyService.class); //is working both
startService(myIntent); and stopService(myIntent); // also working.

Upvotes: 0

user1788155
user1788155

Reputation: 1

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 

is no compiling Remove arguments to match 'Intent()' is appearing with X mark.

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Change your code as for starting Service on Button Click:

Intent intent = new Intent(Current_Activity.this, Main.class);
startService(intent); 

and make sure you have registered you service in Manifast.xml as:

<service
    android:name=".Main"/>

Upvotes: 4

Robin Chander
Robin Chander

Reputation: 7415

Try this

Intent myIntent = new Intent(this, Main.class);
startService(myIntent); 

Also add your service to your manifest

<service android:name="packagename.Main"></service> 

Upvotes: 0

Related Questions