-
Notifications
You must be signed in to change notification settings - Fork 111
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing OnNewIntent from a native application? #275
Comments
I think realistically there are quite a number of things that aren't practically possible for applications that are strictly constrained to using At this point E.g. in my situation working on a Bluetooth library where I want to use the Companion API to discover devices then I need to be able to hook in to At least in my similar situation it also highlighted a slightly different challenge which is, "how can we build applications based on
I recently opened this issue when I was starting to figure out how I could build a Rust app based on In your case the minimal change might just be to introduce a custom subclass of |
I saw your issue #266 ago but missed your last update, great work on that! As you pointed its probably inevitable that there needs to be some java code involvement. Though I fear that I cannot just extend |
Heya, I was meaning to reply to this the other day after I had planned to cook up a quick example of subclassing NativeActivity but ended up getting distracted, sorry :) I just had a chance to add an example here though: That particular example is based on another Also sorry for the bad etiquette linking away from It probably would have helped to add a few comments to the code but in java code it looks like this: public class MainActivity extends NativeActivity {
static {
System.loadLibrary("na_subclass_jni");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
notifyOnNewIntent();
}
private native void notifyOnNewIntent();
} With the main fiddly bits being: 1) we have to use (actually I was surprised by the need to call LoadLibrary manually here, I was expecting that to be unnecessary since NativeActivity will be loading the same lib anyway) Then in Rust we can implement that function with something like: #[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Java_co_realfit_nasubclassjni_MainActivity_notifyOnNewIntent(
_env: jni::JNIEnv,
_class: jni::objects::JObject, // This is the JClass, not the instance,
_activity: jni::objects::JObject,
) {
info!("onNewIntent was called!");
} Just keep in mind that the function will be called from the Java main thread, which won't be the same as your native main thread. Btw, the example app is set to have a #[allow(non_snake_case)]
#[no_mangle]
pub extern "C" fn Java_co_realfit_nasubclassjni_MainActivity_notifyOnNewIntent(
env: jni::JNIEnv,
_class: jni::objects::JObject, // This is the JClass, not the instance,
_activity: jni::objects::JObject,
data: jni::objects::JObject, //
) {
let s = env.get_string(jni::objects::JString::from(data)).unwrap();
let s = s.to_str().unwrap();
info!("onNewIntent was called with {s}!");
} Hope that helps point you in the right direction for what you're trying to do. |
Hey awesome work! This looks really promising, I was able to get the code you provided to compile and run on my device (the smallest of victories). Regarding how to fetch the @Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
notifyOnNewIntent();
} Not sure what the That said I feel there is a gap, and you pointed that out as well, between your code and how to use it in conjunction with I tried looking at ways to get it into the |
Cool, glad you got something running. Yeah not, sure if there's any need to chain up with If you were using
You shouldn't need to worry about messing with the NativeActivity implementation as alluded here. At least in the context of running a native Rust app you can't have multiple Activities. The main thing is that you need to choose a build system that can handle compiling the java subclass and you need to specify the subclass in your AndroidManifest.xml. The example above uses Gradle, which is what Android Studio uses and personally I'd recommend just using Gradle for any Android projects (including if you use |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Hello!
My question is about how I can access the OnNewIntent?
Some background; when a native application is launched from an intent you can fetch the intent using getDataString But if the application is set to
singleTop
and if application is already running the intent will have to be fetched usingOnNewIntent
which requires overriding theOnNewIntent
. However that function is only accessible from the base class Activity and native applications extend the class NativeActivity which does not expose that function. So from a native application is there a way to access the new intent?Any help is greatly appreciated 👍
The text was updated successfully, but these errors were encountered: