Android-bindService远程服务启动其他应用的Activity
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的更多相关文章
- Android手机app启动的时候第一个Activity必须是MainActivity吗
原文:Android手机app启动的时候第一个Activity必须是MainActivity吗 Android手机APP启动的第一个Activity是可以自己设置的,不是必须的MainActivity ...
- Android 图文教学让你彻底理解activity启动模式
我们首先从最简单的开始, standard 这个模式就是默认的模式,我们都知道 当你用这个模式时,每次发送一个intent,都会生成一个新的实例! 我写一个简单的例子: <?xml versio ...
- Android 通过广播启动另一个应用的Activity
需求:现在有应用A和应用B,我需要在A应用中启动B应用中的某个Activity 实现:A应用中的Activity发送广播,关键代码如下: String broadcastIntent = " ...
- android 21 隐式意图启动系统预定义activity
Intent intent=new Intent(LoginActivity.this, MainActivity.class);//显示意图启动,显示从一个activity到另一个activity, ...
- Android 跨进程启动Activity黑屏(白屏)的三种解决方案
原文链接:http://www.cnblogs.com/feidu/p/8057012.html 当Android跨进程启动Activity时,过程界面很黑屏(白屏)短暂时间(几百毫秒?).当然从桌面 ...
- Android应用程序启动时发生AndroidRuntime : ClassNotFoundException for Activity class的解决方法
在android应用程序启动时抛出下面异常导致启动失败:07-09 17:12:35.709: ERROR/AndroidRuntime(3866): Uncaught handler: thread ...
- Android之后台启动Activity
在实际开发中.Activity须要启动但界面又不能显示出来,这时就须要后台启动.但又不是finish(),这时就要用到Activity中的moveTaskToBack函数,先看下官网 參数nonRoo ...
- Android 面试必备 - 系统、App、Activity 启动过程“一锅端”
Android 系统启动过程 从系统层看: linux 系统层 Android系统服务层 Zygote 从开机启动到Home Launcher: 启动bootloader (小程序:初始化硬件) 加载 ...
- Android随笔之——跨进程通信(一) Activity篇
在Android应用开发中,我们会碰到跨进程通信的情况,例如:你用QQ通讯录打电话的时候会调用系统的拨号应用.某些新闻客户端可以将新闻分享到QQ.微信等应用,这些都是跨进程通信的情况.简而言之,就是一 ...
随机推荐
- Tkinter简易教程
支持python的常见GUI工具包: Tkinter 使用Tk平台 很容易得到 半标准 wxpython 基于wxWindows.跨平台越来越流行 Python Win 只能在Windows上使用 使 ...
- sublime中开启表格插入
1. 导入包 tableedit 2. ctrl+shift +p 输入teecv,然后开启后面连个东西: 3 按tab可以插入表格
- 一分钟学会 ConstraintLayout 之从属性角度理解布局
ConstraintLayout 在 Android 开发中,我们通常是手写布局,很少会用拖动来写布局,虽然 ConstraintLayout 在 I/O 上以拖动来展现了各种功能,我估计在以后开发中 ...
- python学习——练习题(8)
""" 题目:输出 9*9 乘法口诀表. """ def answer1(): """ 自己用最普通的双重循环 ...
- Spring Boot启动流程详解
注:本文转自http://zhaox.github.io/java/2016/03/22/spring-boot-start-flow 环境 本文基于Spring Boot版本1.3.3, 使用了sp ...
- spring cloud zuul 配置(Robbin 和 熔断)
#启动负载均衡的重试机制,默认falsespring.cloud.loadbalancer.retry.enabled=true#Hystrix是否启用超时时间hystrix.command.defa ...
- Spring IOC基础
2.1.1 IOC是什么IOC—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,IOC意味着将你设计好的对象交给容器控制,而不是传统的在你的 ...
- H5 css学习
p{text-indent:2em;}段前空两格 段落排版--行间距(行高) p{line-height:1.5em;} 段落排版--中文字间距.字母间距 h1{ letter-spaci ...
- linux系统中的命令替换与整数运算$(),$(())
一.$()与`` 在 bash shell 中,$( ) 与 ` ` (反引号) 都是用来做命令替换(command substitution)用的. 所谓的命令替换与我们第五章学过的变量替换差不多, ...
- spring4-2-bean配置-9-通过工厂方法配置bean
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjwAAAFICAIAAADbNrOHAAAgAElEQVR4nO2dy7W0uhGFOxWn4JEXAS