运行截图
Android Studio 之 启动和停止服务_第1张图片
MainActivity.java
运行前
Android Studio 之 启动和停止服务_第2张图片
这里写图片描述

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;        }    }}

停止服务后运行截图
Android Studio 之 启动和停止服务_第3张图片
Android Studio 之 启动和停止服务_第4张图片
截图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>

更多相关文章

  1. Android上SD卡图片的标准缓存方法
  2. Android实现图片预览浏览功能
  3. android 将图片转换成二进制,再讲二进制转换成图片 可用于编码加
  4. android缩放大图片加载
  5. RadioButton使用的过程中Text文本和图片显示的问题
  6. android 使用handler更新ui,使用与原理分析详解(附上代码以及截图)
  7. Android 三大图片缓存原理、特性对比

随机推荐

  1. Linux服务器权限管理实践——添加用户只
  2. 《深入理解Linux网络技术内幕》阅读笔记(
  3. Linux Tcp/ip UDP基本概念(6.8)
  4. Codeigniter2.25部署Linux(php5.6)
  5. 在Linux上确定陈旧的Samba挂载的最快方法
  6. Linux无法连接网络解决方案
  7. Linux环境下注册函数的调用顺序
  8. 浅谈如何学习linux
  9. 软交换FreeSWITCH系统概要和源代码分析预
  10. linux终端常用快捷键