如何在应用程序在后台时接收回调

huangapple go评论68阅读模式
英文:

How to receive callback with app in background

问题

I would like to be able to receive a callback when my app is in the background and then switch back to the application without any manual steps from the user when the callback is received. An activity opens an external browser for the user to perform some action. Then the phone should receive a callback; which it does, but not until the user manually switches back to the application from the webview (an undesired step). Until then, the page seems to get hung up. My questions are...

  1. Do I need to create a websocket? or Notification Handler?
  2. If so, how would I go about that for this case?
  3. Do I need to switch this to an activityforResult and when the result (any result) happens to then switch back to the application?
  4. if so, how would I do that?

Here is what I have in terms of code:
Open the web page where 'exe' is the webpage as a string

Log.i("inititating commands for", exe);
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(exe));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setPackage("com.android.chrome");
try {
    context.startActivity(i);
} catch (Exception e) {
    e.printStackTrace();
}

mainfest.xml intent filters (I'm not certain this is necessary, nor am I sure it changed anything for me)

<intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:host="127.0.0.1:54321/" android:scheme="http"/>
</intent-filter>

I'm experiencing quasi-success. The flow completes, but with extra steps from the user. I would like to remove those manual steps, and make the flow seamless from the user's perspective.

英文:

I would like to be able to receive a callback when my app is in the background and then switch back to the application without any manual steps from the user when the callback is received. An activity opens an external browser for the user to perform some action. Then the phone should receive a callback; which it does, but not until the user manually switches back to the application from the webview (an undesired step). Until then, the page seems to get hung up. My questions are...

  1. Do I need to create a websocket? or Notification Handler?
  2. If so, how would I go about that for this case?
  3. Do I need to switch this to an activityforResult and when the result (any result) happens to then switch back to the application?
  4. if so, how would I do that?

Here is what I have in terms of code:
Open the web page where 'exe' is the webpage as a string

Log.i(&quot;inititating commands for&quot;, exe);
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(exe));
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setPackage(&quot;com.android.chrome&quot;);
                try {
                    context.startActivity(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }

mainfest.xml intent filters (i'm not certain this is neccessary, nor am I sure it changed anything for me)

&lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot;/&gt;
                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;/&gt;
                           &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
                           &lt;category android:name=&quot;android.intent.category.DEFAULT&quot;/&gt;
                           &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
                           &lt;data android:host=&quot;127.0.0.1:54321/&quot; android:scheme=&quot;http&quot;/&gt;
            &lt;/intent-filter&gt;

I'm experiencing quasi-success. The flow completes, but with extra steps from the user. I would like to remove those manual steps, and make the flow seamless from the user's perspective.

答案1

得分: 1

以下是您要翻译的内容:

在意图过滤器上定义您的自定义模式

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="your_custom_scheme" />
</intent-filter>

定义回调URL

String callbackUrl = "your_custom_scheme://callback";

将此传递给您的意图如下

intent.putExtra("android.intent.extra.REFERRER", Uri.parse(callbackUrl));

当用户在外部浏览器中执行所需操作并触发回调时,浏览器应尝试打开回调URL。这应该将您的应用程序带回前台。

在您的活动中,覆盖onNewIntent方法以在您的应用程序已在运行时处理回调

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Uri data = intent.getData();
    if (data != null && data.getScheme().equals("your_custom_scheme")) {
        // 在此处处理回调
    }
}
英文:

define your custom schema on intent filter

    &lt;intent-filter&gt;
    &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
    &lt;category android:name=&quot;android.intent.category.DEFAULT&quot;/&gt;
    &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
    &lt;data android:scheme=&quot;your_custom_scheme&quot; /&gt;
&lt;/intent-filter&gt;

define a callback url

String callbackUrl = &quot;your_custom_scheme://callback&quot;;

pass this to your intent as below

intent.putExtra(&quot;android.intent.extra.REFERRER&quot;, Uri.parse(callbackUrl));

When the user performs the desired action in the external browser and triggers the callback, the browser should attempt to open the callback URL. This should bring your app back to the foreground.
In your activity, override the onNewIntent method to handle the callback when your app is already running

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Uri data = intent.getData();
    if (data != null &amp;&amp; data.getScheme().equals(&quot;your_custom_scheme&quot;)) {
        // Handle the callback here
    }
}

huangapple
  • 本文由 发表于 2023年5月14日 00:47:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243893.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定