使用Action启动/关闭Service

前面有用使用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);

 

posted @ 2016-04-15 10:07  安谧世界  阅读(1230)  评论(0编辑  收藏  举报