zoukankan      html  css  js  c++  java
  • 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 的情况下服务才会被激活,否则不会激活。

  • 相关阅读:
    转:详解iPhone Tableview分批显示数据 点击加载更多
    能不写全局变量就不写全局变量。
    ios 打电话 一键拨号
    下一步目标:整理出1套相对成熟的ios 开发框架
    dispatch_sync 线程 GCD iOS
    iOS 播放声音 最简单的方法
    判断 网络是否通常,以及判断用户使用的网络类型,时2G\3G\还是wifi
    ios 特效 新思路 :加载gif 动画,然后在动画上增加点击事件即可。
    Oracle小技巧
    excel导出时”内存或磁盘空间不足“错误的解决方法
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/8409785.html
Copyright © 2011-2022 走看看