我在 MediaPActivity 活动中声明了一个媒体播放器变量,然后想在第二个活动中暂停或停止它。我已经在第二类中声明了意图,但是当我尝试点击 MediaPActivity 中的 btn1 时媒体播放器,第二个活动中的媒体播放器没有暂停/停止。下面给出的代码有什么问题?
public class MediaPActivity extends Activity {
Button btn, btn1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn);
btn1 = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MediaPActivity.this, Second.class);
startActivity(i);
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MediaPActivity.this, Second.class);
i.setAction(Second.ACTION_PAUSE);
}
});
}
}
第二个活动:
public class Second extends Activity {
Button btnplay, btnpause;
MediaPlayer player;
public static final String ACTION_PLAY = "com.synapse.test.PLAY";
public static final String ACTION_PAUSE = "com.synapse.test.PAUSE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
btnplay = (Button) findViewById(R.id.button1);
btnpause = (Button) findViewById(R.id.button2);
player = new MediaPlayer();
player = MediaPlayer.create(Second.this, R.raw.testsong_20_sec);
btnplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
player.start();
}
});
btnpause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
player.pause();
}
});
}
@Override
protected void onNewIntent(Intent intent) {
if(intent.getAction().equals(ACTION_PLAY)){
player.start();
}
else if(intent.getAction().equals(ACTION_PAUSE)){
player.pause();
}
}
}
Manifest File:
<activity
android:name=".MediaPActivity"
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=".Second" android:launchMode="singleTop" />