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.微信等应用,这些都是跨进程通信的情况.简而言之,就是一 ...
随机推荐
- Java-从堆栈常量池解析equals()与==
一.基本概念 ①JAVA中的基本数据类型(简单类型,内置类型): 字节型(byte),短整型(short),整型(int),长整型(long),字符型(char),浮点型(float),双精度型(do ...
- 【转】RocketMQ事务消费和顺序消费详解
RocketMQ事务消费和顺序消费详解 转载说明:该文章纯转载,若有侵权或给原作者造成不便望告知,仅供学习参考. 一.RocketMq有3中消息类型 1.普通消费 2. 顺序消费 3.事务消费 顺序消 ...
- Dubbo限制大数据传输的解决方案
当service层向web层传输大数据容量的对象时,会受到Dubbo的限制,报类似如下异常: com.alibaba.dubbo.remoting.transport.AbstractCodec.ch ...
- python+Django创建第一个项目
1.首先搭建好环境 1.1 安装pyhton,Linux系统中,python是系统自带的所以就不用安装 1.2 安装Django框架 使用pip安装: pip install django 1.3 检 ...
- bugfree调试
最近项目用到bugfree ,一直不能用,原来是session目录的文件权限有问题.
- ssh框架,工具类调用service层方法
解决方法: @Component//声明为spring组件 public class CopyFileUtil{ @Autowired private DataFileManager dataFile ...
- 简单HttpClientUtils工具类
package com.zy.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import o ...
- Jenkins + Jmeter +Ant自动化集成环境搭建(一)
所需工具 一.jmeter 工具下载 https://jmeter.apache.org/ 配置环境JDK等及各种插件可以看小七之前的教程 二.Ant安装(http://ant.apache.org ...
- python解析pcap文件中的http数据包
使用scapy.scapy_http就可以方便的对pcap包中的http数据包进行解析 scapy_http可以在https://github.com/invernizzi/scapy-http下载, ...
- XPath在python中的高级应用
XPath在python的爬虫学习中,起着举足轻重的地位,对比正则表达式 re两者可以完成同样的工作,实现的功能也差不多,但XPath明显比re具有优势,在网页分析上使re退居二线. XPath介绍: ...