美文网首页
Service启动和停止

Service启动和停止

作者: 昨天剩下的一杯冷茶 | 来源:发表于2018-11-05 11:08 被阅读8次

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.hzx.myservice.MainActivity">

    <Button
        android:id="@+id/start_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Service"/>

    <Button
        android:id="@+id/stop_service"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop Service"/>
    
</LinearLayout>

创建一个MyService 继承 Service

public class MyService extends Service {

    private final String TAG = "123";

    public MyService(){
    }

    public IBinder onBind(Intent intent){
        //throw new UnsupportedOperationException("Not yet implemented");
        return null;
    }
    public void onCreate(){
        super.onCreate();
        Log.d(TAG,"-->onCreate");
    }

    public  int onStartCommand(Intent intent, int flags, int startId){
        Log.d(TAG,"-->onStartCommand");
        return super.onStartCommand(intent,flags,startId);
        //stopSelf();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
    }
}

Activity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private final String TAG = "123";

    @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(MainActivity.this);
        stopService.setOnClickListener(MainActivity.this);
    }

    public void onClick(View v){
        switch (v.getId()){
            case R.id.start_service:
                Log.d(TAG,"-->activity start_service");
                Intent startIntent = new Intent(this,MyService.class);
                startService(startIntent);
                break;
            case R.id.stop_service:
                Log.d(TAG,"-->activity stop_sercie");
                Intent stopIntent = new Intent(this,MyService.class);
                stopService(stopIntent);
                break;

                default:
                    break;
        }
    }
}

注意:
1、 需要在AndroidManifest.xml文件注册

        <service android:name=".MyService"
            android:enabled="true"
            android:exported="true"/>

运行效果

image.png

相关文章

  • MySQL常用一些命令

    MySQL管理服务的命令(启动、停止、重启) 启动 service mysql start 停止 service ...

  • docker的常用命令

    1.docker的启动、停止、重启 启动:service docker start 停止:service dock...

  • Service启动和停止

    Layout 创建一个MyService 继承 Service Activity 注意:1、 需要在Android...

  • LINUX重启MYSQL的命令

    如何启动/停止/重启MySQL 一、启动方式 1、使用 service 启动:service mysqld sta...

  • Linux下MySQL

    如何启动/停止/重启MySQL 一、 启动方式 1、使用 service 启动:service mysql sta...

  • Linux如何启动停止Service

    查看service状态 启动service 停止service 查看是否自动重启 启动自动重启 关闭自动重启

  • Android服务的启动模式

    Android 服务两种启动方式的区别 关键字:Android Service服务的启动和停止方式 Service...

  • 2018-01-09

    可以用以下命令启动和停止服务: /sbin/service crond start /sbin/service c...

  • MongoDB

    启动 :sudo service mongod |mongodb start停止 :sudo service mo...

  • Redis 启动,停止,重启

    启动服务: service redis start 停止服务: service redis stop 重启服务: ...

网友评论

      本文标题:Service启动和停止

      本文链接:https://www.haomeiwen.com/subject/omojxqtx.html