停止服务,停止活动

时间:2014-01-27 16:36:23

标签: android service android-activity

我正在尝试完成我的申请。但我有一个伤害我头脑的虫子。

当我编译我的AVD时,我没有任何错误,但在我的android,crach应用程序上使用我的应用程序。这是我的Boutton停止后面的代码,它终止了可能应该是问题根源的应用程序。

请帮忙。 我在Android设备中编译应用程序。

 public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
    int delay = 0;
    boolean isFirstTime = true;
    int period = 0;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Log.d(TAG, "onCreate");
    period = 60000;

    player = MediaPlayer.create(this, R.raw.son1);
    AudioManager mAudioManager = (AudioManager)getApplication().getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
    /***************Repete la sonnerie****************/
    Timer timer = new Timer();
      timer.scheduleAtFixedRate(new TimerTask() {
      public void run() {
          if (isFirstTime) {
           isFirstTime = false;
           player.start();
          }
          else{
              player.start();
          }
      }
      }, delay, period);

}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
    player.release();
}

@Override
public void onStart(Intent intent, int startid) {
    Log.d(TAG, "onStart");
    player.start();
}   

}

logcat的

02-05 11:36:50.237: D/MyService(2473): onDestroy
02-05 11:37:10.647: W/dalvikvm(2473): threadid=10: thread exiting with uncaught exception (group=0x4001e578)
02-05 11:37:10.657: E/AndroidRuntime(2473): FATAL EXCEPTION: Timer-0
02-05 11:37:10.657: E/AndroidRuntime(2473): java.lang.IllegalStateException
02-05 11:37:10.657: E/AndroidRuntime(2473):     at android.media.MediaPlayer._start(Native Method)
02-05 11:37:10.657: E/AndroidRuntime(2473):     at android.media.MediaPlayer.start(MediaPlayer.java:960)
02-05 11:37:10.657: E/AndroidRuntime(2473):     at com.sms.MyService$1.run(MyService.java:51)
02-05 11:37:10.657: E/AndroidRuntime(2473):     at java.util.Timer$TimerImpl.run(Timer.java:284)

2 个答案:

答案 0 :(得分:0)

要停止服务,请尝试以下操作:

stopService(new Intent(this, MyService.class));

停止活动:

MyActivity.this.finish();

答案 1 :(得分:0)

public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
    int delay = 0;
    boolean isFirstTime = true;
    int period = 0;
    Timer timer;
    TimerTask tt;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    period = 60000;

    player = MediaPlayer.create(this, R.raw.son1);
    AudioManager mAudioManager = (AudioManager)getApplication().getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
    /***************Repete la sonnerie****************/
    timer = new Timer();
    tt = new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if (isFirstTime) {
                   isFirstTime = false;
                   player.start();
                  }
                  else{
                      player.start();
                  }
        }
    };
    timer.scheduleAtFixedRate(tt, delay, period);

}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
    player.release();
    tt.cancel();
    timer.cancel();

}

@Override
public void onStart(Intent intent, int startid) {
    Log.d(TAG, "onStart");
    player.start();
}

}