carsten-klaffke / send-intent

Repository for send-intent Capacitor plugin
MIT License
106 stars 12 forks source link

Help android: app crash, i just want to start the app #50

Closed mirsella closed 2 years ago

mirsella commented 2 years ago

Hello

My final goal is to execute a certain function in JS depending on how the app was launched, so no sharing from another app or arguments. Sorry, i know it's not very clear, feel free to ask precision. My first try was with android's device control, but didn't find anyway simple way with capacitor. I don't know if this plugin is the right way to do it, but android's intent seems to be a way to do it.

**Smartphone

whatever the app.vue it crash, even without importing the plugin. i tried a lot of configuration my last one was like this

       <activity
              android:name="mirsella.test.power"
              android:label="@string/app_name"
              android:exported="true"
              android:theme="@style/AppTheme.NoActionBar">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <!-- <category android:name="android.intent.category.DEFAULT" /> -->
          </intent-filter>
      </activity>

I never really touched the android apps, so I don't know what I'm doing, I'm probably missing something obvious. I'm launching the activity mirsella.test.power with this app https://play.google.com/store/apps/details?id=de.szalkowski.activitylauncher but after I'll launch the activity from termux

Please enlight me, I'm stuck.

Thanks a lot for any time you put in helping me, have a good day

carsten-klaffke commented 2 years ago

Hey,

if you are looking for something to handle general intents in JS, have a look at Capacitor's bridge.eval() function! In your Activity-class you can dispatch an event (called it "sendIntentReceived" in the example):

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String action = intent.getAction();
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            bridge.getActivity().setIntent(intent);
            bridge.eval("window.dispatchEvent(new Event('sendIntentReceived'))", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {
                }
            });
        }
    }

And then in your JS-code you can catch it with a listener and call your method.

window.addEventListener("sendIntentReceived", () => {
    ...
})

I hope this helps. Otherwise I think your question is probably not related to this plugin and maybe you will find better help on Stackoverflow.

Best regards Carsten

mirsella commented 2 years ago

Hey, thanks a lot for the response, couldn't have done it without you. I've tried and learned a lot of new things. I'm close to it be working.

I was wondering why using bridge.eval instead of triggerJSEvent ? No reasons in particular ?

carsten-klaffke commented 2 years ago

That is great to hear!

In my case, I had problems receiving the event with triggerJSEvent. I think it was because it didn't work reliably with window or document. But that is a good point, triggerJSEvent() is suggested by the Capacitor documentation. So basically triggerJSEvent should work, too.

mirsella commented 2 years ago

hey, when lauching my app i get a attempt to invoke virtual metod on a null object reference : with triggerJSEvent https://paste.evolution-x.org/XfR4NW or with eval : https://paste.evolution-x.org/wwEaU6

i guess 'bridge' is not initialized

package mirsella.powercontrol;

import android.os.Bundle;
import android.webkit.ValueCallback;

import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;

import android.content.Intent;  

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // bridge.triggerJSEvent("testintent", "window",  "{'data': 'test'}");
    bridge.eval("window.dispatchEvent(new Event('testintent'))", new ValueCallback<String>() {
      @Override
      public void onReceiveValue(String s) {
      }
    });
  }

edit : not crashing now by adding some imports