0

I am still a newbie in Android Development and I am about to release my first app to the google play store.

I have a button in my app which is a share button. When the user clicks on it, I want it to share the google play link of my app. But I don't know how to get my google play link of my app.

In the shareBody line I want it to contain the google play link of my app. I got the following code from this website so I don't really understand whats going on :P.

public void ShareClick(View view){
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "https://play.google.com/store";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Download XXXXXX");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
 }

eg :

String shareBody = "https://play.google.com/store";

What I want is :

String shareBody = "google play link of my app";

Can anyone please tell me what should I do?

P.S During the testing my app on my phone, whenever I go to the home menu or when i press the multitasking button and then go back to my app, it crashes

Is there anything I can do to prevent this? Thanks a million!

Emil
  • 2,786
  • 20
  • 24
Karan
  • 201
  • 1
  • 3
  • 13
  • Check this http://stackoverflow.com/questions/11753000/how-to-open-the-google-play-store-directly-from-my-android-application – Emil Sep 28 '15 at 17:59

3 Answers3

1

use your package name instead of link somthing like that
market://details?id=" + context.getPackageName()

Satyavrat
  • 469
  • 1
  • 7
  • 24
0

Check the link Boss posted to help you figure out how to launch the Play store via an Intent.

As for why your app crashes when you press home, or multitasking, it is most likely because you are not handling your Activity Lifecycle properly. I'm assuming what's happening is that your app leaves the foreground, and its state is not being saved (you are not saving certain data in onPause() or onStop(). When your app returns to the foreground, it attempts to access data which is no longer there since you are not retrieving that data from onStart() or onResume(). Post the stacktrace so we can see the error, and read this: http://developer.android.com/reference/android/app/Activity.html

Shahbaz Sheikh
  • 404
  • 1
  • 6
  • 15
  • My app consists of buttons that make funny sounds when clicked. My code contains clicklisteners of the buttons,sharing intent, a method that stops the current sound in the mediaplayer when the other button is clicked (in order to play the sound of that button) and finally this method: @Override protected void onPause() { super.onPause(); mp.release(); } How to improve this code? – Karan Sep 29 '15 at 16:18
  • Post the code. I'll see if I can help. Just a very general guess, I would say something with your MediaPlayer is not being handled correctly. – Shahbaz Sheikh Sep 30 '15 at 03:48
  • I have posted my code below as an answer. I am not sure if my ad banner code and the intent sharing code are correct ? :/ I followed the guideline in google developer site on how to add banners in the app but I have a feeling it might not be correct because I have seen people doing it a bit differently on YouTube tutorials. – Karan Sep 30 '15 at 17:35
  • When you close the app, mp.release() within onPause is called, and the media player is released. When you start the app again, and press a button, the first thing that happens is stopPlaying() is called. The problem, I think, is that in stopPlaying, you are calling mp.stop() on a media player which has already been released. in onPause(), try doing mp.stop; mp.release; and then mp = null. There is room for improvement in the way you implemented this Media Player, but let's see if this works. – Shahbaz Sheikh Sep 30 '15 at 18:15
  • 1
    Wow!!!! it worked perfectly. Thank you so so much! You rock! :D But when I press the share button right after I open the app, it crashes. I don't think it's a big issue. – Karan Oct 02 '15 at 12:32
  • Excellent. There is a better way to write your project, so keep on studying and improving! Please mark the answer as accepted also! – Shahbaz Sheikh Oct 03 '15 at 18:46
0
package com.example.narula.funnysounds;

import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
MediaPlayer mp;

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

    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    android.support.v7.app.ActionBar actionbar=getSupportActionBar();
    actionbar.hide();
}

@Override
protected void onPause() {
    super.onPause();
    mp.release();
}

private void stopPlaying() {
    if (mp != null) {
        mp.stop();
        mp.release();
        mp = null;
    }
}
public void funnyClick1(View view) {
    stopPlaying();
    mp = MediaPlayer.create(this, R.raw.funny1);
    mp.start();
}
public void funnyClick2(View view){
    stopPlaying();
    mp=MediaPlayer.create(this,R.raw.funny2);
    mp.start();
}
public void funnyClick3(View view) {
    stopPlaying();
    mp= MediaPlayer.create(this, R.raw.funny3);
    mp.start();
}
public void funnyClick4(View view) {
    stopPlaying();
    mp = MediaPlayer.create(this, R.raw.funny4);
    mp.start();
}
public void funnyClick5(View view) {
    stopPlaying();
    mp = MediaPlayer.create(this, R.raw.funny5);
    mp.start();
}
public void funnyClick6(View view) {
    stopPlaying();
    mp = MediaPlayer.create(this, R.raw.funny6);
    mp.start();
}
public void funnyClick7(View view) {
    stopPlaying();
    mp = MediaPlayer.create(this, R.raw.funny7);
    mp.start();
}
public void SuperfunnyClick1(View view){
    stopPlaying();
    mp= MediaPlayer.create(this,R.raw.Sfunny1);
    mp.start();
}
public void Superfunny2(View view){
    stopPlaying();
    mp= MediaPlayer.create(this,R.raw.Sfunny2);
    mp.start();
}
public void ShareClick(View view){
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    String shareBody = "market://details?id=com.example.narula.funnysounds";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Download Funny Sounds");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }
}
Karan
  • 201
  • 1
  • 3
  • 13