Log in or register to post comments

Make a Java plugin and call it in Unity

June 6, 2011 - 6:53am #1

Hi guys,
I'm trying to get two java classes working in Unity. I've got them working as a normal android application, so I know that the code is right, but I'm kind of lost to get this working inside my Unity app. The goal of this is to push a notification one minute after the method setNotification is called.

Here's my two classes (working together):

public class PushNotification extends Activity {
	
    protected static final int HELLO_ID = 1;
    
    public PushNotification() {}
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setNotification();
    }
    
    public void setNotification()
    {
    	Calendar cal = Calendar.getInstance();  

	cal.add(Calendar.MINUTE, 1);
	cal.set(Calendar.SECOND, 0);
		
	Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
	alarmintent.putExtra("title","TITLE");
	alarmintent.putExtra("note","NOTE");

        PendingIntent sender = PendingIntent.getBroadcast
		(getApplicationContext(), HELLO_ID,  alarmintent,PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

	AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
	am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    }
    
}
public class AlarmReceiver extends BroadcastReceiver {
 
	private static int NOTIFICATION_ID = 1;
 
	@Override
	public void onReceive(Context context, Intent intent) {
 
	    // NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
		NotificationManager manger = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.icon, "Tamagochi", System.currentTimeMillis());
		PendingIntent contentIntent = PendingIntent.getActivity(context, NOTIFICATION_ID, new Intent(context, AlarmReceiver.class), 0);
		Bundle extras=intent.getExtras();
		String title=extras.getString("title");//here we get the title and description of our Notification
			//
		String note=extras.getString("note");
		notification.setLatestEventInfo(context, note, title, contentIntent);
		notification.flags = Notification.FLAG_ONLY_ALERT_ONCE;
		notification.defaults |= Notification.DEFAULT_SOUND;//here we set the default sound for our //notification
 
		// The PendingIntent to launch our activity if the user selects this notification
		manger.notify(NOTIFICATION_ID++, notification);

	}
 
};

And my Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test.pushnotification"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".PushNotification"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".AlarmReceiver" android:enabled="true">
        </receiver>
    </application>
</manifest>

I tried to export this as a jar and then call it through JNIHelper with AndroidJavaObject but I didn't get any result.
I also tried to extends the QCARActivity but it closes as soon as the app starts.

Any idea?

Re: Make a Java plugin and call it in Unity

June 7, 2011 - 6:55am #2

I would take a close look at how your activities are playing with the main PlayerActivity. The Manifest file goes in the Plugins/Android folder, take a look at the one that is currently there. You can extend our main activity if you would like, but you can't simply replace it. Here's the signature of our main activity:

com.qualcomm.QCARUnityPlayer.PlayerActivity

There is some sample code that may be useful to you in this thread:

http://ar.qualcomm.at/node/2000434

- Kim

Log in or register to post comments