首先聲明一個service:
我們提供的服務(wù)有:網(wǎng)站設(shè)計、成都做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、遜克ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的遜克網(wǎng)站制作公司
public class myservice extends Service {
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
System.out.println("start");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
System.out.println("stop");
}
}
并在Mainfiest中配置:
在MainActivity中啟動和停止service:
public class MainActivity extends Activity implements OnClickListener {
private Button start,stop;
private Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start=(Button) findViewById(R.id.start);
stop=(Button) findViewById(R.id.stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);
i=new Intent(this,myservice.class);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.start:
startService(i);
break;
case R.id.stop:
stopService(i);
break;
}
}
}