我正在开发一个在线广播应用程序。该应用程序在后台运行。当我单击NotificationManager时,radioclass再次开始工作。我想调用正在运行的radioclass。我该怎么办?
player = MediaPlayer.create(this, Uri.parse("http://...../playlist.m3u8"));
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer player) {
}
});
player.start();
final int notificationID = 1234;
String msg = "mesajjj";
Log.d(TAG, "Preparing to update notification...: " + msg);
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, RadioClass.class);
//这里再次运行RadioClass。但RadioClass已经在运行。我应该调用正在运行的RadioClass。
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.logotam)
.setContentTitle("Test FM")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(pendingIntent);
mNotificationManager.notify(notificationID, mBuilder.build());
答案 0 :(得分:1)
如果您想要返回当前正在运行的同一活动,可以这样做:
在AndroidManifest.xml上添加以下标记:
<activity
........
android:launchMode="singleTop" >
........
</activity>
像这样修改您的PendingIntent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
通过此修改,您可以确保您的活动在任何给定时间只有一个实例。在您的活动类中,您还可以覆盖onNewIntent方法:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
此方法将处理您活动的所有其他调用。