I am doing an android application , i want to get data from activity to a service and then this service wil make a notification and send datat to another activity and the user can see the result in activity ? please hep me
Asked
Active
Viewed 1,443 times
-4
-
3Have you tried anything yet? – fweigl Apr 06 '15 at 18:33
-
Take a look at this [question](http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging) – Ibrahim Disouki Apr 06 '15 at 18:46
-
I want to get the data first from activity A to service S and send data to activity D? – Yh Ghi Apr 06 '15 at 19:18
1 Answers
0
Write this in Activity from where you want to send data:(msg is data you want to send)
Intent myintent = new Intent("package name of class where you want to send");
myintent.putExtra("message", msg);
myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//You might need this
getApplicationContext().sendBroadcast(myintent);
In onCreate() of your receiver activity : Add this :(anywhere after setting content view)
getApplicationContext().registerReceiver(broadcastReceiver, new IntentFilter("package name of current class i.e receiver"));
Add this in class :
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Data received is : " + intent.getStringExtra("message"));
//Make a Notification here
}
};
Also(In receiver Activity) :
@Override
public void onResume() {
super.onResume();
getApplicationContext().registerReceiver(broadcastReceiver, new IntentFilter("package name of current class i.e receiver"));
}
protected void onPause() {
super.onPause();
getApplicationContext().unregisterReceiver(broadcastReceiver);
}

Nikhil Verma
- 1,777
- 1
- 20
- 41
-
-
-
Take a look at this [question](http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging) – Ibrahim Disouki Apr 06 '15 at 19:02