通过通知移动到活动后,开始制作新的活动

时间:2014-06-02 05:46:36

标签: android android-activity android-notifications

MainActivity中存在一个问题,运行一个服务,发送一个通知表明应用程序正在运行,在最小化应用程序后,如果单击通知然后返回上一个Activity,一切都很好,但如果没有则运行应用程序从创建新活动的快捷方式。 如果未通过通知,则启动快捷方式将返回上一个活动。 可能是什么问题?

代码MainActivity:

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));
}

Code MediaService:

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());

}

0 个答案:

没有答案