Android 通过通知移动到活动后,开始创建新活动

Android 通过通知移动到活动后,开始创建新活动,android,android-activity,android-notifications,Android,Android Activity,Android Notifications,MainActivity运行服务时存在问题,该服务发送通知,说明应用程序正在运行。最小化应用程序后,如果单击通知,然后返回上一个活动,则一切正常,但如果没有,则从创建新活动的快捷方式运行应用程序。 如果未通过通知,启动快捷方式将返回上一个活动。 有什么问题吗 代码main活动: public class MainActivity extends FragmentActivity { final String LOG_TAG = "myLogs"; static boolean isPlayin

MainActivity运行服务时存在问题,该服务发送通知,说明应用程序正在运行。最小化应用程序后,如果单击通知,然后返回上一个活动,则一切正常,但如果没有,则从创建新活动的快捷方式运行应用程序。 如果未通过通知,启动快捷方式将返回上一个活动。 有什么问题吗

代码main活动:

public class MainActivity extends FragmentActivity {

final String LOG_TAG = "myLogs";
static boolean isPlayingMain;
static boolean isPlayingLiquid;
static boolean isPlayingDubstep;
MediaPlayer mediaPlayer;
static AudioManager am;
static CheckBox pdaStream;
Button btnNews;
static TitleAdapter titleAdapter;
static ViewPager mViewPager;
Boolean isInternetPresent;
ConnectionDetector cd;
static String selectStream;
public String stream_sel;
public int stream;
public boolean checker;
public static boolean track;
public boolean searchtrack;
SharedPreferences mSettings;
static TextView titleMusic;
static boolean replay = false;
private MusicIntentReceiver myReceiver;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    titleAdapter = new TitleAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(titleAdapter);
    mViewPager.setCurrentItem(stream);
    mViewPager.setOffscreenPageLimit(3);
    pdaStream = (CheckBox) findViewById(R.id.pdaStream);
    am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    SeekBar music = (SeekBar)findViewById(R.id.seekBar1);
    initBar(music, AudioManager.STREAM_MUSIC);
            isPlayingMain = false;
    isPlayingLiquid = false;
    isPlayingDubstep = false;

    titleMusic = (TextView) findViewById(R.id.titleMusic);
    titleMusic.setSelected(true);
    titleMusic.setVisibility(View.VISIBLE);


    IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    myReceiver = new MusicIntentReceiver();
    registerReceiver(myReceiver, filter);

    startService(new Intent(this, MediaService.class));
}
代码中介服务:

public class MediaService extends Service implements OnPreparedListener,OnCompletionListener{

    static MediaPlayer mediaPlayer;
static NotificationManager nm;
private static NotificationCompat.Builder mBuilder;
public static Context ctx;
    String titleNotif = "Station";
String contentNotif = "Running";

    public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2)  {

    try    {
          TimeUnit.SECONDS.sleep(0);
          notif(titleNotif, contentNotif);
          return super.onStartCommand(paramIntent, paramInt1, paramInt2);
        }
        catch (InterruptedException localInterruptedException)    {
          for (;;)      {
            localInterruptedException.printStackTrace();
          }
        }
}


public void notif(String titleNotif, String contentNotif){

    //building the notification
    mBuilder = new NotificationCompat.Builder(ctx)
    .setSmallIcon(R.drawable.spy)
    .setContentTitle(titleNotif)
    .setTicker(contentNotif)
    .setOngoing(true);

    Intent notificationIntent = new Intent(ctx, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pendingIntent);
    //Notification n = mBuilder.build();
    //nm.notify(1, n);
    startForeground(1, mBuilder.build());

}

您可以在AndroidManifet上将活动标记为singletop活动。您可以在通知中添加标志..清除顶部和新任务..以启动新活动