2013-03-22 16 views
16

में चल रहा है तो अलर्ट बॉक्स में पुश अधिसूचना प्रदर्शित करें, मैं अपने एंड्रॉइड ऐप में पुश अधिसूचना कर रहा हूं जो जीसीएम द्वारा ट्रिगर किया गया है। यदि एप नहीं चल रहा है, तो ऐप अधिसूचना प्रदर्शित करना चाहता है, यदि ऐप नहीं चल रहा है, तो अग्रभूमि में नहीं है, बस स्टेटस बार अधिसूचना प्रदर्शित करें, आप कैसे निर्धारित करते हैं कि ऐप चल रहा है और अग्रभूमि में है? इस तरह की अधिसूचना दिखाना संभव है। अब मैं इस स्थिति का उपयोग शो स्टेटस बार पुश अधिसूचनाजब एप फोरग्राउंड

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

+0

ठीक है धन्यवाद, मैं जीसीएम का उपयोग करूंगा और अपने प्रश्न को संपादित करने जा रहा हूं –

+0

@ जिथू मुझे अपने ऐप्स में से एक में भी ऐसा करने की ज़रूरत है, क्या आपको यहां एक उचित समाधान और दिमाग पोस्ट करना है, यह एक बड़ी मदद होगी धन्यवाद। –

उत्तर

1

आपके पास कितनी गतिविधियां हैं? यदि केवल एक है, तो इसे सिंगलटन संदर्भ को रेज़्यूम() और ऑनपोज() (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; 
      } 
     } 
बस जानकारी के लिए
संबंधित मुद्दे