将 Android 活动从后台线程置于前台

发布于 2024-12-10 14:27:14 字数 230 浏览 0 评论 0原文

我有一个通过 Intent 启动系统浏览器的活动。在此之前不久,我对其他 URL 执行了 HTTP GET。用户在浏览器中完成任务(使用 OAuth 登录)后,将立即响应此 GET。

我希望能够关闭浏览器和/或让我的应用程序的活动回到前面。

我不想使用 WebView,因为我想避免别人认为我可能试图监视密码。

知道如何解决这个问题吗?有可能吗?

非常感谢!

丹尼尔

I have an activity that starts the system browser via Intent. Short before it does that I do a HTTP GET to some other URL. This GET will be answered as soon as the user finishes his task in the browser (logging in using OAuth).

I'd like to be able to close down the browser and / or get my application's activity back to the front.

I do not want to use a WebView because I'd like to avoid the perception that I might be trying to spy on passwords.

Any idea how to solve this? Is it possible at all?

Thanks a bunch!

Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫雨熙 2024-12-17 14:27:15

确保 OAuth 打开类似于 yourapp://success 的 URL

接下来,添加一个意图过滤器来处理此自定义协议和地址。更多详细信息请参见 http://developer.android.com/guide /topics/intents/intents-filters.html#ifs

Make sure OAuth opens up URL which is something like yourapp://success

Next you add a intent filter to handle this custom protocol and address. More details are at http://developer.android.com/guide/topics/intents/intents-filters.html#ifs

怎樣才叫好 2024-12-17 14:27:15

这就是我的项目中的窍门。

应用程序清单非常标准:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

这是后台线程的代码片段,用于发送重新显示活动的意图:

public class AlarmThread extends Thread {

    private int mSleepTime;

    public AlarmThread(int sleepSeconds) {
        super("AlarmThread");
        mSleepTime = sleepSeconds * 1000;
    }
    @Override
    public void run() {
        Log.i("thread", "started sleeping for " + mSleepTime + " milliseconds");
        try {
            Thread.sleep(mSleepTime);
        } catch (InterruptedException e) {
            // ignored
        }
        Log.i("thread", "creating intent to bring activity to foreground");
        Intent intent = new Intent(MainActivity.getContext(), MainActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MainActivity.getContext().getApplicationContext().startActivity(intent);
    }
}

请注意,技巧在 MainActivity.getContext().getApplicationContext().startActivity(intent) 中; 部分(上面最后一行)。

在 MainActivity 中,我添加了 getContext 方法:

public static Context getContext() {
    return mInstance;
}

并且在“onCreate”中设置了成员“mInstance”:

private static MainActivity mInstance = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Other code....

    mInstance = this;
}

Here's what did the trick in my project.

The application manifest is pretty standard:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

Here's a code snippet of the background thread that sends the intent to re-show the activity:

public class AlarmThread extends Thread {

    private int mSleepTime;

    public AlarmThread(int sleepSeconds) {
        super("AlarmThread");
        mSleepTime = sleepSeconds * 1000;
    }
    @Override
    public void run() {
        Log.i("thread", "started sleeping for " + mSleepTime + " milliseconds");
        try {
            Thread.sleep(mSleepTime);
        } catch (InterruptedException e) {
            // ignored
        }
        Log.i("thread", "creating intent to bring activity to foreground");
        Intent intent = new Intent(MainActivity.getContext(), MainActivity.class);
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MainActivity.getContext().getApplicationContext().startActivity(intent);
    }
}

Note that the trick is in the MainActivity.getContext().getApplicationContext().startActivity(intent); part (last line above).

In the MainActivity, I added the getContext method:

public static Context getContext() {
    return mInstance;
}

And the member 'mInstance' is set in the 'onCreate':

private static MainActivity mInstance = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Other code....

    mInstance = this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文