2015-08-08 59 views
1

我有一個已經被定義爲一個服務如下Android的 - 活動的通知點擊重新啓動(該通知已被打開前臺服務)

public class ForegroundService extends Service { 
private static final String LOG_TAG = "ForegroundService"; 

@Override 
public void onCreate() { 
    super.onCreate(); 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    if(intent != null) { 
     if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) { 
      Log.i(LOG_TAG, "Received Start Foreground Intent "); 
      Intent notificationIntent = new Intent(this, DashboardActivity.class); 
      notificationIntent.setAction(Constants.ACTION.MAIN_ACTION); 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

      PendingIntent pendingIntent = PendingIntent.getActivity(getApplication(), 
        0, 
        notificationIntent, 
        PendingIntent.FLAG_NO_CREATE); 

      Intent previousIntent = new Intent(this, ForegroundService.class); 
      previousIntent.setAction(Constants.ACTION.PREV_ACTION); 
      PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, 
        previousIntent, 0); 

      Intent playIntent = new Intent(this, ForegroundService.class); 
      playIntent.setAction(Constants.ACTION.PLAY_ACTION); 
      PendingIntent pplayIntent = PendingIntent.getService(this, 0, 
        playIntent, 0); 

      Intent nextIntent = new Intent(this, ForegroundService.class); 
      nextIntent.setAction(Constants.ACTION.NEXT_ACTION); 
      PendingIntent pnextIntent = PendingIntent.getService(this, 0, 
        nextIntent, 0); 

      Bitmap icon = BitmapFactory.decodeResource(getResources(), 
        R.drawable.art_background); 

      Notification notification = new NotificationCompat.Builder(this) 
        .setContentTitle("Truiton Music Player") 
        .setTicker("Truiton Music Player") 
        .setContentText("My Music") 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setLargeIcon(
          Bitmap.createScaledBitmap(icon, 128, 128, false)) 
        .setContentIntent(pendingIntent) 
        .setOngoing(true) 
        .addAction(android.R.drawable.ic_media_previous, 
          "Previous", ppreviousIntent) 
        .addAction(android.R.drawable.ic_media_play, "Play", 
          pplayIntent) 
        .addAction(android.R.drawable.ic_media_next, "Next", 
          pnextIntent).build(); 
      startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, 
        notification); 
     } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) { 
      Log.i(LOG_TAG, "Clicked Previous"); 
     } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) { 
      Log.i(LOG_TAG, "Clicked Play"); 
     } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) { 
      Log.i(LOG_TAG, "Clicked Next"); 
     } else if (intent.getAction().equals(
       Constants.ACTION.STOPFOREGROUND_ACTION)) { 
      Log.i(LOG_TAG, "Received Stop Foreground Intent"); 
      stopForeground(true); 
      stopSelf(); 
     } 
    } 
    return START_STICKY; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Log.i(LOG_TAG, "In onDestroy"); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // Used only in case of bound services. 
    return null; 
} 
} 

我已經定義在我的清單

android:launchMode="singleTask" 
以下

而且

這是多麼我開始服務

private void showNotificationService() { 
    Intent startIntent = new Intent(DashboardActivity.this, ForegroundService.class); 
    startIntent.setAction(Constants.ACTION.STARTFOREGROUND_ACTION); 
    startService(startIntent); 
} 

所以當我點擊通知應用程序去onCreate,但我希望應用程序恢復到通知出現之前它是在什麼狀態。任何線索我做錯了什麼?

而且我使用這個博客前臺服務提供的代碼

http://www.truiton.com/2014/10/android-foreground-service-example/

回答

0

類別添加到您的notificationIntent

請參閱下面的代碼

notificationIntent.addCategory("android.intent.category.LAUNCHER"); 
0

我做了一些修改你的代碼。問題在於你在創建新的Intent時。

public class ForegroundService extends Service { 
    private static final String LOG_TAG = "ForegroundService"; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     if(intent != null) { 
      if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) { 
       Log.i(LOG_TAG, "Received Start Foreground Intent "); 
       Intent notificationIntent = new Intent(this, DashboardActivity.class); 
       notificationIntent.setAction(Constants.ACTION.MAIN_ACTION); 
       notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 


       PendingIntent pendingIntent = PendingIntent.getActivity(getApplication(), 
         0, 
         notificationIntent, 
         PendingIntent.FLAG_UPDATE_CURRENT); 

       Intent previousIntent = new Intent(this, ForegroundService.class); 
       previousIntent.setAction(Constants.ACTION.PREV_ACTION); 
       PendingIntent ppreviousIntent = PendingIntent.getService(this, 0, 
         previousIntent, 0); 

       Intent playIntent = new Intent(this, ForegroundService.class); 
       playIntent.setAction(Constants.ACTION.PLAY_ACTION); 
       PendingIntent pplayIntent = PendingIntent.getService(this, 0, 
         playIntent, 0); 

       Intent nextIntent = new Intent(this, ForegroundService.class); 
       nextIntent.setAction(Constants.ACTION.NEXT_ACTION); 
       PendingIntent pnextIntent = PendingIntent.getService(this, 0, 
         nextIntent, 0); 

       Bitmap icon = BitmapFactory.decodeResource(getResources(), 
         R.drawable.art_background); 

       Notification notification = new NotificationCompat.Builder(this) 
         .setContentTitle("Truiton Music Player") 
         .setTicker("Truiton Music Player") 
         .setContentText("My Music") 
         .setSmallIcon(R.drawable.ic_launcher) 
         .setLargeIcon(
           Bitmap.createScaledBitmap(icon, 128, 128, false)) 
         .setContentIntent(pendingIntent) 
         .setOngoing(true) 
         .addAction(android.R.drawable.ic_media_previous, 
           "Previous", ppreviousIntent) 
         .addAction(android.R.drawable.ic_media_play, "Play", 
           pplayIntent) 
         .addAction(android.R.drawable.ic_media_next, "Next", 
           pnextIntent).build(); 
       startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, 
         notification); 
      } else if (intent.getAction().equals(Constants.ACTION.PREV_ACTION)) { 
       Log.i(LOG_TAG, "Clicked Previous"); 
      } else if (intent.getAction().equals(Constants.ACTION.PLAY_ACTION)) { 
       Log.i(LOG_TAG, "Clicked Play"); 
      } else if (intent.getAction().equals(Constants.ACTION.NEXT_ACTION)) { 
       Log.i(LOG_TAG, "Clicked Next"); 
      } else if (intent.getAction().equals(
        Constants.ACTION.STOPFOREGROUND_ACTION)) { 
       Log.i(LOG_TAG, "Received Stop Foreground Intent"); 
       stopForeground(true); 
       stopSelf(); 
      } 
     } 
     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.i(LOG_TAG, "In onDestroy"); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // Used only in case of bound services. 
     return null; 
    } 
    } 

我已經定義在下面我的清單

android:launchMode="singleInstance" 

我可以爲你工作。我做到了,我工作了!如果你需要別的或更多的幫助,我可用。祝你今天愉快!