【问题标题】:Sharing Intent link of my app我的应用程序的共享意图链接
【发布时间】:2015-10-01 01:24:58
【问题描述】:

我还是 Android 开发的新手,我即将在 google play 商店发布我的第一个应用程序。

我的应用中有一个按钮,它是一个分享按钮。当用户点击它时,我希望它分享我的应用程序的 google play 链接。但我不知道如何获取我的应用的 google play 链接。

shareBody 行中,我希望它包含我的应用程序的谷歌播放链接。我从这个网站得到了以下代码,所以我不太明白发生了什么: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"));
 }

例如:

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

我想要的是:

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

谁能告诉我该怎么办?

P.S 在我的手机上测试我的应用程序期间,每当我进入主菜单或按下多任务按钮然后返回我的应用程序时,它就会崩溃

我能做些什么来防止这种情况发生吗?谢谢一百万!

【问题讨论】:

标签: android android-intent


【解决方案1】:

使用你的包名而不是类似的链接
market://details?id=" + context.getPackageName()

【讨论】:

    【解决方案2】:

    查看 Boss 发布的链接,以帮助您了解如何通过 Intent 启动 Play 商店。

    至于为什么你的应用程序在你按下 home 或多任务时崩溃,很可能是因为你没有正确处理你的 Activity 生命周期。我假设正在发生的事情是您的应用程序离开了前台,并且它的状态没有被保存(您没有在 onPause() 或 onStop() 中保存某些数据。当您的应用程序返回前台时,它会尝试访问数据它不再存在,因为您没有从 onStart() 或 onResume() 检索该数据。发布堆栈跟踪,以便我们可以看到错误,并阅读以下内容: http://developer.android.com/reference/android/app/Activity.html

    【讨论】:

    • 我的应用程序包含的按钮在点击时会发出有趣的声音。我的代码包含按钮的点击侦听器、共享意图、在单击另一个按钮时停止媒体播放器中当前声音的方法(以便播放该按钮的声音),最后是这个方法:@Override protected void onPause() { super.onPause(); mp.release(); } 如何改进这段代码?
    • 发布代码。我看看能不能帮上忙。只是一个非常笼统的猜测,我会说您的 MediaPlayer 处理不正确。
    • 我在下面发布了我的代码作为答案。我不确定我的广告横幅代码和意图共享代码是否正确? :/ 我遵循了谷歌开发者网站上关于如何在应用程序中添加横幅的指南,但我觉得这可能不正确,因为我看到人们在 YouTube 教程中的做法有所不同。
    • 当您关闭应用程序时,会调用 onPause 中的 mp.release(),并释放媒体播放器。当您再次启动应用程序并按下按钮时,首先会调用 stopPlaying()。我认为问题在于,在 stopPlaying 中,您在已经发布的媒体播放器上调用 mp.stop()。在 onPause() 中,尝试做 mp.stop; mp.release;然后 mp = null。您实现此媒体播放器的方式还有改进的余地,但让我们看看这是否可行。
    • 哇!!!!它工作得很好。非常感谢你!你摇滚! :D 但是当我在打开应用程序后立即按下共享按钮时,它会崩溃。我不认为这是一个大问题。
    【解决方案3】:
    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"));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多