Rafa Vieira
Rafa Vieira

Reputation: 161

Send data from one xamarin.android app to another xamarin.forms app

is there any way to send data from one xamarin.forms app to another xamarin.forms app. Android application the goal is only for android, while the other application is android and ios. The android application has to send data locally to android application / ios but the android application is receiving it. in my aplication android i cant open another app and send data. but i cant receveing data in my aplication. this is the code in android aplication:

 Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setComponent(new ComponentName("com.companyname.Opiniao","android.intent.action.B"));
        intent.putExtra(Intent.EXTRA_TEXT, editTextEmail.getText() + "\n " + editTextPassword.getText());
        intent.setType("text/plain");
        startActivity(intent);

Upvotes: 2

Views: 1445

Answers (1)

Rafa Vieira
Rafa Vieira

Reputation: 161

I've already been able to solve the problem.

In my Android Aplication, i use this code:

    void enviar(){
    // Usa o nome do package que nos queremos encontrar
    boolean isAppInstalled = appInstalledOrNot("com.companyname.Opiniao");

    if(isAppInstalled) {
       Intent intent = new Intent(Intent.ACTION_MAIN);
       intent.setComponent(new ComponentName("com.companyname.Opiniao","android.intent.action.B"));
        intent.putExtra("Dados", editTextEmail.getText() + "\n" + editTextPassword.getText());
        startActivity(intent);


    } else {
        String url = "http://www.google.com";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
}

//Procura o package, se tiver instalado, envia um true, se não encontrar devolve um false
private boolean appInstalledOrNot(String uri) {
    PackageManager pm = getPackageManager();
    try {
        pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

in my xamarin.forms, in android mainActivity, i put this:

        async void Receber()
    {
        text = Intent.GetStringExtra("Dados");
        string dados = "Dados";
        bool result;
        result = await dados.WriteTextAllAsync(text);
     }

Upvotes: 4

Related Questions