I made app for shoutcast stream and work good but when I stop it and go back by back button
this message appear unfortunately android app has stopped
and this my code i want to close the app when i stop it by stop button and when press back button close do not unfortunately android app has stopped
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button startButton, stopButton;
static Context context;
boolean isPlaying;
Intent streamService;
SharedPreferences prefs;
// headset
private int headsetSwitch = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
startButton = (Button) findViewById(R.id.startButton);
stopButton = (Button) findViewById(R.id.stopButton);
prefs = PreferenceManager.getDefaultSharedPreferences(context);
getPrefs();
streamService = new Intent(MainActivity.this, StreamService.class);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(streamService);
startButton.setEnabled(false);
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopService(streamService);
startButton.setEnabled(true);
//headset2
registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
}
});
}
// If headset gets unplugged, stop music and service.
private BroadcastReceiver headsetReceiver = new BroadcastReceiver() {
private boolean headsetConnected = false;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// Log.v(TAG, "ACTION_HEADSET_PLUG Intent received");
if (intent.hasExtra("state")) {
if (headsetConnected && intent.getIntExtra("state", 0) == 0) {
headsetConnected = false;
headsetSwitch = 0;
// Log.v(TAG, "State = Headset disconnected");
// headsetDisconnected();
} else if (!headsetConnected
&& intent.getIntExtra("state", 0) == 1) {
headsetConnected = true;
headsetSwitch = 1;
// Log.v(TAG, "State = Headset connected");
}
}
switch (headsetSwitch) {
case (0):
headsetDisconnected();
break;
case (1):
break;
}
}
};
private void headsetDisconnected() {
stopService(streamService);
startButton.setEnabled(true);
}
public void getPrefs() {
isPlaying = prefs.getBoolean("isPlaying", false);
if (isPlaying) startButton.setEnabled(false);
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
//Incoming call: Pause music
stopService(streamService);
startButton.setEnabled(true);
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
//Not in call: Play music
startService(streamService);
startButton.setEnabled(false);
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
stopService(streamService);
startButton.setEnabled(true);
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
public void onDestroy() {
stopService(streamService);
startButton.setEnabled(true);
}
}
08-06 13:12:25.519: D/StreamService(22065): onDestroy
08-06 13:12:25.519: V/MediaPlayer-JNI(22065): stop
08-06 13:12:25.519: V/MediaPlayer(22065): stop
08-06 13:12:25.529: V/MediaPlayer-JNI(22065): release
08-06 13:12:25.529: V/MediaPlayer(22065): setListener
08-06 13:12:25.529: V/MediaPlayer(22065): disconnect
08-06 13:12:25.539: V/MediaPlayer(22065): destructor
08-06 13:12:25.539: V/MediaPlayer(22065): disconnect
08-06 13:12:26.624: D/AndroidRuntime(22039): Shutting down VM
08-06 13:12:26.624: W/dalvikvm(22039): threadid=1: thread exiting with uncaught exception (group=0x41d44700)
08-06 13:12:26.629: E/AndroidRuntime(22039): FATAL EXCEPTION: main
08-06 13:12:26.629: E/AndroidRuntime(22039): android.app.SuperNotCalledException: Activity {com.test.test/com.test.test.MainActivity} did not call through to super.onDestroy()
08-06 13:12:26.629: E/AndroidRuntime(22039): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3621)
08-06 13:12:26.629: E/AndroidRuntime(22039): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3654)
08-06 13:12:26.629: E/AndroidRuntime(22039): at android.app.ActivityThread.access$1300(ActivityThread.java:159)
08-06 13:12:26.629: E/AndroidRuntime(22039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
08-06 13:12:26.629: E/AndroidRuntime(22039): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 13:12:26.629: E/AndroidRuntime(22039): at android.os.Looper.loop(Looper.java:176)
08-06 13:12:26.629: E/AndroidRuntime(22039): at android.app.ActivityThread.main(ActivityThread.java:5419)
08-06 13:12:26.629: E/AndroidRuntime(22039): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 13:12:26.629: E/AndroidRuntime(22039): at java.lang.reflect.Method.invoke(Method.java:525)
08-06 13:12:26.629: E/AndroidRuntime(22039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
08-06 13:12:26.629: E/AndroidRuntime(22039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
08-06 13:12:26.629: E/AndroidRuntime(22039): at dalvik.system.NativeStart.main(Native Method)