Android 服务 (Service) 示例:启动、绑定和通信

本示例演示了 Android 中如何启动和绑定服务,以及如何在绑定服务的情况下实现 Activity 与服务之间的通信。代码包含 Java 代码和 layout 代码,并使用日志记录观察服务生命周期。

Java 代码:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = 'MainActivity';

    private MyService.MyBinder binder;
    private boolean isBound = false;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (MyService.MyBinder) service;
            isBound = true;
            Log.d(TAG, 'onServiceConnected: Service is bound');
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            isBound = false;
            Log.d(TAG, 'onServiceDisconnected: Service is unbound');
        }
    };

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

        Button startServiceBtn = findViewById(R.id.start_service_btn);
        Button bindServiceBtn = findViewById(R.id.bind_service_btn);
        Button callServiceFuncBtn = findViewById(R.id.call_service_func_btn);

        startServiceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MyService.class);
                startService(intent);
                Log.d(TAG, 'onClick: Service is started');
            }
        });

        bindServiceBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MyService.class);
                bindService(intent, connection, BIND_AUTO_CREATE);
                Log.d(TAG, 'onClick: Service is bound');
            }
        });

        callServiceFuncBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isBound) {
                    binder.getService().serviceFunc();
                    Log.d(TAG, 'onClick: Service function called');
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isBound) {
            unbindService(connection);
            isBound = false;
            Log.d(TAG, 'onDestroy: Service is unbound');
        }
    }
}

layout 代码:

<?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'>

    <Button
        android:id='@+id/start_service_btn'
        android:text='启动服务'
        android:layout_width='match_parent'
        android:layout_height='wrap_content'/>

    <Button
        android:id='@+id/bind_service_btn'
        android:text='绑定服务'
        android:layout_width='match_parent'
        android:layout_height='wrap_content'/>

    <Button
        android:id='@+id/call_service_func_btn'
        android:text='调用服务方法'
        android:layout_width='match_parent'
        android:layout_height='wrap_content'/>

</LinearLayout>

注意:

  • MyService 类需要定义服务,并在其中实现 serviceFunc() 方法。
  • 需要在清单文件中声明服务。
  • 可以使用 Logcat 查看日志记录,了解服务生命周期。

希望这个示例能够帮助您理解 Android 服务的概念和使用方法。

Android 服务 (Service) 示例:启动、绑定和通信

原文地址: https://www.cveoy.top/t/topic/mRjH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录