实例:创建、启动、停止和绑定一个Service

来源:互联网 发布:程序员工作累吗? 编辑:程序博客网 时间:2024/06/15 14:05

创建MainActivity:

import android.app.Activity;  import android.app.Service;  import android.content.ComponentName;  import android.content.Intent;  import android.content.ServiceConnection;  import android.os.Bundle;  import android.os.IBinder;  import android.util.Log;  import android.view.View;  import android.widget.Button;  import android.widget.Toast;  import layout.MyService;  public class MainActivity extends Activity implements View.OnClickListener {    private Button btn_start,btn_stop,btn_bind,btn_unbind;      private Intent intent;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn_start = (Button) findViewById(R.id.btn_start);        btn_stop = (Button) findViewById(R.id.btn_stop);        btn_bind = (Button) findViewById(R.id.btn_bind);        btn_unbind = (Button) findViewById(R.id.btn_unbind);        intent = new Intent(MainActivity.this, MyService.class);        btn_start.setOnClickListener(this);        btn_stop.setOnClickListener(this);        btn_bind.setOnClickListener(this);        btn_unbind.setOnClickListener(this);    }      @Override      public void onClick(View v) {          switch (v.getId()){              case R.id.btn_start:                  startService(intent);                  break;              case R.id.btn_stop:                  stopService(intent);                  break;              case R.id.btn_bind:                  bindService(intent, conn, Service.BIND_AUTO_CREATE);                  break;              case R.id.btn_unbind:                  unbindService(conn);                  break;          }      }      private ServiceConnection conn = new ServiceConnection() {          @Override          public void onServiceConnected(ComponentName componentName, IBinder iBinder) {              Log.i("SERVICE","连接成功");              Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_LONG).show();          }          @Override          public void onServiceDisconnected(ComponentName componentName) {              Log.i("SERVICE","断开连接");              Toast.makeText(MainActivity.this,"断开连接",Toast.LENGTH_LONG).show();          }      };  }

创建MyService:

import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Log;import android.widget.Toast;public class MyService extends Service {    @Override    public IBinder onBind(Intent intent) {        Log.i("SERVICE","<---------onBind--------->");        Toast.makeText(MyService.this,"<---------onBind--------->",Toast.LENGTH_LONG).show();        return null;  //可以返回null,通常返回一个有aidl定义的接口    }    // Service创建时调用    @Override    public void onCreate() {        Log.i("SERVICE","<---------onCreate--------->");        Toast.makeText(MyService.this,"<---------onCreate--------->",Toast.LENGTH_LONG).show();    }    //当客户端调用startService()方法启动Service时,该方法被调用    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i("SERVICE","<---------onStartCommand--------->");        Toast.makeText(MyService.this,"<---------onStartCommand--------->",Toast.LENGTH_LONG).show();        return START_STICKY;    }    //当Service不再使用时调用    @Override    public void onDestroy() {        Log.i("SERVICE","<---------onDestroy--------->");        Toast.makeText(MyService.this,"<---------onDestroy--------->",Toast.LENGTH_LONG).show();    }}

layout中activity_main是:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="绑定Service练习"        android:textSize="20dp"/>    <Button        android:text="@string/btn_start"        android:id="@+id/btn_start"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:text="@string/btn_stop"        android:id="@+id/btn_stop"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:text="@string/btn_bind"        android:id="@+id/btn_bind"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:text="@string/btn_unbind"        android:id="@+id/btn_unbind"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

结果如图:
这里写图片描述

0 0
原创粉丝点击