安卓四大组件之一service

                                               service作为安卓四大组件之一,主要处理后台事务,和用户直接交互通过toast和notification这里浅谈service的基本使用

XML文件:

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

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="启动service" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="bind"
        android:text="绑定启动service" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="stop"
        android:text="停止service" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="unbind"
        android:text="解除綁定service" />
</LinearLayout>

java代码service

//创建service
public class MyService extends Service {


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        //通过onBind启动service可以获取自定义的service的引用调用service的方法。
        return new MyBinder();
    }


    //如果需要使用onBind,必须创建个内部类继承Binder
    public class MyBinder extends Binder {
        //通过内部类获取外部类引用返回出去
        public MyService getService() {
            //把创建的service返回出去
            return MyService.this;
        }

    }


    //创建个方法可以被外部持有service的引用调用
    public void showToast() {
        Toast.makeText(this, "你好:我是service的方法", Toast.LENGTH_SHORT).show();
        //一般在service做耗时任务比如下载
        //service是个没有界面的组件,和用户交互通过toast和notification。
        //service启动需要依附activity启动。启动有直接启动和绑定启动

    }


    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate: ----------");
    }

    @Override
    public void onDestroy() {
        //可以调用下面方法自动停止service
        stopSelf();
        super.onDestroy();
        Log.e(TAG, "onDestroy: ----------");
    }
}


//java代码activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    //普通启动service
    Intent intent;

    public void start(View view) {
        intent = new Intent(this, MyService.class);
        startService(intent);

    }

    //普通停止service
    public void stop(View view) {
        stopService(intent);

    }


    //创建服务连接对象
    ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //连接成功

            //通过IBinder获取MyBinder对象
            MyService.MyBinder myBinder = (MyService.MyBinder) service;
            //通过MyBinder对象调用他的方法获取MyService引用
            MyService service1 = myBinder.getService();
            //通过MyService引用,调用他的方法
            service1.showToast();

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            //出現非正常断开连接执行这个方法
        }
    };

    //绑定启动
    public void bind(View view) {
        //参数(意图,服务连接对象,标记一般为绑定自动创建BIND_AUTO_CREATE)
       //BIND_AUTO_CREATE意思service創建就自動綁定,没有创建就创建再自动绑定
bindService( intent , conn , BIND_AUTO_CREATE) ; } //解除绑定 public void unbind(View view) { unbindService( conn) ; }}

//最后不要忘记在manifest文件注册

<!--注意 不要忘记注册 也可以设置白名单-->
<service android:name=".MyService">
    <intent-filter>
        <action android:name=""></action>
    </intent-filter>
</service>

总结:

service启动需要依附activity,如果是一般启动可以不停止service退出程序,service会一直在后台执行,如果你绑定启动必须解除绑定要不系统会抛出异常。

如果需要activity和service通讯必须要绑定启动,service启动了也可以绑定。

猜你喜欢

转载自blog.csdn.net/qq_36237165/article/details/69388538
今日推荐