Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 生命周期,应用程序自行重新打开_Java_Android_Eclipse - Fatal编程技术网

Java 生命周期,应用程序自行重新打开

Java 生命周期,应用程序自行重新打开,java,android,eclipse,Java,Android,Eclipse,理解生命周期并尝试对应用程序编写onDestroy()代码后,仍然存在应用程序自行启动的相同问题,如果我尝试以任何方式关闭应用程序,通过多任务exp将其自行重新启动,我希望使其在关闭后不再启动该代码 MainActivity.java package com.webcraftbd.radio; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.app.

理解生命周期并尝试对应用程序编写onDestroy()代码后,仍然存在应用程序自行启动的相同问题,如果我尝试以任何方式关闭应用程序,通过多任务exp将其自行重新启动,我希望使其在关闭后不再启动该代码

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 = "Radio 16Bar";
    static String radioStreamURL = "http://s9.voscast.com:8724"; 

    Button playButton;
    Button pauseButton;
    Button stopButton;  
    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();
    }


    @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));
    }

    /* 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;
        }
    };
}