• 02_启动和销毁Service


    在Application关闭后,Service仍然会运行。

    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();
        }
    
    }
  • 相关阅读:
    asp.net :使用jquery 的ajax +WebService+json 实现无刷新去后台值
    如何清理数据库缓存
    如何在虚拟机中Linux+Oracle10gRAC安装
    ORA01031 权限不足存储过程中DBA 角色用户无法执行DDL
    如何查看存储过程执行计划
    如何查看执行计划
    如何使用tkprof
    C#位运算讲解与示例[转]
    C#中Invalidate() 方法
    如何创建强命名程序集, 如何查看强命名程序集的PublicKeyToken
  • 原文地址:https://www.cnblogs.com/510602159-Yano/p/4038532.html
Copyright © 2020-2023  润新知