2013-03-22 16 views
16

我在我的Android應用程序中執行由GCM觸發的推送通知。我想要顯示通知,如果應用程序正在警報框中運行,如果應用程序未運行意味着不在前臺只顯示狀態欄通知,您如何確定應用程序是否正在運行並且位於前臺?這是可能顯示這樣的通知。現在我正在使用此代碼顯示狀態欄推送通知當應用程序在前臺運行時在警告框中顯示推送通知

NotificationManager notificationManager = (NotificationManager) 
       context.getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = new Notification(icon, message, when); 

     String title = context.getString(R.string.app_name); 

     Intent notificationIntent = new Intent(context, BottomActivity.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_SOUND; 

     //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3"); 

     // Vibrate if vibrate is enabled 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 
+1

,如果這是因爲它是過時和谷歌說:「C2DM將不接受新用戶,你應該使用GCM,而不是C2DM一個新的項目,它將不授予新配額「 – NickT 2013-03-22 15:34:48

+0

好的,謝謝,我將使用GCM並編輯我的問題 – 2013-03-22 15:38:40

+0

@Jithu我也需要在我的一個應用程序中做同樣的事情,您是否找到了適當的解決方案和思路在此發佈相同,這將是一個很好的幫助謝謝。 – 2015-09-03 04:14:00

回答

1

您有多少活動?如果只有一個,那麼在onResume()和onPause()(當它處於活動狀態時)(AKA處於活動狀態)之間保存一個單例引用很容易,如果是,則從接收方調用活動。如果你有很多活動,你可以通過擁有一個共同的基類來達到類似的邏輯。像這樣:

class MyActivity: Activity 
{ 
    static private MyActivity _Current = null; 

    protected void onResume() //Activated 
    { 
     super.onResume(); 
     _Current = this; 
    } 

    protected void onPause() //Deactivated 
    { 
     super.onPause(); 
     _Current = null; 
    } 

    //This is for the receiver to call 
    static public PopAlert() 
    { 
     if(_Current != null) 
     { 
      new AlertDialog.Builder(_Current) 
       .setMessage("Hello world") 
       //More alert setup; use _Current as a context object 
       .create().show(); 
     } 
    } 
} 
2

您可以創建一個新的活動和主題,如對話框和檢查前景活動。檢查下面的代碼。

  ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
     List<RunningTaskInfo> services = activityManager 
       .getRunningTasks(Integer.MAX_VALUE); 
     boolean isActivityFound = false; 

     if (services.get(0).topActivity.getPackageName().toString() 
       .equalsIgnoreCase(context.getPackageName().toString())) { 
      isActivityFound = true; 
     } 

     if (isActivityFound) { 
      resultIntent = new Intent(context, AlertDialogNotification.class); 
      resultIntent.putExtra(EXTRA_ALERT_MESSAGE_BEAUTIFUL, "anyMessage"); 
      resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(resultIntent); 
     } 

在清單中,將活動的主題設置爲對話框。希望能幫助到你。讓我們知道你是否有問題。

0

,請嘗試以下

public static boolean isAppIsInBackground(Context context) { 

     boolean isInBackground = true; 
     ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); 
     if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) { 
      List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses(); 
      for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) { 
       if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { 
        for (String activeProcess : processInfo.pkgList) { 
         if (activeProcess.equals(context.getPackageName())) 
          isInBackground = false; 
        } 
       } 
      } 
     } else { 
      List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1); 
      ComponentName componentInfo = taskInfo.get(0).topActivity; 
      if (componentInfo.getPackageName().equals(context.getPackageName())) { 
       isInBackground = false; 
      } 
     } 
只是爲了信息
相關問題