Back to project page CloudCopyPaster.
The source code is released under:
Copyright (c) 2014, Dima Moiseev Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of s...
If you think the Android project CloudCopyPaster listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.newbilius.WinCopyPast; // ww w. j a v a 2s. co m import android.app.Activity; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.AsyncTask; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.gcm.GoogleCloudMessaging; import javax.inject.Inject; import java.io.IOException; public class PushNotification { private Activity activity; private String senderId; public OnComplete Complete; @Inject ConnectivityManager connectivityManager; @Inject Options options; Net net=new Net(); private boolean HaveInternet(){ NetworkInfo nInfo = connectivityManager.getActiveNetworkInfo(); return nInfo != null && nInfo.isConnected(); } public final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000; public boolean checkPlayServices() { int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity); if (resultCode != ConnectionResult.SUCCESS) { if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { if (resultCode==ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){ return true; } try{ if (activity!=null){ GooglePlayServicesUtil.getErrorDialog(resultCode, activity,PLAY_SERVICES_RESOLUTION_REQUEST).show(); } }catch (Error e){ Log.Error(e); } } return false; } return true; } String tmpId; int repeatCounter=0; public void register(Activity activity,String senderId,String notificationId,OnComplete complete){ this.activity=activity; this.senderId=senderId; this.Complete =complete; Complete.Process(""); if (checkPlayServices()) { if (notificationId.isEmpty()) { repeatCounter=0; RegisterPushInTask registerInBackgroundTask=new RegisterPushInTask(); registerInBackgroundTask.execute(); } } } class RegisterPushInTask extends AsyncTask<Void,Void,Void> { String msg=""; String pushRegId=""; boolean ok=false; @Override protected Void doInBackground(Void... params) { Thread.currentThread().setName("RegisterPushInTask"); try { GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(activity); pushRegId = gcm.register(senderId); msg = "push Device registered, registration ID=" + pushRegId; } catch (IOException ex) { msg = "push Error :" + ex.getMessage(); } if (!msg.contains("Error") && !msg.contains("SERVICE_NOT_AVAILABLE")){ ok=SaveToServer(pushRegId); } return null; } @Override protected void onPostExecute(Void aVoid) { if (msg.contains("SERVICE_NOT_AVAILABLE")){ repeatCounter++; if (repeatCounter<10){ if (HaveInternet()){ RegisterPushInTask registerInBackgroundTask=new RegisterPushInTask(); registerInBackgroundTask.execute(); Log.Error(msg+" ??? ?? ??????????!"); }else{ Complete.Error("???? ?????????."); repeatCounter=0; } }else{ Complete.Error("????-???????? ?? ????????."); repeatCounter=0; } }else{ if (msg.contains("Error")){ Log.Error(msg); Complete.Error("?????? ????????????: "+msg); }else{ if (ok){ Complete.Complete(); }else{ Complete.Error(net.ErrorText); } } } } } private boolean SaveToServer(String pushRegId){ boolean result; result=net.Execute(options.getLogin(),options.getPass(),pushRegId,activity.getBaseContext()); if (result){ options.setNotificationId(pushRegId); } return result; } }