2017-04-13 9 views
8

मैं वर्तमान में एंड्रॉइड प्रोजेक्ट पर काम कर रहा हूं।सेवा से चलने वाली गतिविधि में कॉल विधि

अब तक, मैं लागू कर दिया है Firebase, विशेष रूप से FirebaseInstanceIdService और FirebaseMessagingService:

:

public class FirebaseIDService extends FirebaseInstanceIdService { 
private static final String TAG = "FirebaseIDService"; 

private Context context; 

@Override 
public void onTokenRefresh() { 
    context = getApplicationContext(); 
    // Get updated InstanceID token. 
    String refreshedToken = FirebaseInstanceId.getInstance().getToken(); 
    Log.e(TAG, "Refreshed token: " + refreshedToken); 

    SharedPreferences sharedPreferences = context.getSharedPreferences("token", context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putString("token", refreshedToken); 
    editor.commit(); 

    sendRegistrationToServer(refreshedToken); 
} 

/** 
* Persist token to backend. 
* @param token The new token. 
*/ 
private void sendRegistrationToServer(String token) { 
    SendRegistrationKey task = new SendRegistrationKey(context); 
    task.execute(token); 
} 

}

public class MessagingService extends FirebaseMessagingService { 

private static final String TAG = "MsgService"; 

/** 
* Called when message is received. 
* 
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging. 
*/ 
@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    Log.e(TAG, "From: " + remoteMessage.getFrom()); 

    // Check if message contains a data payload. 
    if (remoteMessage.getData().size() > 0) { 
     Log.e(TAG, "Message data payload: " + remoteMessage.getData()); 
    } 

    // Check if message contains a notification payload. 
    if (remoteMessage.getNotification() != null) { 
     Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 

     sendNotification(remoteMessage); 
    } 

    // Also if you intend on generating your own notifications as a result of a received FCM 
    // message, here is where that should be initiated. See sendNotification method below. 
} 
// [END receive_message] 

private void sendNotification(RemoteMessage remoteMessage) { 
    RemoteMessage.Notification notification = remoteMessage.getNotification(); 
    PushNotificationManager PNManager = PushNotificationManager.getInstance(getApplicationContext()); 
    PNManager.buildNotification(notification.getTitle(), notification.getBody()); 
} 

क्या मैं हासिल करना चाहते हैं निम्नलिखित है जब ऐप पृष्ठभूमि में होता है, तो मैं अधिसूचना केंद्र में बस एक साधारण अधिसूचना चाहता हूं। (यह पहले से ही काम कर रहा है)

लेकिन, जब एप्लिकेशन को अग्रभूमि में और वर्तमान में चल रहा है, मैं एक अलग व्यवहार करना चाहते हैं: मैं पुश अधिसूचना उपभोग करते हैं और बदले में एक चेतावनी दिखाना चाहते हैं।

लेकिन मेरा सवाल यह है कि: मैं सेवा से चल रही गतिविधि के साथ कैसे बातचीत कर सकता हूं या इच्छित व्यवहार को प्राप्त करने का सही तरीका क्या है? थर एक साधारण आत्मा होना चाहिए, है ना?

अग्रिम धन्यवाद इस के साथ

+0

सेवा से sendBroadcast, से और गतिविधि में

context.registerReceiver (myBroadcastReceiver, new IntentFilter ("CUSTOM_ACTION")); private final BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive (Context context, Intent intent) { //Do something } }; 

2. संदेश प्रसारण अपनी गतिविधि में कस्टम प्रसारण रिसीवर रजिस्टर क्षुधा चलाने के लिए mothod जांच से ऊपर प्रसारण – X3Btel

उत्तर

1

कोशिश:

सभी चल रहे एप्लिकेशन के लिए इस विधि की जांच, और सही या गलत लौटने कि मौजूदा एप्लिकेशन को पृष्ठभूमि राज्य में या अग्रभूमि स्थिति में है।

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; 
      } 
     } 

     return isInBackground; 
    } 
+0

के लिए सुनने , और वास्तविक या गलत वापस आना चाहे वर्तमान ऐप पृष्ठभूमि स्थिति में है या अग्रभूमि राज्य –

+0

में कौन सी जानकारी और कहां है? –

4

लिखने आवेदन वर्ग में इस कोड

public Context currentactvity = null; 
public Context getCurrentactvity() { 
    return currentactvity; 
} 

public void setCurrentactvity(Context currentactvity) { 
    this.currentactvity = currentactvity; 
} 

लिखने प्रत्येक गतिविधि onresume विधि में और onpause विधि में इस कोड को अशक्त सेट

// in onresume 
MyApplication.getInstance().setCurrentactvity(this); 

// in onpause 
MyApplication.getInstance().setCurrentactvity(null); 
अब

आप सेवा वर्ग से गतिविधि विधि कॉल कर सकते हैं

if (MyApplication.getInstance().getCurrentactvity() != null && MyApplication.getInstance().getCurrentactvity() instanceof youractivityname) { 
      ((youractivityname) MyApplication.getInstance().getCurrentactvity()).youmethodname(parameter); 

     } 
+0

यह बढ़िया है, यही वह था जो मैं देख रहा था (y) –

2

मैं सेवा से चल रही गतिविधि के साथ कैसे बातचीत कर सकता हूं या इच्छित व्यवहार को प्राप्त करने का सही तरीका क्या है?

इवेंट बस (यानी GreenRobot's या) का उपयोग करने का सबसे अच्छा तरीका होगा। आपकी गतिविधि श्रोता onResume() (और onPause() में deregisters) में श्रोता रजिस्टर करता है और आपकी सेवा बस समय आने पर संदेश प्रसारित करती है।

मुख्य लाभ यह है कि आप दोनों तत्वों (सेवा और गतिविधि) को पूरी तरह से अलग रखते हैं। आप सबसे खराब संभावित समाधान से भी बचते हैं - गतिविधि की विधियों को सीधे कॉल करना।

पता लगाने के लिए जब आप पृष्ठभूमि में हैं या नहीं, यह UTILIS सबसे अच्छा है आवेदन के GreenRobot'sActivityLifecycleCallbacks - रिपोर्ट करने के लिए है कि वहाँ कि से है के रूप में पूरी तरह से कोई लाभ गतिविधियों के लिए मजबूर कर के कोई मतलब नहीं है।

0

1 का प्रयास करें। आपकी सेवा

Intent intent = new Intent ("CUSTOM_ACTION"); 
context.sendBroadcast (intent) 
+1

लागू प्रसारण एंड्रॉइड ओ –

+0

+1 में काम नहीं करेगा। अद्यतन के लिए धन्यवाद। उत्तर अपडेट किया गया। "मैनिफेस्ट में रिसीवर घोषित करने के बजाय Context.registerReceiver() को कॉल करके रनटाइम पर रिसीवर बनाएं।" https://developer.android.com/preview/features/background.html#broadcasts – Pehlaj

संबंधित मुद्दे