2014-02-14 133 views
0

这是我的AndroidManifest.xml如何打开相同的应用程序,而无需打开新的实例,当应用程序在后台

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

    <uses-permission android:name="android.permission.GET_TASKS" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application android:name="MBCRADIO" android:label="MBCRADIO"> 
     <activity 
       android:name=".ListenMBC" 
      android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" />     
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

和ListenMBC.java文件在这里...

package org.apache.android.media; 
import android.app.Activity; 
import android.os.Bundle; 
import java.io.IOException; 
import android.widget.Button; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnPreparedListener; 
import android.os.Binder; 
import android.content.Intent; 


public class ListenMBC extends Activity { 
private static final String TAG = "radioListen"; 

private Button buttonPlaymbc1;  
private Button buttonPlaymbc2; 
private Button buttonStopPlay; 
private MediaPlayer player; 

public class LocalBinder extends Binder { 
    ListenMBC getService() { 
     return ListenMBC.this; 
    } 
} 

@Override 
public void onCreate(Bundle icicle) { 

    super.onCreate(icicle); 
    setContentView(R.layout.main); 

    buttonPlaymbc1 = (Button) findViewById(R.id.buttonPlaymbc1);    
    buttonPlaymbc2 = (Button) findViewById(R.id.buttonPlaymbc2);   
    buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay); 
    buttonStopPlay.setEnabled(false); 

    buttonPlaymbc1.setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
      stopPlayback(); 
      playRadio1(); 
      buttonPlaymbc1.setEnabled(false); 
      if(!buttonStopPlay.isEnabled()){ 
       buttonStopPlay.setEnabled(true); 
      } 
      if(!buttonPlaymbc2.isEnabled()){ 
       buttonPlaymbc2.setEnabled(true); 
      } 
     } 
    }); 



    buttonPlaymbc2.setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
      stopPlayback(); 
      playRadio2(); 
      buttonPlaymbc2.setEnabled(false); 
      if(!buttonStopPlay.isEnabled()){ 
       buttonStopPlay.setEnabled(true); 
      } 
      if(!buttonPlaymbc1.isEnabled()){ 
       buttonPlaymbc1.setEnabled(true); 
      } 
     } 
    }); 

    buttonStopPlay.setOnClickListener(new OnClickListener() { 
     public void onClick(View view) { 
     stopPlayback(); 
     } 
    }); 

} 



private void playRadio1() { 

    player = new MediaPlayer();    
    try { 
     player.reset(); 
     player.setDataSource("http://onlineradio.globemw.net:8000"); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    catch (Exception e) { 
     Log.e(TAG, "error: " + e.getMessage(), e); 
     if (player != null) { 
      stopPlayback(); 
     } 
    } 

    player.prepareAsync(); 
    player.setOnPreparedListener(new OnPreparedListener() { 

     public void onPrepared(MediaPlayer player) { 
      player.start(); 
     } 
    }); 
} 

private void playRadio2() { 
    player = new MediaPlayer(); 

    try { 
     player.reset(); 
     player.setDataSource("http://onlineradio.globemw.net:8002"); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    catch (Exception e) { 
     Log.e(TAG, "error: " + e.getMessage(), e); 
     if (player != null) { 
      stopPlayback(); 
     } 
    } 

    player.prepareAsync(); 
    player.setOnPreparedListener(new OnPreparedListener() { 

     public void onPrepared(MediaPlayer player) { 
      player.start(); 
     } 
    }); 
} 

private void stopPlayback() { 
    if (player!=null) { 
     player.stop(); 
     player.release(); 
     player=null; 
    } 
    buttonStopPlay.setEnabled(false); 
    if(!buttonPlaymbc1.isEnabled()){ 
     buttonPlaymbc1.setEnabled(true); 
    } 
    if(!buttonPlaymbc2.isEnabled()){ 
     buttonPlaymbc2.setEnabled(true); 
    } 
    } 
} 

这是MBCRADIO.java文件...

package org.apache.android.media; 

import android.app.Application; 

public class MBCRADIO extends Application { 

@Override 
public void onCreate() { 
} 

@Override 
public void onTerminate() { 
} 
} 

这是我的main.xml文件....

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      android:background="@drawable/bg" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="10dp" 
    android:text="@string/toptext" /> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="horizontal" 
    android:baselineAligned="false" >  
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:orientation="vertical" 
      android:layout_weight="1"> 

       <Button 
       android:id="@+id/buttonPlaymbc1" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:text="@string/play1" /> 

       <Button 
       android:id="@+id/buttonPlaymbc2" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:text="@string/play2" /> 

       <Button 
       android:id="@+id/buttonStopPlay" 
       android:layout_width="150dp" 
       android:layout_height="wrap_content" 
       android:text="@string/stop" /> 

     </LinearLayout> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1" 
      android:layout_gravity="center" 
      android:gravity="center" > 

      <ImageView android:id="@+id/ib" 
      android:src="@drawable/mbc150" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     </LinearLayout>  
</LinearLayout> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" /> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="20dp" 
    android:gravity="center" 
    android:text="@string/copy" /> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="20dp" 
    android:gravity="center" 
    android:text="@string/copytext" /> 

</LinearLayout> 

此代码工作正常。唯一的事情是打开应用程序后,当我点击后退按钮后会发生。然后,如果我再次打开这个应用程序,它打开另一个实例。但我想打开后台应用程序。如果有人对此主题感兴趣,请帮助。

谢谢。

+0

你可能想使用savedInstanceState,看到这里http://developer.android.com /training/basics/activity-lifecycle/recreating.html#SaveState – donfuxx

+0

没有什么改变! – LahiruTM

回答

0

您需要为活动的启动模式添加singleTop标志。改变你menifest文件添加启动模式为您的活动是这样的:

<activity 
      android:name=".ListenMBC" 
     android:label="@string/app_name" 
     android:launchMode="singleTop"> //This is what you need to add to you manifest 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" />     
     <category android:name="android.intent.category.DEFAULT" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

参考Android文档here更多信息

+0

谢谢你的回答,仍然有同样的问题。 – LahiruTM

相关问题