简体   繁体   中英

Android Java, Can call startForegroundService in MainActivity but not in another class/activity

i need your help to understand my error, i have a java script where i call a startForegroundService but nothing work if i dont do this in the Main activity

When a try use startForegroundService in my MainActivity everything is good, but for my projet i cant use this activity.

This code works

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent serviceIntent = new Intent(this, ForegroundService.class);
        startForegroundService(serviceIntent);
    }
}

This code dont works.

package com.exemple.test;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;

public class Bridge {
    public static Activity unityActivity;
    public static void receiveUnityActivity(Activity tActivity){
        unityActivity = tActivity;

    }

    public static void Start(){
        unityActivity.startForegroundService(new Intent(unityActivity, ForegroundService.class));

    }

I call the start method with a c# script from unity. the c# script is good, i have tried call another method and all works if i dont call a service.

I have check my manifest file and all service and permission is good.

So, where am I wrong?

Edit: I call receiveUnityService with a c# script from Unity3D

c#

AndroidJavaClass unityClass;
AndroidJavaObject unityActivity;
AndroidJavaClass customClass;

void Start()
{
    sendActivityReference("com.example.test.Bridge");

   startService();
}

void sendActivityReference(string packageName)
{
    unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
    customClass = new AndroidJavaClass(packageName);
    customClass.CallStatic("receiveUnityActivity", unityActivity);
}

void startService()
{
    customClass.CallStatic("Start");
}

i finally find the soluce. When you create a plugin for unity, you need two manifest file with the same permission. So you need one for unity and another one in the plugin when you build them.

But for now, i dont really understant why the unity manifest file need to be duplicate like this.

thanks for your help:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM