这是我的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>
并在这里监听MBC.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 :(得分: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