0

I am trying to start a service inside a Unity3d Android plugin but can not make it work. Is working on Android Studio test app, but the service call fail inside unity.

My service class

    public class ProximityService extends Service {
    private String TAG = "iProximity: ";

    NotificationManager _NotificationManager;
    private Context _Context;
    private static Timer _Timer = new Timer();

    public ProximityService() {
    }

    private void sendNotification() {

        Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder builder;
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, iRealUnityPlayerActivity.class), 0);

        builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ireal)
                .setContentTitle("Notification")
                .setContentText("message")
                .setTicker("msg: mensaje")
                .setSound(sound)
                .setAutoCancel(true);

        builder.setContentIntent(contentIntent);
        _NotificationManager.notify(0, builder.build());

    }

    @Override
    public void onCreate() {
        Log.i(TAG, "onCreate()");
        super.onCreate();

        _Context = getApplicationContext();
        _NotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    }

    // methods
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommanf()");

        //startProximity();
        sendNotification();

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "onBind()");
        // TODO: Return the communication channel to the service.
        return null;
    }

}

Java class that is used to start the service:

public final class StatusCheckStarter {

    static Context myContext;
    // Called From C# to get the Context Instance
    public static void ReceiveContextInstance(Context tempContext) {
        myContext = tempContext;
    }
    public static String StartProximityService()
    {
        String result = "OK";

        try
        {
        myContext.startService(new Intent(myContext, ProximityService.class));
        }
        catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage();
        }

        return result;

    }

    public static String Dummy() {
        return "DONEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
    }

}

Unity C# code that is used to call the java functions to start the service:

    AndroidJavaClass unityClass;
    AndroidJavaObject unityActivity;
    AndroidJavaObject unityContext;
    AndroidJavaClass customClass;

    string a1 = "";
    string a2 = "";
    string a3 = "";
    string a4 = "";

    void Start () {

        //Replace with your full package name
        sendActivityReference("info.ireal.proximitylib.StatusCheckStarter");

        //Now, start service
        startService();

        Debug.Log ("START");
    }

    void sendActivityReference(string packageName)
    {
        unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
        unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");

        customClass = new AndroidJavaClass(packageName);
        customClass.CallStatic("ReceiveContextInstance", unityContext);
    }

    void startService()
    {
        a4 = customClass.CallStatic<string> ("Dummy"); 
        a3 = customClass.CallStatic<string>("StartProximityService");
    }

The Dummy method is working and return a string value, but the service does not work

Adb logcat message:

Unable to start service Intent { cmp=info.ireal.proximitytest/info.ireal.proximitylib.ProximityService VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} } U=0: not found

I really appreciate any help Best regards

Mariano

I am using the solution from this thread but still cant make it work.

EDIT

My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.unity3d.player"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">
    <supports-screens
        android:smallScreens="true"
        android:normalScreens="true"
        android:largeScreens="true"
        android:xlargeScreens="true"
        android:anyDensity="true"/>

    <application
        android:theme="@style/UnityThemeSelector"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:debuggable="true">
        <activity android:name="com.unity3d.player.UnityPlayerActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
        </activity>

        <service android:name="info.ireal.proximitylib.ProximityService" />
        <!---<service android:name="ProximityService" />-->

    </application>

</manifest>
Community
  • 1
  • 1
on4ir4m
  • 1
  • 2

1 Answers1

0

This could be the Manifest. Make sure to include the service class in the service tag. It could be any of these problems below. Build, Run and test each one you try.

1.Add the class to the Manifest.

<application
    <service android:name=".ProximityService" />
</application>

2.Add the class with the full path to the Manifest. If this does not work, use the full service class package name

<application
    <service android:name="info.ireal.proximitylib.ProximityService" />
</application>

3.Add the class to the Manifest from Unity itself.

Again, if that fails, go to <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk

Copy the AndroidManifest.xml from that place file to your Assets\Plugins\Android directory then include the service tag code above in the Manifest. Save and run.

4.Finally, use Activity instead of Context.

Changes on the Java side:

A.Rename static Context myContext; to static Activity myActivity;

B.Change

public static void ReceiveContextInstance(Context tempContext) {
    myContext = tempContext;
} 

to

public static void ReceiveContextInstance(Activity tempActivity) {
    myActivity = tempActivity;
}

Changes on C# side:

C.Replace customClass.CallStatic("ReceiveContextInstance", unityContext); with customClass.CallStatic("ReceiveContextInstance", unityActivity);

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hi Programmer, thank you very much for your answer, i follow your tips and still get same error, i will post my AndroidManifest.xml , Im still noob in Android programming, that could be related to plugin dependency? – on4ir4m Aug 31 '16 at 14:11
  • The library proximitylib show dependenciy on com.android.support:appcompat-v7:24.0.0, should i include this on Plugin directory? – on4ir4m Aug 31 '16 at 14:16
  • No you shouldn't. I don't think that is the problem. I asked you a question under your question. Check that question and answer it here. – Programmer Aug 31 '16 at 14:17
  • i change the java dummy method to return myActivity.toString() and the value is not null : com.unity3d.player.UnityPlayerActivity@356..... – on4ir4m Aug 31 '16 at 15:40
  • OK, seems that Im moving a little forward now, i replace the .jar with the .aar output since Unity 5 support aar libraries and the intent error gone, now have error inside the service guess that is on the notification routine cause when I comment the piece of code the error gone. Im testing all on Unity 5.3.4 , and an Android 6.0.1 device. Library build on Android Studio 2.1.2 – on4ir4m Aug 31 '16 at 22:02
  • Android 6.0.1 is different from other Android version I have worked with. The important thing that changed is the permission. Permission is now done from code. It looks like your starting service problem has been solved – Programmer Aug 31 '16 at 22:18