-4

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

Yh Ghi
  • 25
  • 3

1 Answers1

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