简体   繁体   中英

How do I destroy a JFrame window?

After searching in life cycle I have found that my application needs to use the destroy(); method. However, when I implement this the application doesn't close. I am trying to apply this to the button exitButton . If someone could direct me in the right direction then that would be great.

MainActivity.java

package com.webcraftbd.radio;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {        

    static String radioTitle = "Android Live Radio";
    static String radioStreamURL = "http://stream.infowars.com:80"; 

    Button playButton;
    Button pauseButton;
    Button stopButton;  
    Button exitbutton;
    TextView statusTextView, bufferValueTextView;
    NotificationCompat.Builder notifyBuilder;

    private RadioUpdateReceiver radioUpdateReceiver;
    private RadioService radioServiceBinder;

    //Notification
     private static final int NOTIFY_ME_ID=12345;
     private NotificationManager notifyMgr=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_main);

        TextView titleTextView = (TextView) this.findViewById(R.id.titleTextView);
        titleTextView.setText(radioTitle);

        playButton = (Button) this.findViewById(R.id.PlayButton);
        pauseButton = (Button) this.findViewById(R.id.PauseButton);
        stopButton = (Button) this.findViewById(R.id.StopButton);
        playButton.setEnabled(true);
        pauseButton.setEnabled(false);
        stopButton.setEnabled(false);
        pauseButton.setVisibility(View.INVISIBLE);

        statusTextView = (TextView) this.findViewById(R.id.StatusDisplayTextView);

        notifyMgr=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        showNotification();

        // Bind to the service
        Intent bindIntent = new Intent(this, RadioService.class);
        bindService(bindIntent, radioConnection, Context.BIND_AUTO_CREATE);
        startService(new Intent(this, RadioService.class));
    }


    public void onClickPlayButton(View view) {
        radioServiceBinder.play();
    }

    public void onClickPauseButton(View view) {
        radioServiceBinder.pause();
    }

    public void onClickStopButton(View view) {      
        radioServiceBinder.stop();
    }
    public void onClicexitbutton(View view) {       
        super.finish();
        super.onDestroy();
    radioServiceBinder.onDestroy();
    radioServiceBinder.stop();
    radioServiceBinder.stopService(getParentActivityIntent());

    }


    @Override
    protected void onPause() {
        super.onPause();
        if (radioUpdateReceiver != null) 
            unregisterReceiver(radioUpdateReceiver);
    }

    @Override
    protected void onResume() {     
        super.onResume();

        /* Register for receiving broadcast messages */
        if (radioUpdateReceiver == null) radioUpdateReceiver = new RadioUpdateReceiver();   
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_CREATED));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_DESTROYED));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_STARTED));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_PREPARED));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_PLAYING));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_PAUSED));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_STOPPED));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_COMPLETED));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_ERROR));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_BUFFERING_START));
        registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_BUFFERING_END));
    }

    protected void onDestroy(){

        super.onDestroy();
    }
    /* Receive Broadcast Messages from RadioService */
    private class RadioUpdateReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(RadioService.MODE_CREATED)) {
                showNotification();
            }
            else if (intent.getAction().equals(RadioService.MODE_DESTROYED)) {
                clearNotification();
            }
            else if (intent.getAction().equals(RadioService.MODE_STARTED)) {
                playButton.setEnabled(false);
                pauseButton.setEnabled(false);
                stopButton.setEnabled(true);
                playButton.setVisibility(View.VISIBLE);
                pauseButton.setVisibility(View.INVISIBLE);
                updateStatus("Buffering...");
            }
            else if (intent.getAction().equals(RadioService.MODE_PREPARED)) {
                playButton.setEnabled(true);
                pauseButton.setEnabled(false);
                stopButton.setEnabled(false);
                playButton.setVisibility(View.VISIBLE);
                pauseButton.setVisibility(View.INVISIBLE);
                updateStatus("Rady");
            }
            else if (intent.getAction().equals(RadioService.MODE_BUFFERING_START)) {
                updateStatus("Buffering...");
            }
            else if (intent.getAction().equals(RadioService.MODE_BUFFERING_END)) {
                updateStatus("Playing");
            }
            else if (intent.getAction().equals(RadioService.MODE_PLAYING)) {
                playButton.setEnabled(false);
                pauseButton.setEnabled(true);
                stopButton.setEnabled(true);
                playButton.setVisibility(View.INVISIBLE);
                pauseButton.setVisibility(View.VISIBLE);
                showNotification();
                updateStatus("Playing");
            }
            else if(intent.getAction().equals(RadioService.MODE_PAUSED)) {
                playButton.setEnabled(true);
                pauseButton.setEnabled(false);
                stopButton.setEnabled(true);
                playButton.setVisibility(View.VISIBLE);
                pauseButton.setVisibility(View.INVISIBLE);
                updateStatus("Paused");
            }
            else if(intent.getAction().equals(RadioService.MODE_STOPPED)) {
                playButton.setEnabled(true);
                pauseButton.setEnabled(false);
                stopButton.setEnabled(false);
                playButton.setVisibility(View.VISIBLE);
                pauseButton.setVisibility(View.INVISIBLE);
                updateStatus("Stopped");
                clearNotification();
            }
            else if(intent.getAction().equals(RadioService.MODE_COMPLETED)) {
                playButton.setEnabled(true);
                pauseButton.setEnabled(false);
                stopButton.setEnabled(false);
                playButton.setVisibility(View.VISIBLE);
                pauseButton.setVisibility(View.INVISIBLE);
                updateStatus("Stopped");
            }
            else if(intent.getAction().equals(RadioService.MODE_ERROR)) {
                playButton.setEnabled(true);
                pauseButton.setEnabled(false);
                stopButton.setEnabled(false);
                playButton.setVisibility(View.VISIBLE);
                pauseButton.setVisibility(View.INVISIBLE);
                updateStatus("Error");
            }   
        }
    }

    public void updateStatus(String status) {       
        try {
                if(notifyBuilder!=null && notifyMgr!=null) {
                    notifyBuilder.setContentText(status).setWhen(0);
                    notifyMgr.notify(NOTIFY_ME_ID,notifyBuilder.build());
                }
                statusTextView.setText(status);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void showNotification() {
        notifyBuilder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setContentTitle(radioTitle).setContentText("");
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(MainActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

        notifyBuilder.setContentIntent(resultPendingIntent);
        notifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notifyMgr.notify(NOTIFY_ME_ID, notifyBuilder.build());
    }

    public void clearNotification() {
        notifyMgr.cancel(NOTIFY_ME_ID);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.about:
            Intent i = new Intent(this, AboutActivity.class);
            startActivity(i);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    // Handles the connection between the service and activity
    private ServiceConnection radioConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            radioServiceBinder = ((RadioService.RadioBinder)service).getService();
        }
        public void onServiceDisconnected(ComponentName className) {
            radioServiceBinder = null;
        }
    };
}
enter code here

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4a4a4a"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/player_header_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="48dp"
            android:gravity="center"
            android:text="Classic Christmas Radio"
            android:textAlignment="center"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#c0413b"
            android:textSize="40sp" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_marginBottom="120dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="110dp"
        android:src="@drawable/cover" />

    <TextView
        android:id="@+id/StatusDisplayTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/PauseButton"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignRight="@+id/imageView1"
        android:gravity="center"
        android:text="Unknown" />

    <Button
        android:id="@+id/PauseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/StatusDisplayTextView"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="0.0dip"
        android:background="@drawable/btn_pause"
        android:onClick="onClickPauseButton" />

    <Button
        android:id="@+id/PlayButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/StatusDisplayTextView"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="0.0dip"
        android:background="@drawable/btn_play"
        android:onClick="onClickPlayButton" />

    <Button
        android:id="@+id/StopButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@id/StatusDisplayTextView"
        android:layout_marginBottom="0.0dip"
        android:background="@drawable/btn_stop"
        android:onClick="onClickStopButton" />

    <Button
        android:id="@+id/exitbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/StatusDisplayTextView"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

RadioService.java

package com.webcraftbd.radio;

import java.io.IOException;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class RadioService extends Service implements OnErrorListener, OnCompletionListener, OnPreparedListener, OnInfoListener {

    private MediaPlayer mediaPlayer;
    private String radioStreamURL = MainActivity.radioStreamURL;

    public static final String MODE_CREATED = "CREATED";
    public static final String MODE_DESTROYED = "DESTROYED";
    public static final String MODE_PREPARED = "PREPARED";
    public static final String MODE_STARTED = "STARTED";
    public static final String MODE_PLAYING = "PLAYING";
    public static final String MODE_PAUSED = "PAUSED";
    public static final String MODE_STOPPED = "STOPPED";
    public static final String MODE_COMPLETED = "COMPLETED";
    public static final String MODE_ERROR = "ERROR";
    public static final String MODE_BUFFERING_START = "BUFFERING_START";
    public static final String MODE_BUFFERING_END = "BUFFERING_END";

    private boolean isPrepared = false;

    private final IBinder binder = new RadioBinder();

    @Override
    public void onCreate() {
        /* Create MediaPlayer when it starts for first time */
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnInfoListener(this);

        sendBroadcast(new Intent(MODE_CREATED));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mediaPlayer.stop();
        mediaPlayer.reset();
        isPrepared = false;
        sendBroadcast(new Intent(MODE_DESTROYED));
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {  
        sendBroadcast(new Intent(MODE_STARTED));

        /* Starts playback at first time or resumes if it is restarted */
        if(mediaPlayer.isPlaying())
            sendBroadcast(new Intent(MODE_PLAYING));
        else if(isPrepared) {
            sendBroadcast(new Intent(MODE_PAUSED));
        }
        else
            prepare();

        return Service.START_STICKY;
    }


    @Override
    public void onPrepared(MediaPlayer _mediaPlayer) {
        /* If radio is prepared then start playback */
        sendBroadcast(new Intent(MODE_PREPARED));
        isPrepared = true;
        play();
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) { 
        /* When no stream found then complete the playback */
        mediaPlayer.stop();
        mediaPlayer.reset();
        isPrepared = false;
        sendBroadcast(new Intent(MODE_COMPLETED));
    }

    public void prepare() {     
        /* Prepare Async Task - starts buffering */
        try {           
            mediaPlayer.setDataSource(radioStreamURL);
            mediaPlayer.prepareAsync();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void play() {
        if(isPrepared) {
            mediaPlayer.start();
            System.out.println("RadioService: play");
            sendBroadcast(new Intent(MODE_PLAYING));
        }
        else
        {
            sendBroadcast(new Intent(MODE_STARTED));
            prepare();
        }
    }

    public void pause() {
        mediaPlayer.pause();
        System.out.println("RadioService: pause");
        sendBroadcast(new Intent(MODE_PAUSED));
    }

    public void stop() {
        mediaPlayer.stop();
        mediaPlayer.reset();
        isPrepared = false;
        System.out.println("RadioService: stop");
        sendBroadcast(new Intent(MODE_STOPPED));
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        /* Check when buffering is started or ended */
        if(what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
            sendBroadcast(new Intent(MODE_BUFFERING_START));
        }
        else if(what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
            sendBroadcast(new Intent(MODE_BUFFERING_END));
        }

        return false;
    }


    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        sendBroadcast(new Intent(MODE_ERROR));
        switch (what) {
            case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
                Log.v("ERROR","MEDIA ERROR NOT VALID FOR PROGRESSIVE PLAYBACK " + extra);
                break;
            case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                Log.v("ERROR","MEDIA ERROR SERVER DIED " + extra);
                break;
            case MediaPlayer.MEDIA_ERROR_UNKNOWN:
                Log.v("ERROR","MEDIA ERROR UNKNOWN " + extra);
                break;
        }
        return false;
    }


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

    /* Allowing activity to access all methods of RadioService */
    public class RadioBinder extends Binder {
        RadioService getService() {
            return RadioService.this;
        }
    }

}

Ok, after developing some pseudo and some logic, I have noticed that Andorid doesn't have an exit method in the way that you're hoping. When first reading the question I didn't realise that it was Android as I was multi-tasking. As you may notice, almost every android application doesn't have an exit button, and there is a reason for this. The reason being that Android is supposedly meant to load and unload applications through the OS, hence why you don't do it. If the user wanted to exit the application, they would do what they do with every other, enter the multi-tasking menu and swipe it off. Please read the following for more information: http://android.nextapp.com/site/fx/doc/exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM