Android跨应用启动Service

摘要:
importandroid.app.IntentService;importandroid.support.annotation.Nullable;publicclassMyIntentServiceextendsIntentService{publicMyIntentService(){super(“MyIntentServices”);

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

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

Android跨应用启动Service第1张

步骤二:在项目一中创建一个自定义类继承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: 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动其他APP中的service" />

    <Button
        android: 
        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 的情况下服务才会被激活,否则不会激活。

免责声明:文章转载自《Android跨应用启动Service》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ubuntu下升级R版本为什么使用C#开发软件的公司和程序员都很少?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

STL之六:map/multimap用法详解

转载于:http://blog.csdn.net/longshengguoji/article/details/8547007 map/multimap     使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理。它们可根据key的排序准则自动将元素排序。mul...

禁止、允许MySQL root用户远程访问权限

禁止:1、进入mysql; 2、 mysql> use mysql; 3、执行修改权限语句(禁用); mysql> update user set host = "localhost" where user = "root" and host = "%"; 4、刷新权限; mysql> plush privileges;   启用: 1、...

thinkphp 验证码

在控制器中定义一个控制器,一定要开启 session class PublicAction extends Action {    public function verify()    {        session('[start]');        import('ORG.Util.Image');        Image::buildImag...

vue 使用vuedraggable 实现列表拖拽排序

首先安装  npm install vuedraggable --save 页面使用时引入import Draggable from 'vuedraggable' export default { name: "draggable", components: { Draggable }, data(){ return {...

【Vue】基于UI库二次组件封装——ant design table(包括支持slot插槽)

vue现在使用非常广泛,对于一些公用的功能我们通常也会封装成组件,同时还有各类的UI组件库给我们开发提供了便利。 为什么要封装成组件 能够把页面抽象成多个相对独立的模块 实现代码重用,提高开发效率和代码质量,使得代码易于维护 为什么要讲基于第三方UI库封装组件 这段时间经手了几个项目,都是后台管理系统的,大家知道后台管理系统最多的就是table以及...

【转载】BootStrap表格组件bootstrap table详解

(转载,来源“脚本之家”,作者不详) 一、Bootstrap Table的引入 关于Bootstrap Table的引入,一般来说还是两种方法: 1、直接下载源码,添加到项目里面来。由于Bootstrap Table是Bootstrap的一个组件,所以它是依赖Bootstrap的,我们首先需要添加Bootstrap的引用。 2、使用我们神奇的Nuget打开...