开发者

Share application "link" in Android

开发者 https://www.devze.com 2023-02-10 11:31 出处:网络
I want my application use开发者_开发知识库r to be able to share/recommend my app to other users. I use the ACTION_SEND intent. I add plain text saying something along the lines of: install this cool a

I want my application use开发者_开发知识库r to be able to share/recommend my app to other users. I use the ACTION_SEND intent. I add plain text saying something along the lines of: install this cool application. But I can't find a way to enable users to directly go to the install screen of market place for instance. All I can provide them with is a web link or some text. In other words I am looking for a very direct way for android users to have my app installed.

Thanks for any help/pointers,

Thomas


This will let you choose from email, whatsapp or whatever.

try { 
    Intent shareIntent = new Intent(Intent.ACTION_SEND);  
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
    String shareMessage= "\nLet me recommend you this application\n\n";
    shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
    shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);  
    startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch(Exception e) { 
    //e.toString();
}   


You can use also ShareCompat class from support library.

ShareCompat.IntentBuilder.from(activity)
    .setType("text/plain")
    .setChooserTitle("Chooser title")
    .setText("http://play.google.com/store/apps/details?id=" + activity.getPackageName())
    .startChooser();

https://developer.android.com/reference/android/support/v4/app/ShareCompat.html


Thomas,

You would want to provide your users with a market:// link which will bring them directly to the details page of your app. The following is from developer.android.com:

Loading an application's Details page

In Android Market, every application has a Details page that provides an overview of the application for users. For example, the page includes a short description of the app and screen shots of it in use, if supplied by the developer, as well as feedback from users and information about the developer. The Details page also includes an "Install" button that lets the user trigger the download/purchase of the application.

If you want to refer the user to a specific application, your application can take the user directly to the application's Details page. To do so, your application sends an ACTION_VIEW Intent that includes a URI and query parameter in this format:

market://details?id=

In this case, the packagename parameter is target application's fully qualified package name, as declared in the package attribute of the manifest element in the application's manifest file. For example:

market://details?id=com.example.android.jetboy

Source: http://developer.android.com/guide/publishing/publishing.html


Call this method:

public static void shareApp(Context context)
{
    final String appPackageName = context.getPackageName();
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out the App at: https://play.google.com/store/apps/details?id=" + appPackageName);
    sendIntent.setType("text/plain");
    context.startActivity(sendIntent);
}


To be more exact

   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.android.example"));
   startActivity(intent);

or if you want to share your other apps from your dev. account you can do something like this

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/developer?id=Your_Publisher_Name"));
startActivity(intent);


To automatically fill in the application name and application id you could use this:

int applicationNameId = context.getApplicationInfo().labelRes;
final String appPackageName = context.getPackageName();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, activity.getString(applicationNameId));
String text = "Install this cool application: ";
String link = "https://play.google.com/store/apps/details?id=" + appPackageName;
i.putExtra(Intent.EXTRA_TEXT, text + " " + link);
startActivity(Intent.createChooser(i, "Share link:"));


Share application with title is you app_name, content is your application link

fun shareApp(context: Context) {
    val appPackageName = BuildConfig.APPLICATION_ID
    val appName = context.getString(R.string.app_name)
    val shareBodyText = "https://play.google.com/store/apps/details?id=$appPackageName"

    val sendIntent = Intent(Intent.ACTION_SEND).apply {
        type = "text/plain"
        putExtra(Intent.EXTRA_TITLE, appName)
        putExtra(Intent.EXTRA_TEXT, shareBodyText)
    }
    context.startActivity(Intent.createChooser(sendIntent, null))
}


I know this question has been answered, but I would like to share an alternate solution:

Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareSubText = "WhatsApp - The Great Chat App";
String shareBodyText = "https://play.google.com/store/apps/details?id=com.whatsapp&hl=en";
shareIntent.putExtra(Intent.EXTRA_SUBJECT, shareSubText);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareBodyText);
startActivity(Intent.createChooser(shareIntent, "Share With"));


finally, this code is worked for me to open the email client from android device. try this snippet.

Intent testIntent = new Intent(Intent.ACTION_VIEW);
                    Uri data = Uri.parse("mailto:?subject=" + "Feedback" + "&body=" + "Write Feedback here....." + "&to=" + "someone@example.com");
                    testIntent.setData(data);
                    startActivity(testIntent);


Kotlin extension for share action. You can share whatever you want e.g. link

fun Context.share(text: String) =
    this.startActivity(Intent().apply {
        action = Intent.ACTION_SEND
        putExtra(Intent.EXTRA_TEXT, text)
        type = "text/plain"
    })

Usage

context.share("Check https://stackoverflow.com")


According to official docs in 2021 preferred way is

fun shareTextToOtherApps(message: String) {
        val sendIntent: Intent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_TEXT, message)
            type = "text/plain"
        }

        val shareIntent = Intent.createChooser(sendIntent, null)
        startActivity(shareIntent)
    }


Actually the best way to sheared you app between users , google (firebase) proved new technology Firebase Dynamic Link Through several lines you can make it this is documentation https://firebase.google.com/docs/dynamic-links/ and the code is

  Uri dynamicLinkUri = dynamicLink.getUri();
      Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
            .setLink(Uri.parse("https://www.google.jo/"))
            .setDynamicLinkDomain("rw4r7.app.goo.gl")
            .buildShortDynamicLink()
            .addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
                @Override
                public void onComplete(@NonNull Task<ShortDynamicLink> task) {
                    if (task.isSuccessful()) {
                        // Short link created
                        Uri shortLink = task.getResult().getShortLink();
                        Uri flowchartLink = task.getResult().getPreviewLink();
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_SEND);
                        intent.putExtra(Intent.EXTRA_TEXT,  shortLink.toString());
                        intent.setType("text/plain");
                        startActivity(intent);
                    } else {
                        // Error
                        // ...
                    }
                }
            });


try { 
Intent shareIntent = new Intent(Intent.ACTION_SEND);  
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Application name");
String shareMessage= "\n Your Message \n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);  
startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch(Exception e) { 
//e.toString();
}   


@Linh answer is almost good but causing a crash because of the missing FLAG_ACTIVITY_NEW_TASK, here is what worked for me

    public static void shareApp(Context context) {
    final String appPackageName = context.getPackageName();
    Intent sendIntent = new Intent();
    sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out the App at: https://play.google.com/store/apps/details?id=" + appPackageName);
    sendIntent.setType("text/plain");
    context.startActivity(sendIntent);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号