• Android Studio 之 启动和停止服务


    运行截图
    这里写图片描述
    MainActivity.java
    运行前
    这里写图片描述
    这里写图片描述

    package csdn.example.com.pushmessage;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    import csdn.example.com.pushmessage.ServiceTest.MyService;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button startService = (Button) findViewById(R.id.start_service);
            Button stopService = (Button) findViewById(R.id.stop_service);
            startService.setOnClickListener(this);
            stopService.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.start_service:
                    Intent startIntent = new Intent(this, MyService.class);
                    startService(startIntent);//启动服务
                    break;
                case R.id.stop_service:
                    Intent stopIntent = new Intent(this,MyService.class);
                    stopService(stopIntent);//停止服务
                    break;
                default:
                    break;
            }
        }
    }
    

    停止服务后运行截图
    这里写图片描述
    这里写图片描述
    截图MyService.java

    package csdn.example.com.pushmessage.ServiceTest;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.util.Log;
    
    public class MyService extends Service {
        public MyService() {
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Log.d("MyService","onCreate executed");
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.d("MyService","onStartCommand executed");
            return super.onStartCommand(intent, flags, startId);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            Log.d("MyService","onDestroy executed");
        }
    }
    

    清单文件 AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="csdn.example.com.pushmessage">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <service
                android:name=".ServiceTest.MyService"
                android:enabled="true"
                android:exported="true"></service>
        </application>
    
    </manifest>
  • 相关阅读:
    C#中IDisposable学习
    C# volatile与lock
    TFS源代码管理
    C#如何获取真实IP地址
    c# 协变和逆变
    基本数据类型
    用户交互
    变量.常量
    输出语句,注释,
    基础知识随笔
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781920.html
Copyright © 2020-2023  润新知