public
class
MainActivity
extends
Activity
implements
OnClickListener
{
private
TextView textView;
@Override
protected
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button_startservice).setOnClickListener(
this
);
findViewById(R.id.button_stopservice).setOnClickListener(
this
);
findViewById(R.id.button_stopforeground).setOnClickListener(
this
);
textView =(TextView) findViewById(R.id.textView1);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
@Override
public
void
onClick(View v)
{
switch
(v.getId())
{
case
R.id.button_startservice:
StartService();
break
;
case
R.id.button_stopservice:
StopService();
break
;
case
R.id.button_stopforeground:
StopForeground();
break
;
default
:
break
;
}
}
private
void
StopForeground()
{
Intent intent =
new
Intent(
this
, MyService.
class
);
intent.putExtra(
"isStop"
,
true
);
startService(intent );
}
private
void
StopService()
{
Intent intent =
new
Intent(
this
, MyService.
class
);
stopService(intent );
}
private
void
StartService()
{
Intent service =
new
Intent(
this
, MyService.
class
);
startService(service );
}
}