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>