Xamarin Android: get one app's state information from another

时间秒杀一切 提交于 2019-12-25 03:22:59

问题


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

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

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

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.

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);
    }
}

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");
    }

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.

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?

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)?


回答1:


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);

            }

Note:NewTestApp.NewTestApp is package name of Archy.



来源:https://stackoverflow.com/questions/53841667/xamarin-android-get-one-apps-state-information-from-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!