Service2应用,在AndroidManifest.xml文件中对外暴露MyService2服务:

    <!--
代表在应用程序里,当需要该service时,会自动创建新的进程。
android:process=":remote" 是否可以被系统实例化
android:enabled="true" 代表是否能被其他应用隐式调用
android:exported="true"
-->
<service android:name=".service.MyServie2"
android:process=":remote"
android:enabled="true"
android:exported="true"> <intent-filter> <!-- 激活 MyService2 唯一name,不能重名-->
<action android:name="liudeli.service2.service.MyService2" /> </intent-filter> </service>

Service2应用,MyService2服务的代码:

package liudeli.service2.service;

import android.app.Service;

import android.content.Intent;
import android.os.IBinder; import liudeli.service2.MainActivity; public class MyServie2 extends Service { @Override
public IBinder onBind(Intent intent) {
serviceStartActivity();
return null;
} /**
* 在Service启动Activity,需要配置:.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
*/
private void serviceStartActivity() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}

Service2应用,MainActivity界面相关:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"
android:background="#fd00"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Service2 APP"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textSize="30sp"
android:textColor="@android:color/black"
/> </android.support.constraint.ConstraintLayout>

----------------------- 下面的代码是 Service1应用相关的

Service1应用,去启动Service1应用 的服务连接代码:

package liudeli.service1;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View; public class StartRemoteActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_remote);
} private boolean startRemoteServiceActivity; public void startRemoteServiceActivity(View view) {
Intent intent = new Intent();
intent.setAction("liudeli.service2.service.MyService2");
// 注意:⚠️ 5.0以后的版本,需要设置包名才能绑定远程服务
intent.setPackage("liudeli.service2");
bindService(intent, conn, BIND_AUTO_CREATE); startRemoteServiceActivity = true;
} /**
* 服务连接接口
*/
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) { } @Override
public void onServiceDisconnected(ComponentName name) { }
}; @Override
protected void onDestroy() {
super.onDestroy(); if (startRemoteServiceActivity) { // 解绑服务,一定要记得解绑服务,否则会报异常(服务连接资源异常)
unbindService(conn);
}
}
}

注意:⚠️ 只要把Service2应用(服务端)安装到手机

Android-bindService远程服务启动其他应用的Activity的更多相关文章

  1. Android手机app启动的时候第一个Activity必须是MainActivity吗

    原文:Android手机app启动的时候第一个Activity必须是MainActivity吗 Android手机APP启动的第一个Activity是可以自己设置的,不是必须的MainActivity ...

  2. Android 图文教学让你彻底理解activity启动模式

    我们首先从最简单的开始, standard 这个模式就是默认的模式,我们都知道 当你用这个模式时,每次发送一个intent,都会生成一个新的实例! 我写一个简单的例子: <?xml versio ...

  3. Android 通过广播启动另一个应用的Activity

    需求:现在有应用A和应用B,我需要在A应用中启动B应用中的某个Activity 实现:A应用中的Activity发送广播,关键代码如下: String broadcastIntent = " ...

  4. android 21 隐式意图启动系统预定义activity

    Intent intent=new Intent(LoginActivity.this, MainActivity.class);//显示意图启动,显示从一个activity到另一个activity, ...

  5. Android 跨进程启动Activity黑屏(白屏)的三种解决方案

    原文链接:http://www.cnblogs.com/feidu/p/8057012.html 当Android跨进程启动Activity时,过程界面很黑屏(白屏)短暂时间(几百毫秒?).当然从桌面 ...

  6. Android应用程序启动时发生AndroidRuntime : ClassNotFoundException for Activity class的解决方法

    在android应用程序启动时抛出下面异常导致启动失败:07-09 17:12:35.709: ERROR/AndroidRuntime(3866): Uncaught handler: thread ...

  7. Android之后台启动Activity

    在实际开发中.Activity须要启动但界面又不能显示出来,这时就须要后台启动.但又不是finish(),这时就要用到Activity中的moveTaskToBack函数,先看下官网 參数nonRoo ...

  8. Android 面试必备 - 系统、App、Activity 启动过程“一锅端”

    Android 系统启动过程 从系统层看: linux 系统层 Android系统服务层 Zygote 从开机启动到Home Launcher: 启动bootloader (小程序:初始化硬件) 加载 ...

  9. Android随笔之——跨进程通信(一) Activity篇

    在Android应用开发中,我们会碰到跨进程通信的情况,例如:你用QQ通讯录打电话的时候会调用系统的拨号应用.某些新闻客户端可以将新闻分享到QQ.微信等应用,这些都是跨进程通信的情况.简而言之,就是一 ...

随机推荐

  1. linux中sed命令

    sed sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也就是把前一个程序的输出引入sed的输入,经过一系列编辑命令转换为另一种格式输出. ...

  2. apt-get使用国内镜像源

    apt-get 1.复制原文件备份(万一弄坏源文件可恢复) sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak 2.编辑源列表文件 sudo ...

  3. C# Graphics中有关绘图质量的几个Mode

    一.CompositingMode 获取一个值,该值指定如何将合成图像绘制到此 Graphics.复合模式确定从源映像的像素是覆盖(SourceCopy)还是组合(SourceOver, 需要使用半透 ...

  4. C#内存流示例----->用内存流来读取图片

    背景:一个窗体.一个pictureBox.一个lable[没有选择图片,lable的text为"图片未选择"],在pictureBox1的Click事件中添加如下代码: priva ...

  5. 「小程序JAVA实战」小程序导航组件(26)

    转自:https://idig8.com/2018/08/19/xiaochengxujavashizhanxiaochengxudaohangzujian26/ 来说下 ,小程序的导航组件.源码:h ...

  6. Spring 学习记录6 BeanFactory(2)

    主题 除了Spring 学习记录5 BeanFactory 里写的几个接口外,BeanFactory的实现类还实现了一些其他接口,这篇文章主要介绍这些接口和实现类. 结构 DefaultListabl ...

  7. Spring Session 学习记录1

    先写些废话 新公司项目是有用到redis,之前老公司使用的缓存框架是ehcache.我redis并不熟悉.看过介绍以后知道是个nosql..既然是个数据库,那我想操作方法和jdbc操作关系数据库应该差 ...

  8. MySQL批量添加表字段

    ALTER TABLE custom ADD contacts2 VARCHAR(50) NOT NULL DEFAULT '' COMMENT '客户联系人2',ADD phone2 VARCHAR ...

  9. Dubbo限制大数据传输的解决方案

    当service层向web层传输大数据容量的对象时,会受到Dubbo的限制,报类似如下异常: com.alibaba.dubbo.remoting.transport.AbstractCodec.ch ...

  10. Javascript —— 有向图广度优先搜索

    用Javascript实现有向图的广度优先搜索 刚好遇到一个需求,对于一个有向图,指定一个节点 i 作为起点,输出从 i 出发,可以到达的所有节点,也就是图中以 i 作为起点的子连通片,思考了一下,可 ...