前面有用使用Intent来控制  Service

使用的参数是Service的类

Service的启动/关闭还有另外一种方式来控制

通过Intent传入Action的参数

 

 

在manifest中注册一个Service并且设置一个action

<service     
            android:enabled="true" 
            android:exported="false"
            android:name="com.services.sev.PlayService" >
            <intent-filter>
                <action android:name="com.example.codetest_1.action.startPlayService" />
            </intent-filter>
        </service>

注意这个name的字符串

可以在Service类中增加一个字段

public static final String ACTION = "com.example.codetest_1.action.startPlayService";

这样 只需修改Intent的调用方法 就可以启动/关闭Service了

Intent intent = new Intent();
            intent.setAction(PlayService.ACTION);
            this.startService(intent);
Intent intent = new Intent();
            intent.setAction(PlayService.ACTION);
            this.stopService(intent);

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2021-11-24
  • 2022-01-16
  • 2022-01-21
  • 2021-10-15
  • 2021-11-05
猜你喜欢
  • 2021-12-14
  • 2022-12-23
  • 2022-02-16
  • 2022-12-23
  • 2021-08-09
  • 2021-06-09
相关资源
相似解决方案