Android跨应用启动Service

Android5.0之后规定只能通过显示Intent启动服务,所以掌握以下的启动方式很有必要

 

步骤一:创建两个安卓项目one,two

 

 

步骤二:在项目一中创建一个自定义类继承Service

MyIntentService.java

 

package com.contentprovide.liuliu.a2_3;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;


public class MyIntentService extends IntentService {

    public MyIntentService() {
        super("MyIntentService");
    }
    

    @Override
    protected void onHandleIntent(Intent intent) {

    }


    @Override
    public void onStart(@Nullable Intent intent, int startId) {
        super.onStart(intent, startId);

        System.out.println("MyIntentService onStart==============================");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        System.out.println("MyIntentService onDestroy==============================");

    }
}

 

步骤三:项目二的布局文件activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.contentprovide.liuliu.two.MainActivity">


    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动其他APP中的service" />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止其他APP中的service" />


</LinearLayout>

 

 

步骤四:项目二中java代码实现启动项目一种的Service:

package com.contentprovide.liuliu.two;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button btn_start, btn_stop;

    Intent intent;

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

        intent = new Intent();
        /*
        * 第一个参数:放入需要打开Service所在的包路径
        * 第二个参数:放入需要打开Service的所在包路径和Service类名
        * */
        intent.setComponent(new ComponentName("com.contentprovide.liuliu.a2_3", "com.contentprovide.liuliu.a2_3.MyIntentService"));


        btn_start = (Button) findViewById(R.id.btn_start);
        btn_stop = (Button) findViewById(R.id.btn_stop);

        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startService(intent);
            }
        });

        btn_stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                stopService(intent);
            }
        });


    }


}

 

注意:被启动的Service需要在所在的项目的AndroidManifest.xml中声明Service权限,exporter的属性为true,否则不能跨应用启动

 

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

 

 

 

 

  • android:exported:代表是否能被其他应用隐式调用,其默认值是由service中有无intent-filter决定的,如果有intent-filter,默认值为true,否则为false。为false的情况下,即使有intent-filter匹配,也无法打开,即无法被其他应用隐式调用。

  • android:name:对应Service类名

  • android:permission:是权限声明

  • android:process:是否需要在单独的进程中运行,当设置为android:process=”:remote”时,代表Service在单独的进程中运行。注意“:”很重要,它的意思是指要在当前进程名称前面附加上当前的包名,所以“remote”和”:remote”不是同一个意思,前者的进程名称为:remote,而后者的进程名称为:App-packageName:remote。

  • android:isolatedProcess :设置 true 意味着,服务会在一个特殊的进程下运行,这个进程与系统其他进程分开且没有自己的权限。与其通信的唯一途径是通过服务的API(bind and start)。

  • android:enabled:是否可以被系统实例化,默认为 true因为父标签 也有 enable 属性,所以必须两个都为默认值 true 的情况下服务才会被激活,否则不会激活。

 

转载于:https://www.cnblogs.com/lyd447113735/p/8409785.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值