Xamarin Android:从另一个应用程序获取一个应用程序的状态信息 [英] Xamarin Android: get one app's state information from another

查看:284
本文介绍了Xamarin Android:从另一个应用程序获取一个应用程序的状态信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Xamarin Android应用程序-我们称它们为"Archy"和"Mehitabel".

I have two Xamarin Android apps -- let's call them "Archy" and "Mehitabel".

Archy具有一些持久状态信息(为了便于说明,它位于SQLite DB中).

Archy has some persistent state information (which, let's say for the sake of argument, is in a SQLite DB).

如果Mehitabel发生某件事,她需要知道一些状态信息.

If a certain thing happens to Mehitabel, she needs to know a piece of that state information.

为了完成这一壮举,我让Mehitabel向Archy发送了一份意向书.阿奇有一个广播接收器,可以听到它,收集必要的状态,然后向梅希塔贝尔发出不同的意图.

To accomplish this feat, I have Mehitabel send an intent to Archy. Archy has a broadcast receiver that hears it, gathers the necessary state, and fires a different intent back to Mehitabel.

这是来自Archy的代码:

Here's the code from Archy:

[BroadcastReceiver(Enabled = true)]
[IntentFilter(new [] { "com.example.Archy.SendStateToMehitabel"})]
public class StateQueryReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        var msg = new Intent("com.example.Mehitabel.StateFromArchy");
        msg.PutExtra("ImportantStateInfo", GetSomeState());
        context.SendBroadcast(msg);
    }
}

这是Mehitabel的代码:

And here's the code from Mehitabel:

    private async Task AskArchyForState()
    {
        var filter = new IntentFilter("com.example.Mehitabel.StateFromArchy");
        var csrc = new TaskCompletionSource<bool>();
        var rcvr = new ActionBroadcastReceiver((context, intent) =>
        {
            State = intent.GetStringExtra("ImportantStateInfo");
            csrc.TrySetResult(State != null);
        });
        RegisterReceiver(rcvr, filter);

        var msg = new Intent("com.example.Archy.SendStateToMehitabel");
        SendBroadcast(msg);

        var task = await Task.WhenAny(csrc.Task, Task.Delay(Timeout));

        UnregisterReceiver(rcvr);
        if (task != csrc.Task)
            bomb("Archy has not answered state query after {0}ms", Timeout);
        if (!csrc.Task.IsCompletedSuccessfully || csrc.Task.Result == false)
            bomb("failed to get all necessary state from Archy");
    }

如果Archy实际上正在运行(即显示在最近"列表中),那么一切都很好.如果Archy没有运行,则Archy的接收者代码将永远不会执行,而Mehitabel也会超时.

That all works great, provided Archy is actually running (i.e., shown in the "recent" list). If Archy isn't running, Archy's receiver code is never executed and Mehitabel times out.

我希望我缺少一些简单的东西(例如,接收者属性之一中的标志,或者com.example.Archy.SendStateToMehitabel意向中的一些秘密调味品.)

My hope is I'm missing something simple (like a flag in one of the receiver attributes, or some secret sauce in the com.example.Archy.SendStateToMehitabel intent).

你能告诉我我在这里想念什么吗?

Can you tell me what I'm missing here?

我是否需要使用一种完全不同的方法(例如在Archy中进行Mehitabel StartActivityForResult()活动,或使用在启动时一直运行并一直运行的服务)?

Do I need to be using a completely different approach (like having Mehitabel StartActivityForResult() an activity within Archy, or using a service that starts on boot and runs all the time)?

推荐答案

根据我的研究,我认为您可以在需要Mehitabel中的数据之前打开Archy.这是有关使用代码打开应用程序的演示.

Based on my research, I think you could open Archy before you need the data in Mehitabel. This is demo about opening an app in code.

 Intent launchIntent = PackageManager.GetLaunchIntentForPackage("NewTestApp.NewTestApp");
            if (launchIntent != null)
            {
                StartActivity(launchIntent);

            }

注意:NewTestApp.NewTestApp是Archy的程序包名称.

Note:NewTestApp.NewTestApp is package name of Archy.

这篇关于Xamarin Android:从另一个应用程序获取一个应用程序的状态信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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