Android-bindService远程服务启动其他应用的Activity

出门莫恨无人随,书中车马多如簇。这篇文章主要讲述Android-bindService远程服务启动其他应用的Activity相关的知识,希望能为你提供帮助。
【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>

Android-bindService远程服务启动其他应用的Activity

文章图片

 
 
----------------------- 下面的代码是 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 ServiceConnectionconn = 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应用(服务端)安装到手机

    推荐阅读