Service使用

来源:互联网 发布:java源代码流程图 编辑:程序博客网 时间:2024/06/07 04:16
                               **Service的使用**

Service和其他3大组件一样,自定义一个MyService继承Service.其实Service就是一个看不见界面的Activity!

一 ,自定义一个MyService继承Service

package com.zhang.myservice;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class DemoService extends Service {    @Override    public IBinder onBind(Intent intent) {        System.out.println("onBind");        return null;    }    @Override    public void onCreate() {        System.out.println("onCreate");        super.onCreate();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        System.out.println("onStartCommand");        return super.onStartCommand(intent, flags, startId);    }    @Override    public void onDestroy() {        System.out.println("onDestroy");        super.onDestroy();    }}

1.public void onCreate() 当服务第一次创建的时候调用
2.public int onStartCommand() onStart()方法被onStartCommand()取代了 .service再次重启,onStartCommand会被调用
3.public void onDestroy() 当服务销毁的是调用

二,AndroidManifest.xml注册自定义的Service

 <!--配置服务  -->        <service android:name="com.zhang.myservice.DemoService">        </service>

三,MainActivity.java中开启,关闭Service

package com.zhang.myservice;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {    private MyConn conn;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    //点击按钮 开启服务  通过startservice    public void click1(View v) {        Intent intent = new Intent(this,DemoService.class);        startService(intent); //开启服务        Toast.makeText(this,"startservice-开启服务",Toast.LENGTH_SHORT).show();    }    public void click2(View v) {        Intent intent = new Intent(this,DemoService.class);        stopService(intent);        Toast.makeText(this,"stopService-关闭服务",Toast.LENGTH_SHORT).show();    }    //点击按钮 绑定服务 开启服务的第二种方式    public void click3(View v){        Intent intent = new Intent(this,DemoService.class);        //连接到DemoService 这个服务        conn = new MyConn();        bindService(intent,conn , BIND_AUTO_CREATE);        Toast.makeText(MainActivity.this,"bindService-绑定服务",Toast.LENGTH_SHORT).show();    }    //点击按钮手动解绑服务    public void click4(View v){        unbindService(conn);        Toast.makeText(this,"unbindService-手动解绑服务",Toast.LENGTH_SHORT).show();    }    @Override    protected void onDestroy() {        //当Activity销毁的时候 要解绑服务        unbindService(conn);        super.onDestroy();    }    //定义一个类 用来监视服务的状态    private class MyConn implements ServiceConnection{        //当服务连接成功调用        @Override        public void onServiceConnected(ComponentName name, IBinder service) {        }        //失去连接调用        @Override        public void onServiceDisconnected(ComponentName name) {        }    }}
原创粉丝点击