我想要一個只有一個按鈕的應用程序。當你按下它播放聲音,當你長按它,它給你選擇分享。該應用程序工作沒有任何錯誤..但共享選項只是不會打開。長按按鈕時,按鈕不起作用。實施長按分享按鈕在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"));
}
});
Did it .. still not working .. also set the View on .. OnLongClickListener() – Shamik 2013-05-11 18:42:52
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
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