2013-04-04 7 views
10

मैं सोच रहा हूं कि जीसीएम से onMessage() प्राप्त करते समय मेरा एप्लिकेशन खुला है या वर्तमान में उपयोगकर्ता के लिए दृश्यमान है या नहीं। सबसे पहले, मैं बस अपने boolean isVisible का उपयोग कर रहा था, लेकिन फिर मुझे एहसास हुआ कि यह विश्वसनीय नहीं है, क्योंकि यदि ऐप खुला नहीं है, तो उस ध्वज को एक्सेस करने के लिए मैं जिस ऑब्जेक्ट का उपयोग करता हूं वह null है। हालांकि यह स्वयं यह देखने के लिए इस्तेमाल किया जा सकता है कि ऐप खुला है या नहीं, यह थोड़ा गन्दा लगता है। क्या सिस्टम स्तर से एंड्रॉइड में किसी भी तरह से यह जांचने का कोई तरीका है कि एप्लिकेशन वर्तमान में खुला है या नहीं, और यदि उपयोगकर्ता ऐप देख रहा है? ध्यान रखें कि एक ऐप तकनीकी रूप से चल रहा है, लेकिन दिखाई नहीं दे रहा है, क्योंकि उपयोगकर्ता ने हाल ही में पृष्ठभूमि में इसे "होम" बटन दबाया है।जांचें कि क्या जीसीएम ऑन मैसेज इवेंट के दौरान ऐप खुला है?

@Override 
protected void onMessage(Context arg0, Intent arg1) { 
    String turn = intent.getExtras().getString("turn"); 
    if (turn.equals("yours"){ 
     if (/*app is open*/){ <------------------ what can go here? 
      // dont generate a notification 
      // display something in the game instead 
     } 
     else{ 
      // generate notification telling player its their turn 
     } 
    } 
} 

उत्तर

2

उपयोग SharedPreferences बूलियन isVisible बचत, और आप एक डिफ़ॉल्ट मान जोड़ सकते हैं जब आप वरीयता से मूल्य मिलता है।

SharedPreferences settings = context.getSharedPreferences("NAME_XXX", Activity.MODE_PRIVATE); 
settings.getBoolean("visible", false); 
1

मैं हमेशा जो करता हूं वह वर्तमान गतिविधि का संदर्भ है।

मैंने वर्तमान गतिविधि को प्रत्येक पर रेज़्यूम में सेट किया है और इसे प्रत्येक पर रोक दिया है।

यदि वर्तमान गतिविधि शून्य है तो ऐप खुला नहीं है। यदि यह शून्य नहीं है तो आप देख सकते हैं कि सही गतिविधि खुली है या उसे उस गतिविधि में पहुंचाती है या नहीं।

GCMIntentService:

public static Activity currentActivity; 
public static final Object CURRENTACTIVIYLOCK = new Object(); 

    @Override 
protected void onHandleIntent(Intent intent) { 
    synchronized(CURRENTACTIVIYLOCK) { 
     if (currentActivity != null) { 
      if (currentActivity.getClass() == CorrectActivity.class) { 
       CorrectActivity act = (CorrectActivity)currentActivity; 
       act.runOnUiThread(new Runnable() { 
        public void run() { 
         // Notifiy activity 
        } 
       }); 
      } else { 
       // show notification ? 
      } 
     } else { 
      // show notification 
     } 
    } 
} 

CorrectActivity:

@Override 
    protected void onResume() { 
     synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { 
       GCMIntentService.currentActivity = this; 
      } 
     } 
     super.onResume(); 
    } 

@Override 
    protected void onPause() { 
     synchronized (GCMIntentService.CURRENTACTIVITYLOCK) { 
      GCMIntentService.currentActivity = null; 
     } 
     super.onPause(); 
    } 
+2

मेमोरी लीक के बारे में सावधान रहें! – rciovati

+0

@rciovati रिसाव कहां है? – Klaasvaak

+0

क्षमा करें, मैंने गतिविधि को चालू होने पर 'currentActivity' के संदर्भ में 'null'' पर सेट नहीं किया है। – rciovati

17

मैं आदेश प्रसारण का प्रयोग करेंगे कि क्या करना है।

अपने onMessage विधि में:

Intent responseIntent = new Intent("com.yourpackage.GOT_PUSH"); 
sendOrderedBroadcast(responseIntent, null); 

अपनी गतिविधि में:

public class YourActivity extends Activity { 

    final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      //Right here do what you want in your activity 
      abortBroadcast(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //..... 
    } 

    @Override 
    protected void onPause() { 
     unregisterReceiver(mBroadcastReceiver); 
     super.onPause(); 
    } 

    @Override 
    protected void onResume() { 
     IntentFilter filter = new IntentFilter("com.yourpackage.GOT_PUSH"); 
     filter.setPriority(2); 
     registerReceiver(mBroadcastReceiver, filter); 
     super.onResume(); 
    } 
} 

अन्य BroadcastReceiver

public class SecondReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //In this receiver just send your notification 
    } 

} 

मैनिफ़ेस्ट:

<activity 
    android:name=".YourActivity" 
    android:label="@string/app_name"> 
    <intent-filter> 
     <action 
      android:name="android.intent.action.MAIN" /> 
     <category 
      android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

<receiver 
    android:name=".SecondReceiver"> 
    <intent-filter 
     android:priority="1"> 
     <action 
      android:name="com.yourpackage.GOT_PUSH" /> 
    </intent-filter> 
</receiver> 

मूल रूप से onMessage विधि में आप एक इरादा भेजते हैं जिसे ब्रॉडकास्ट रिसीवर द्वारा YourActivity के अंदर पंजीकृत किया जाता है यदि यह चल रहा है और अग्रभूमि में है, अन्यथा यह SecondReceiver द्वारा प्राप्त किया जाता है।

+2

धन्यवाद! 'इरादा प्रतिक्रियाइन्टेंट = नया इरादा ("com.yourpackage.GOT_PUSH") के बाद लाइन पर;' मैंने 'प्रतिक्रियाइन्टेंट। पुटएक्स्ट्रेस (इरादा) जोड़ा;' इसलिए मेरे अतिरिक्त प्रतिलिपि बनाये जाएंगे। ' – shane

0

बात है कि मेरे लिए काम किया:

बनाएँ अंतिम कक्षा स्थिरांक, इसके अंदर, स्थिर varaiable बनाएँ:

public final class Constants{ 
public static AppCompatActivity mCurrentActivity; 
} 

अब, अपने activties की फिर से शुरू पर प्रत्येक पर कहते हैं:

@Override 
    protected void onResume() { 
     super.onResume(); 
     Constants.mCurrentActivity = this; 
    } 

अधिसूचना प्राप्त करते समय, जांचें कि वर्तमान गतिविधि शून्य है, अगर इसकी शून्य, एप्लिकेशन खोला नहीं गया है, यदि गतिविधि शून्य नहीं है, तो आप चीजों की जांच कर सकते हैं:

if(Constants.mCurrentActivity instanceof MainActivity){ 
    ((MainActivity) Constants.mCurrentActivity).yourPublicMethodOrStaticObject; 
} 
संबंधित मुद्दे