Код: Выделить всё
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oneanimal);ser=isServiceRunning(this);
intent= new Intent(this, MyService.class);
if (ser)
stopService(intent);
else;
startService(new Intent(this, MyService.class).putExtra("sound", 4));
startService(new Intent(this, MyService.class).putExtra("sound", 3));
}
public static boolean isServiceRunning(Context context) {
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<ActivityManager.RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (ActivityManager.RunningServiceInfo service : services) {
if (".MyService".equals(service.service.getShortClassName())) {
return true;
}
}
return false;
}
protected void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "MainActivity: onDestroy()");
ser=isServiceRunning(this);
if (ser) stopService(intent);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
float x1;
float y1;
x = event.getX();
y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // нажатие
x1 = x;
y1 = y;
ser=isServiceRunning(this);
if (ser)
stopService(intent);
else;
startService(new Intent(this, MyService.class).putExtra("sound", 4));
startService(new Intent(this, MyService.class).putExtra("sound", 3));
break;
case MotionEvent.ACTION_MOVE: // движение
break;
case MotionEvent.ACTION_UP: // отпускание
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
} Код: Выделить всё
public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "MyService onCreate");
es = Executors.newFixedThreadPool(1);
stopselfyes=false;
}
public void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "MyService onDestroy"+" stopselfyes= " + stopselfyes);
if (!stopselfyes)
{
mPlayer.stop();
mPlayer.release();
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(LOG_TAG, "MyService onStartCommand");
int sound = intent.getIntExtra("sound", 0);
switch (sound) {
case 1:
mPlayer= MediaPlayer.create(this, R.raw.skillet);
mPlayer.setLooping(false);
break;
case 2:
mPlayer= MediaPlayer.create(this, R.raw.skillet2);
mPlayer.setLooping(false);
break;
case 3:
mPlayer= MediaPlayer.create(this, R.raw.s1);
mPlayer.setLooping(false);
break;
case 4:
mPlayer= MediaPlayer.create(this, R.raw.s2);
mPlayer.setLooping(false);
break;
default:
break;
}
MyRun mr = new MyRun(sound, startId);
es.execute(mr);
return super.onStartCommand(intent, flags, startId);
}
public IBinder onBind(Intent arg0) {
return null;
}
class MyRun implements Runnable {
int sound;
int startId;
public MyRun(int sound, int startId) {
this.sound = sound;
this.startId = startId;
Log.d(LOG_TAG, "MyRun#" + startId + " create");
}
public void run() {
Log.d(LOG_TAG, "MyRun#" + startId + " start");
play=true;
mPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mPlayer) {
mPlayer.stop();
mPlayer.release();
play=false;
Log.d(LOG_TAG, "MyRun#" + startId + " end, stopSelf(" + startId + ")");
if (startId==2) stopselfyes=true;
stopSelf(startId);
}
});
mPlayer.start();
while(play) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {}
}
}