Я пытаюсь сделать приложение с одной кнопкой. Когда вы нажимаете, он воспроизводит звук, а когда вы LONG нажимаете его, он дает вам возможность делиться. Приложение работает без каких-либо ошибок. Но опция share просто не открывается. И кнопка ничего не делает, когда вы долго нажимаете на нее.Реализация долгого нажатия для совместного использования кнопки в 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;
}
}
Его крайняя неудовлетворенность .. так много работала над этим .. и дело Даама просто не будет работать.
после некоторой дальнейшей работы .. я сейчас застрял на этом .. посыла через поле открывает .. я выбрать электронную почту .. но все, что я получаю пустой электронной почты
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"));
}
});
Сделал это .. все еще не работает .. также установить вид на .. OnLongClickListener() – Shamik
button1.setLongClickable (true); \t \t button1.setOnClickListener (новый View.OnClickListener() { \t \t \t общественного недействительными OnClick (View 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 Log.e ("ButtonListenerActivity", "ошибка:" + e.getMessage(), \t \t \t \t \t \t \t д); \t \t \t \t – Shamik
} button1.setOnLongClickListener (новый View.OnLongClickListener() { \t \t общественного логический onLongClick (View v) { \t \t \t \t ShareIT(); \t \t возвращающие; \t \t} () { \t \t \t \t \t // совместного осуществления здесь \t \t \t \t \t Намерение sharingIntent = новый Intent (android.content.Intent.ACTION_SEND); \t \t \t \t \t shareIntent.SetType ("аудио/MPLAYER"); \t \t \t \t \t} \t \t}); – Shamik