重定向到浏览器从Android应用 [英] redirecting to Android app from browser

查看:167
本文介绍了重定向到浏览器从Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在present一个Android应用程序通过HTML请求,从服务器请求的信息,作为回报,得到一个PARAMS字符串。这发生在启动过程中,以配置应用程序运行它的第一次。

I am working on a Android app at present that request information from a server via an HTML request and gets a params string in return. This happens during the startup process in order to configure the app the first time it is run.

要请求发送给我打电话,从我的应用程序的主要活动的onCreate方法如下服务器:

To send the request to the server I call the following from the OnCreate method of the main activity of my app:

String URL = "http://myserver.mydomain.com/users/start";            
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
startActivity(browserIntent);

服务器,一个Rails应用程序,获取所需的参数值,并重定向到该应用程序使用以下语句:

The server, a rails app, gets the required parameter values and redirects back to the app with the following statement:

redirect_to "http://myappname.mydomain.com/?#{mobile_params.to_query}"

我在AndroidManifest.xml中的一个处理来自服务器返回的参数活动创建一个意图过滤器。这样做的目的过滤器是这样的:

I have created an intent filter in my AndroidManifest.xml for the activity that handles the parameters that come back from the server. The intent filter looks like:

    <activity
        android:name=".StartFromUriActivity"
        android:launchMode="singleTop"
        android:noHistory="true"
        android:screenOrientation="behind"
        android:theme="@style/NoTitle" 
        android:exported="true" >
        <intent-filter>
            <data android:scheme="http" android:host="myappname.mydomain.com" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
             <category android:name="android.intent.category.BROWSABLE" />
        </intent-filter>
    </activity>

最后,我StartFromUriActivity.java的前几行是这样的:

Finally, the first few lines of my StartFromUriActivity.java look like this:

public class StartFromUriActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Uri URIdata = getIntent().getData();
        if(URIdata != null) {
            parameter1value = URIdata.getQueryParameter("param1");
        }
    finish();
    }
}

这一切实际工作很好,但有一个例外。该应用程序将打开浏览器,发送的URI服务器,重定向回用所需的参数值的应用程序,意图筛选指示的活动和参数都正确读取。唯一的问题是,浏览器窗口在前台走了,我必须手动关闭它。我已经试过所有我能想到的让我的应用程序来前景以及浏览器关闭或者去后台没有成功。我在想什么,会导致我的应用程序正确地被带到前台?

All of this actually works fine, with one exception. The app opens a browser, sends the URI to the server, which redirects back to the app with the desired parameter values, the intent filter directs to the activity and the parameters are read correctly. The only problem is that the browser window is left in the foreground and I have to manually close it. I have tried everything I can think of to get my app to come to the foreground and for the browser to close or go to the background without success. What am I missing that would result in my app correctly being brought to the foreground?

推荐答案

我尝试添加的android dsschnau的建议:allowTaskReparenting =true将在AndroidManifest.xml活动。这并没有解决问题,所以我尝试添加机器人:parentActivityName =MainActivity和android:taskAffinity =的myapp,使重排根目录有一个明确的目标,但仍没有运气

I tried dsschnau's suggestion of adding android:allowTaskReparenting="true" to the activity in AndroidManifest.xml. This didn't solve the problem, so I tried adding android:parentActivityName=".MainActivity" and android:taskAffinity="myapp" so that reparenting had a explicit target, but still no luck.

然后我意识到问题是,在StartFromUriActivity.OnCreate结束()语句结束活动,它是正确返回到下一个活动在堆栈中,浏览器。解决方法是以下行结束()语句之前添加。

I then realized that the problem was that the finish() statement in StartFromUriActivity.OnCreate was ending the activity and it was correctly returning to the next activity in the stack, the browser. Solution was to add the following lines prior to the finish() statement.

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);

这篇关于重定向到浏览器从Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆