Hyperlink that when clicked open a particular application and if it is not installed, open the link of the same in Google Play?

Asked

Viewed 99 times

2

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

I would like to click on hyperlink (clickable textView) which has the user link @Fulano, which opens Telegram and already directs it to the user in question.

If Telegram was not installed, the corresponding link on Google Play https://play.google.com/store/apps/details?id=org.telegram.messenger&hl=pt was opened, so the user could install it.

I’ve seen this kind of thing happen in other apps, even in Youtube comments and in my smatphone’s own browser. The option to "open with".

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

Edit:

I tried so, with the code suggested by the extension, this time there were no errors of execution, but with the testing device that has 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);

    }

}

1 answer

2

In xml, declare Textview the usual way:

<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 like this:

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

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

Initial response:

After you have the Input for 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.
If there is 1 or more, the Intent is launched with startActivity(intent).
If not, an input is created for the application page org.telegrammessenger. in the market.

Edit:

I see you’re getting the information through getLaunchIntentForPackage(), then you must do so:

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

}
  • need to put android:linksClickable in textview?

  • You have to implement an onClickListener for Textview.

  • can do this using the setMovementMethod(LinkMovementMethod.getInstance())?

  • added a Intent I named launchIntent with Telegram’s Internet, but clicking on the textView gives error and the app stops. I made some mistake, I should have posted the code earlier

  • the Launch Intent code hasn’t worked, I’ll try it with the inherited class you said

  • All examples have been tested and worked. When not "funnel" you should report the error that occurs.

  • in the inherited class only thing it accused was in public Static void makerHyperlink => "Inner classes cannot have Static declarations"

  • Write the class in a separate java file.

  • I went to test on my device (in it I have Telegram installed) and opened the camera!?!?!!

  • How can you open the camera if you’re like this: .setMovementMethod(new PackageLinkMovementMethod("org.telegram.messenger"));?

  • nn do the least ideiai. if you want put the code.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.