Hyperlink that when clicked open a certain application and if it is not installed, open its link in Google Play?

2

The application in this case is the Telegram and it works with a simple system of reference to users, channels, groups.

I would like that by clicking on the hyperlink (clickable textView) that is with the @Fulano user link, it would open the Telegram and direct it to the user in question.

If the Telegram was not installed, the corresponding link on Google Play was opened, so the user could install it.

I've seen this kind of thing happen in other applications, even in YouTube comments and in my smatphone's own browser. The option to "open with" appears.

----------------------------------------------- -------------------------------------------------- ------

Edit:

I tried this way, with the code suggested by the ramaral, this time had no running errors, but with the test device that owns the telegram, did not open the same but the camera .

Activity:

package genesysgeneration.hl;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tvLink03;

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

        tvLink03=(TextView)findViewById(R.id.tvLink03);
        PackageLinkMovementMethod.makerHyperlink(tvLink03);
        tvLink03.setMovementMethod(new PackageLinkMovementMethod("org.telegram.messenger"));

    }

}

Inherited class (PackageLinkMovementMethod):

package genesysgeneration.hl;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.MotionEvent;
import android.widget.TextView;

public class PackageLinkMovementMethod extends LinkMovementMethod{

    private String packageName;

    public PackageLinkMovementMethod(String packageName){

    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event){

        if (event.getAction()==MotionEvent.ACTION_UP){

            Context context = widget.getContext();
            Intent laucnkIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);

            if (laucnkIntent==null){

                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("market://details?id="+ packageName));
                context.startActivity(intent);

            }else {

                context.startActivity(laucnkIntent);

            }

            return true;

        }

        return super.onTouchEvent(widget, buffer, event);

    }

    public static void makerHyperlink(TextView textView){

        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(textView.getText());
        ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(ssb, TextView.BufferType.SPANNABLE);

    }

}
    
asked by anonymous 16.03.2017 / 23:24

1 answer

2

In xml, declare TextView as usual:

<TextView
    android:id="@+id/link"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Abrir Telegram"/>

Write a class inherited from LinkMovementMethod that implements the desired behavior.

LinkMovementMethod.java

public class PackageLinkMovementMethod extends LinkMovementMethod {

    private String packageName;

    public PackageLinkMovementMethod(String packageName){

        this.packageName = packageName;
    }

    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP){

            Context context = widget.getContext();
            Intent launchIntent = context.getPackageManager()
                                         .getLaunchIntentForPackage(packageName);

            if (launchIntent == null){
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("market://details?id="+ packageName));
                context.startActivity(intent);
            }else {
                context.startActivity(launchIntent);
            }
            return true;
        }
        return super.onTouchEvent(widget, buffer, event);
    }

    public static void makeHyperlink(TextView textView) {
        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(textView.getText());
        ssb.setSpan(new URLSpan("#"), 0, ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(ssb, TextView.BufferType.SPANNABLE);
    }
}

In java use this:

    link = (TextView)findViewById(R.id.link);
    PackageLinkMovementMethod.makeHyperlink(link);
    link.setMovementMethod(new PackageLinkMovementMethod("org.telegram.messenger"));

---------------------------------

Initial response:

Once you've tried the application, do so:

PackageManager packageManager = getPackageManager();
List activities = packageManager
                   .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if(activities.size() > 0){
    startActivity(intent);
else{
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("market://details?id=org.telegram.messenger"));
    startActivity(intent);
}

queryIntentActivities() returns the list of applications that can handle the intent. If there is 1 or more, the intent is cast with startActivity(intent) . If not, an intent is created for the org.telegram.messenger application page.

Edit:

I saw that you are getting the intent through getLaunchIntentForPackage() , so you should do this:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("org.telegram.messenger");

if (launchIntent == null){

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("market://details?id=org.telegram.messenger"));
    startActivity(intent);

}else {

    startActivity(launchIntent);

}
    
16.03.2017 / 23:37