2

I'm very new to android programming and working on an Android app.

I have 2 teams - A and B.

I have a Activity A for team A where I have 3 ImageView to place images from Server. Each time any user from team B uploads images to the server, I want the images to be downloaded from Server and placed on ImageView in Activity A with a notification saying new images are here.

Currently, I'm able to download images from Server and able to place them on ImageView manually, but I want a Service for this which will run constantly for this.

I googled a lot but couldn't find any good example for this process. Please help me with this.

I want to use Service

**I'm using Volley for networking

Thanks.

W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • You can use GCM or Firebase for this. – Shubham Aug 20 '16 at 09:44
  • @Shubham Notification is not a big deal, I'm stuck with Service. Need to know how can I implement service which can download images for me and place those in ImageView. –  Aug 20 '16 at 09:51

4 Answers4

0

Just use LocalBroadcastManager when you get push notification to update activity. Still You need service then please try with below link.

https://gist.github.com/Antarix/8131277

Amit
  • 409
  • 1
  • 5
  • 12
0

Hey use firebase for the notification to get the update of the image. Once you get the notification trigger a broadcast and send the data to your Activity to change the UI. In your case service should not be used because you are getting the data from the Service through Notification. Please let me know I i can further help you. Good you are using Volley for network operation. Use Picasso for image loading

compile 'com.squareup.picasso:picasso:2.5.0'

Sagar Gangawane
  • 1,985
  • 1
  • 12
  • 22
  • Notification is working locally. I need to write code for Service to download the images from Server, whenever any new images uploaded. –  Aug 20 '16 at 09:55
  • Ok ..I got it..then what a issue.write a service using startservice()method.Run the service on new thread the make the webserive call to get the data.use the broadcast to send the data to activity. – Sagar Gangawane Aug 20 '16 at 09:59
  • Now you got my question. Ok! so now I'm calling web service under onCreateCommand. Now help me sending data to Activity via Broadcast. **No knowledge of Broadcast –  Aug 20 '16 at 10:04
  • hey check this link how to send the data from the broadcast to activity. [see this](http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager) If you think i helped you vote me.Use dynamic broadcast receiver. – Sagar Gangawane Aug 20 '16 at 10:07
  • One more question. *Will this service under onCreateCommand will run continuously. –  Aug 20 '16 at 10:12
  • in this method call the webserice /** The service is starting, due to a call to startService() */ @Override public int onStartCommand(Intent intent, int flags, int startId) { return START_STICKY. //this will call the OS to start the service again. } yes until you stop itsel using the stopself() method. – Sagar Gangawane Aug 20 '16 at 10:15
0

Simple create a class and extend is by

Service

and it onStartCommmand write volley code to download the image and in volley's

onResponse

method send a

local broadcast 

with this broadcast update the UI

Shubham
  • 521
  • 4
  • 11
0
public class LocalBroadcastExampleActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.log_list);

        Button buttonStartService = (Button)findViewById(R.id.button_ok);
        buttonStartService.setOnClickListener(new View.OnClickListener(){
                 @Override
                 public void onClick(View v) {
                // TODO Auto-generated method stub
                //Register MessageService in Manifest to work
               startService(newIntent(LocalBroadcastExampleActivity.this, MessageService.class));
            }
        });

    }

    @Override
    protected void onPause() {
        // Unregister since the activity is paused.
        LocalBroadcastManager.getInstance(this).unregisterReceiver(
            mMessageReceiver);
        super.onPause();
    }

    @Override
    protected void onResume() {
        // Register to receive messages.
        // We are registering an observer (mMessageReceiver) to receive Intents
        // with actions named "custom-event-name".
        LocalBroadcastManager.getInstance(this).registerReceiver(
            mMessageReceiver, new IntentFilter("custom-event-name"));
        super.onResume();
    }

    // Our handler for received Intents. This will be called whenever an Intent
    // with an action named "custom-event-name" is broadcasted.
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            // Get extra data included in the Intent
            String message = intent.getStringExtra("message");
            Log.d("receiver", "Got message: " + message);
        }
    };
}

Now Service class

public class MessageService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        sendMessage();
        return super.onStartCommand(intent, flags, startId);
    }

    // Send an Intent with an action named "custom-event-name". The Intent
    // sent should
    // be received by the ReceiverActivity.
    private void sendMessage() {
        Log.d("sender", "Broadcasting message");
        Intent intent = new Intent("custom-event-name");
        // You can also include some extra data.
        intent.putExtra("message", "This is my message!");
        LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    }

}
Shubham
  • 521
  • 4
  • 11