1. 程式人生 > >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>

 

----------------------- 下面的程式碼是 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);
        }
    }
}

 

結果圖: