Pol Hallen
Pol Hallen

Reputation: 1862

stopService inside public void onClick

I created a service to start/stop a song, now when I click on "info", service stars correctly but I can't stop it. I should put an intent to stop service within "public void onClick" but I've an error.

this, is the code to stop service: stopService(new Intent(this, SobService.class));

private void Info(){

    startService(new Intent(this, SobService.class));
    LayoutInflater li = LayoutInflater.from(this);
    View view = li.inflate(R.layout.info, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(view).create();
    TextView text=(TextView) findViewById(R.id.infoView1);
    builder.setCancelable(false); 
    builder.setPositiveButton("Chiudi", new DialogInterface.OnClickListener()
    {  
           public void onClick(DialogInterface dialog, int id) {  
//stopService(new Intent(this, SobService.class));
               dialog.cancel();   
        }  
        });  
    builder.show();

Upvotes: 0

Views: 332

Answers (1)

Pratik
Pratik

Reputation: 30855

Write your Activity class name instead of only "this" in this statement

stopService(new Intent(YourActivity.this, SobService.class));

The only "this" can refer the anonymous class object itself not as your activity object

Upvotes: 1

Related Questions