2013-05-11 48 views
0

我想要一个只有一个按钮的应用程序。当你按下它播放声音,当你长按它,它给你选择分享。该应用程序工作没有任何错误..但共享选项只是不会打开。长按按钮时,按钮不起作用。实施长按分享按钮在android

package com.example.buttonclicksound; 

    import com.example.buttonclicksound.MainActivity; 

    import android.graphics.drawable.AnimationDrawable; 
    import android.media.MediaPlayer; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.util.Log; 
    import android.view.Menu; 
    import android.view.View; 
    import android.view.View.OnLongClickListener; 
    import android.view.Window; 
    import android.view.WindowManager; 
    import android.widget.Button; 
    import android.widget.ImageView; 

public class MainActivity extends Activity { 
protected static final String TAG = "MainActivity"; 
Button button1; 
MediaPlayer mPlayer; 
AnimationDrawable lightsAnimation; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //No title bar is set for the activity 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    //Full screen is set for the Window 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 
    ImageView lights = (ImageView) findViewById(R.id.imageView1); 
    lightsAnimation = (AnimationDrawable) lights.getDrawable(); 

    button1 = (Button) findViewById(R.id.button1); 
    mPlayer = MediaPlayer.create(MainActivity.this, R.raw.splash); 
    button1.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      try { 

      mPlayer.start(); 
      mPlayer.setLooping(false); 

      } catch (Exception e) { 
       Log.e("ButtonListenerActivity", "error: " + e.getMessage(), 
         e); 
      } 


     } 





    }); 
    button1.setOnLongClickListener(new OnLongClickListener() { 
      public boolean onLongClick(View v) { 

       shareIt(); 
       return true; 
      } 

      private void shareIt() { 
       //sharing implementation here 
       Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
       sharingIntent.setType("audio/mPlayer"); 
       } 
     }); 



} 

@Override 
public void onWindowFocusChanged(boolean hasFocus) { 

    super.onWindowFocusChanged(hasFocus); 

    lightsAnimation.start(); 

} 

protected void onDestroy() { 
    super.onDestroy(); 
    // TODO Auto-generated method stub 
    if (mPlayer != null) { 
     mPlayer.release(); 
     mPlayer = null; 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

} 

它的极端令人沮丧..一直在努力工作在这..和Daam的东西不会工作。

经过进一步的工作..我现在停留在这个..通过发送框中打开了..我选择电子邮件..但我得到的是一个空白邮件

private void shareIt(MediaPlayer mPlayer) { 
       //sharing implementation here 
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
sharingIntent.setType("audio/mp3"); 
sharingIntent.putExtra(Intent.EXTRA_SUBJECT,"Ringtone  File:"+getResources().getResourceEntryName(mPlayer)+".mp3"); 
sharingIntent.putExtra(Intent.EXTRA_TEXT,"Ringtone File : "+getResources().getResourceEntryName(mPlayer)+".mp3"); 
       sharingIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("android.resource://com.my.android.soundfiles/"+mPlayer)); 
sharingIntent.putExtra("sms_body","Ringtone File : "+getResources().getResourceEntryName(mPlayer)+".mp3"); 
       startActivity(Intent.createChooser(sharingIntent, "Share Sound File")); 

       } 
     }); 
+0

Did it .. still not working .. also set the View on .. OnLongClickListener() – Shamik 2013-05-11 18:42:52

+0

button1.setLongClickable(true); \t \t button1.setOnClickListener(新View.OnClickListener(){ \t \t \t公共无效的onClick(视图v){ \t \t \t \t尝试{ \t \t \t \t \t \t \t \t \t mPlayer.start(); \t \t \t \t mPlayer.setLooping(false); \t \t \t \t \t \t \t \t \t}赶上(例外五){ \t \t \t \t \t Log.e( “ButtonListenerActivity”, “错误:” + e.getMessage(), \t \t \t \t \t \t \t E); \t \t \t \t} – Shamik 2013-05-11 18:43:18

+0

button1.setOnLongClickListener(新View.OnLongClickListener(){ \t \t公共布尔onLongClick(视图v){ \t \t \t \t shareIt(); \t \t回归真实; \t \t} \t \t \t \t private void shareIt(){ \t \t \t \t \t //分享此实施 \t \t \t \t \t意向sharingIntent =新意图(android.content.Intent.ACTION_SEND); \t \t \t \t \t sharingIntent。的setType( “音频/ MPLAYER”); \t \t \t \t \t} \t \t}); – Shamik 2013-05-11 18:43:41

回答

0

你不用你的sharingIntent做任何事情,只是创建它。

+0

做到了..仍然没有工作..还设置.. OnLongClickListener() button1.setLongClickable(真)的视图; \t \t button1.setOnClickListener(新View.OnClickListener(){ \t \t \t公共无效的onClick(视图v){ \t \t \t \t尝试{ \t \t \t \t \t \t \t \t \t mPlayer.start(); \t \t \t \t mPlayer.setLooping(false); \t \t \t \t \t \t \t \t \t}赶上(例外五){ \t \t \t \t \t Log.e( “ButtonListenerActivity”, “错误:” + e.getMessage(), \t \t \t \t \t \t \t E); \t \t \t \t} – Shamik 2013-05-11 18:40:58

+0

对不起,我编辑我的回答所以现在看起来扑朔迷离。你没有对你的sharingIntent做任何事情。你只是创建它,所以它只是收集垃圾。你忘了一个startActivity或这样的行。 – Slinky 2013-05-11 18:43:51

+0

我不是要开始一个新的活动..按钮有一个音频文件设置为它..当你点击它..它扮演(R.splash)..我希望它能够在长期分享按...当你长按它..共享选项框应弹出..请您与应用.. WhatsApp的时,Gmail分享..等等等等 – Shamik 2013-05-11 18:54:04