简体   繁体   中英

How to handle GCM from an Android service?

I would like to do (this for example): 1.- GCM arrives 2.- Upload some data to my server from a service running in the background (I don't wanna show any alerts or play any sounds when GCM arrives), perhaps upload the device current location to a server.

I want to do this from Phonegap so I guess I'm going to write a plugin or find a plugin that communicates a service with the javascript source. I don't need Phonegap to receive any kind of messages from the service, but it's the service itself that is going to upload the data that I need. In other words, I'm gonna use Phonegap as UI and start the service that will do all the work. I also need the service to start whenever the user turns pn his/her phone (I guess that comes from service configuration) but I think that the user will have to launch the app at least once (which I think its normal, right?).

My problem right now is that I don't know how to tell my service to do 'something' when a GCM arrives. Any advice would be appreciated.

It is very simple , whenever you recieve a GCM message in your service in

@Override
protected void onMessage(final Context arg0, final Intent arg1) {
    String arr=arg1.getStringExtra("message");
    arr2=arr.split("-");

    Handler h = new Handler(Looper.getMainLooper());
       h.post(new Runnable(){

            public void run() {
                // TODO Auto-generated method stub
            Toast.makeText(arg0, arr2[0], Toast.LENGTH_LONG).show();
               if(arr2[0]=="desired value"){
               //launch a method in your service that post data to your server using requests
                  }
    }         
        });
}

Now declare a method in ayour service which gathers all the location data and post it to your server, call that method in void run().

I hope this solution might help you out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM