02 _ start and destroy the service

Source: Internet
Author: User

After the application is disabled, the service will still run.

 

package com.example.servdemo;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.content.Intent;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.Button;import android.os.Build;public class MainActivity extends Activity implements OnClickListener {    private Button btnStartService, btnStopService;    private Intent serviceIntent;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                serviceIntent = new Intent(this, EchoService.class);                btnStartService = (Button) findViewById(R.id.btnStartService);        btnStopService = (Button) findViewById(R.id.btnStopService);                btnStartService.setOnClickListener(this);        btnStopService.setOnClickListener(this);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {                // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public void onClick(View v) {                switch (v.getId()) {        case R.id.btnStartService:            startService(serviceIntent);            break;        case R.id.btnStopService:            stopService(serviceIntent);            break;        }    }}
package com.example.servdemo;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class EchoService extends Service {    @Override    public IBinder onBind(Intent intent) {        return null;    }        @Override    public void onCreate() {        // TODO Auto-generated method stub        System.out.println("Service Start!");        super.onCreate();    }        @Override    public void onDestroy() {        // TODO Auto-generated method stub        System.out.println("Service Stop!");        super.onDestroy();    }}

 

02 _ start and destroy the service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.