我为shoutcast流制作了应用程序,并且工作良好,但是当我停止它并按“后退”按钮返回时,此消息出现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)
从您的logcat中:
android.app.SuperNotCalledException: Activity {com.test.test/com.test.test.MainActivity} did not call through to super.onDestroy()
添加super.onDestroy()
到您的重写onDestroy()
方法。
当我按回buttom时,该应用程序已关闭,我不希望那样
您可以onBackPressed()
在活动中覆盖而不进行呼叫super.onBackPressed()
,finish()
也可以在此处不进行覆盖以防止后退键完成活动。尽管通常使您的应用不符合用户期望的行为是一个坏主意。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句