2014-11-07 23 views
3

我正在開發在線廣播應用程序。該應用程序在後臺工作。當我點擊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()); 

回答

1

如果你想回到你當前正在運行的同一個活動,你可以做這樣的:

添加下面的標籤在你的AndroidManifest.xml:

<activity 
........ 
    android:launchMode="singleTop" > 
........ 
</activity> 

修改您的PendingIntent像這樣:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 

通過此修改,您可以確保您的activ在任何時候都只有一個實例。在您的活動類,你也可以覆蓋onNewIntent方法:

@Override 
protected void onNewIntent(Intent intent) { 
    super.onNewIntent(intent); 

} 

此方法將處理所有您的活動的額外調用。

+0

完美! 非常感謝。 – lvnt 2014-11-07 12:39:30