-1

我想在应用程序启动之前显示类加载。

所以,我有类在打开我的应用程序之前显示加载图像。

但我的问题是,我有 2 节课,src第一节课main.java,第二节课LoadingScreenActivity.java

我如何打开课程LoadingScreenActivity.java然后当我的应用程序加载成功时打开主课程main.java

这是 LoadingScreenActivity 类:-

public class LoadingScreenActivity extends Activity 
{
    //creates a ViewSwitcher object, to switch between Views
    private ViewSwitcher viewSwitcher;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        //Initialize a LoadViewTask object and call the execute() method
        new LoadViewTask().execute();
    }

    //To use the AsyncTask, it must be subclassed
    private class LoadViewTask extends AsyncTask<Void, Integer, Void>
    {
        //A TextView object and a ProgressBar object
        private TextView tv_progress;
        private ProgressBar pb_progressBar;

        //Before running code in the separate thread
        @Override
        protected void onPreExecute() 
        {
            //Initialize the ViewSwitcher object
            viewSwitcher = new ViewSwitcher(LoadingScreenActivity.this);
            /* Initialize the loading screen with data from the 'loadingscreen.xml' layout xml file. 
             * Add the initialized View to the viewSwitcher.*/
            viewSwitcher.addView(ViewSwitcher.inflate(LoadingScreenActivity.this, R.layout.activity_main, null));

            //Initialize the TextView and ProgressBar instances - IMPORTANT: call findViewById() from viewSwitcher.
            tv_progress = (TextView) viewSwitcher.findViewById(R.id.tv_progress);
            pb_progressBar = (ProgressBar) viewSwitcher.findViewById(R.id.pb_progressbar);
            //Sets the maximum value of the progress bar to 100             
            pb_progressBar.setMax(100);

            //Set ViewSwitcher instance as the current View.
            setContentView(viewSwitcher);
        }

        //The code to be executed in a background thread.
        @Override
        protected Void doInBackground(Void... params) 
        {
            /* This is just a code that delays the thread execution 4 times, 
             * during 850 milliseconds and updates the current progress. This 
             * is where the code that is going to be executed on a background
             * thread must be placed. 
             */
            try 
            {
                //Get the current thread's token
                synchronized (this) 
                {
                    //Initialize an integer (that will act as a counter) to zero
                    int counter = 0;
                    //While the counter is smaller than four
                    while(counter <= 4)
                    {
                        //Wait 850 milliseconds
                        this.wait(850);
                        //Increment the counter 
                        counter++;
                        //Set the current progress. 
                        //This value is going to be passed to the onProgressUpdate() method.
                        publishProgress(counter*25);
                    }
                }
            } 
            catch (InterruptedException e) 
            {
                e.printStackTrace();
            }
            return null;
        }

        //Update the TextView and the progress at progress bar
        @Override
        protected void onProgressUpdate(Integer... values) 
        {
            //Update the progress at the UI if progress value is smaller than 100
            if(values[0] <= 100)
            {
                tv_progress.setText("Progress: " + Integer.toString(values[0]) + "%");
                pb_progressBar.setProgress(values[0]);
            }
        }

        //After executing the code in the thread
        @Override
        protected void onPostExecute(Void result) 
        {
            /* Initialize the application's main interface from the 'main.xml' layout xml file. 
             * Add the initialized View to the viewSwitcher.*/
            viewSwitcher.addView(ViewSwitcher.inflate(LoadingScreenActivity.this, R.layout.activity_main, null));
            //Switch the Views
            viewSwitcher.showNext();
        }
    }

    //Override the default back key behavior
    @Override
    public void onBackPressed() 
    {
        //Emulate the progressDialog.setCancelable(false) behavior
        //If the first view is being shown
        if(viewSwitcher.getDisplayedChild() == 0)
        {
            //Do nothing
            return;
        }
        else
        {
            //Finishes the current Activity
            super.onBackPressed();
        }
    }
}

这是主要课程:-

public class MainActivity extends Activity implements OnClickListener{
    public MediaPlayer mp;
    boolean isPrepared = false;
    Button PlayBtn;
    Button PauseBtn;
    Button StopBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mp = new MediaPlayer();
        PlayBtn = (Button)findViewById(R.id.btnPlay);
        PlayBtn .setOnClickListener(this);
        PauseBtn = (Button)findViewById(R.id.btnPause);
        PauseBtn .setOnClickListener(this);
        StopBtn = (Button)findViewById(R.id.btnStop);
        StopBtn .setOnClickListener(this);

    }

        @Override
        public void onClick(View v){
            if(v == PlayBtn){
                 startradio(v);
              }
             else if(v == PauseBtn){
                 pauseradio(v);
              }
             else if(v == StopBtn){
                 stopradio(v);
              }
            }

        public void onCompletion(MediaPlayer mediaPlayer) {
         synchronized(this){
            isPrepared = false;
            }
            }

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

        try {
            mp.setDataSource("http://radio.arabhosters.com:8015/");
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } //also consider mp.prepareAsync().
        // defult start stream when start App.
        mp.start();
        }

        // method for play stream after stop it.
        public void startradio(View v) {
            try{
                if(mp.isPlaying()){
                    return;
                }
                   mp.start();
            } catch(IllegalStateException ex){
                ex.printStackTrace();
            } 
        }

    // method for pause stream. 
    public void pauseradio(View v) {
        mp.pause();
    }

    public boolean isPlaying() {
        return mp.isPlaying();
    }

    public boolean isLooping() {
        return mp.isLooping();
    }

    public void setLooping(boolean isLooping) {
        mp.setLooping(isLooping);
    }

    public void setVolume(float volumeLeft, float volumeRight) {
        mp.setVolume(volumeLeft, volumeRight);
    }

    // method for stop stream.
    public void stopradio(View v) {
        if(mp.isPlaying()){
            mp.stop();
        }
        mp.release();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

}

编辑 :-

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kam"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.kam.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
4

1 回答 1

2

您可以通过将以下意图过滤器添加到您要在 AndroidManifest 中首先加载的活动来做到这一点:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kam"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.kam.LoadingScreenActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.kam.MainActivity"/>
    </application>

</manifest>

然后,当您想从加载活动调用中打开 MainActivity 时:

Intent intent = new Intent(LoadingScreenActivity.this,MainActivity.class);
startActivity(intent);
于 2013-04-26T15:14:00.137 回答