如何通过隐式意图实现跨应用启动Service

跨应用启动Service需要注意的地方有2点:

1.目标service需要声明exported=true 的属性,表示允许其他应用访问该服务.
2.android5.0之前是可以通过设置隐式意图来跨应用打开Service的,5.0之后就必须要通过显示意图来开启Service.
如何创建跨应用的显示意图呢?

通过Intent的setComponent方法,可以传递一个ComponentName对象,该对象有一个接受2个参数的构造方法,第一参数传递目标Service所在的包名,第二个参数传递目标Service的完整类名.

Demo的结构:

本Demo是通过app2工程的App2Activity打开app工程的AppService服务.同时实现跨应用传递数据的功能
 

AppService代码:

public class AppService extends Service {
    public AppService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i("AppService", "onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (null != intent) {
            Log.i("AppService", "onStartCommand接收到的数据是:" + intent.getStringExtra("data"));
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("AppService", "onDestroy");
    }
}

清单文件配置:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mchenys.aidlservicedemo" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name=".AppService"
            android:enabled="true"
            android:exported="true" />
    </application>

</manifest>
 

App2Activity代码:

public class App2Activity extends AppCompatActivity {


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

        //注意:android 5.0之前是可以通过隐式意图打开其他app的服务的,5.0之后只能通过显式意图来打开.
        final Intent intent = new Intent();
        //ComponentName的参数1:目标app的包名,参数2:目标app的Service完整类名
        intent.setComponent(new ComponentName("com.example.mchenys.aidlservicedemo", "com.example.mchenys.aidlservicedemo.AppService"));
        //打开目标AppService
        findViewById(R.id.id_btn_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置要传送的数据
                intent.putExtra("data", "Hello AppService,I am App2Activity");
                startService(intent);
            }
        });
        //关闭目标AppService
        findViewById(R.id.id_btn_stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopService(intent);
            }
        });
    }
}
布局文件的效果就是2个Button,分别用于开启目标服务和关闭目标服务
效果图:
点击启动APPSERVICE后,查看控制台的log.
11-01 10:58:57.691 29359-29359/? I/AppService: onCreate
11-01 10:58:57.692 29359-29359/? I/AppService: onStartCommand接收到的数据是:Hello AppService,I am App2Activity

由控制台可以看到目标Service被开启了.同时在onStartCommand方法中接收到了我们通过显示意图传递过去的参数.
当点击停止APPSERVICE后,再查看控制台log:
11-01 11:00:38.350 29359-29359/? I/AppService: onDestroy
可以看到目标Service的onDestory方法被调用了,说明跨应用关闭服务也成功了.
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值