2014-02-14 1 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> 

Этот код работает отлично. Единственное, что нужно сделать, это открыть приложение, которое будет возвращено, когда я нажму кнопку «Назад». Затем, если я снова открою это приложение, он откроет другой экземпляр. Но я хочу открыть фоновое приложение для фронта. Пожалуйста, помогите, если какой-либо орган заинтересован в этой теме.

Thank you.

+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> 

Обратитесь к документации андроида here для получения дополнительной информации

+0

Благодарим вас за ответ, но с той же проблемой. – LahiruTM

Смежные вопросы