Java tutorial
/*** Copyright (c) 2010 CommonsWare, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // //This class is a service as explained in the android manifest for this application package com.avanade; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; import java.net.Socket; import java.net.UnknownHostException; import java.util.List; import org.apache.http.client.methods.HttpGet; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.RunningAppProcessInfo; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.util.Log; import android.widget.Toast; import com.google.android.c2dm.C2DMBaseReceiver; import com.google.android.c2dm.C2DMessaging; public class C2DMReceiver extends C2DMBaseReceiver implements Serializable { /** * */ private static final long serialVersionUID = 1L; InputStream in; OutputStream os; ObjectInputStream oin; ObjectOutputStream oos; private ByteArrayInputStream is; public static String numberNoti; public C2DMReceiver() { super("this.is.not@real.biz"); } @Override public void onRegistrered(Context context, String registrationId) { Log.w("C2DMReceiver-onRegistered", registrationId); String urlString = "https://mobileapi.whatsnewat.com/WNADataService.svc/RegisterAndroidDevice?deviceToken='" + registrationId + "'&deviceID='" + LoginScreen.androidDevice_id + "'"; HttpGet httpGet = new HttpGet(urlString); // set header parameters with user-provided credentials LoginScreen.httpFlag = 1; HttpGetRequest token = new HttpGetRequest(); httpGet.setHeader(Constants.emailId, LoginScreen.statEmailId); httpGet.setHeader(Constants.companyKey, LoginScreen.statCompKey); // obtain response XML from the server String xml = token.performGetRequest(httpGet); Intent dBUpdateIntent = new Intent(getBaseContext(), DatabaseUpdate.class); dBUpdateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); dBUpdateIntent.putExtra("DeviceId", registrationId); getApplication().startActivity(dBUpdateIntent); // Intent dialogIntent = new Intent(getBaseContext(), NextPage.class); // dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // getApplication().startActivity(dialogIntent); } @Override public void onUnregistered(Context context) { Log.w("C2DMReceiver-onUnregistered", "got here!"); // String urlString = getResources().getString( // R.string.WhatsNewAtDataServiceUnRegisterURL); String urlString = "https://mobileapi.whatsnewat.com/WNADataService.svc/UnRegisterAndroid?deviceID='" + LoginScreen.androidDevice_id + "'"; HttpGet httpGet = new HttpGet(urlString); LoginScreen.httpFlag = 1; HttpGetRequest token = new HttpGetRequest(); httpGet.setHeader(Constants.emailId, LoginScreen.statEmailId); httpGet.setHeader(Constants.companyKey, LoginScreen.statCompKey); // obtain response XML from the server String xml = token.performGetRequest(httpGet); Intent dBUpdateIntent = new Intent(getBaseContext(), DatabaseUpdate.class); dBUpdateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); dBUpdateIntent.putExtra("unregister", true); getApplication().startActivity(dBUpdateIntent); } @Override public void onError(Context context, String errorId) { Log.w("C2DMReceiver-onError", errorId); } @Override protected void onMessage(Context context, Intent intent) { int flag = 0; Log.w("C2DMReceiver", intent.getStringExtra("Count")); String count = intent.getStringExtra("Count"); ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE); List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses(); for (int i = 0; i < procInfos.size(); i++) { if (procInfos.get(i).processName.equals("com.avanade")) { if (count.startsWith("com.avanade")) { count = getIntFromEndOfString(count); numberNoti = count; flag = 1; break; } } } if (flag == 1) { Intent pushUpdateIntent = new Intent(getBaseContext(), PushUpdate.class); pushUpdateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //pushUpdateIntent.putExtra("unregister", true); getApplication().startActivity(pushUpdateIntent); flag = 0; } else { // System.out.println(" the app is not runnning......do something"); //Toast.makeText(getApplicationContext(), "Notification Received", Toast.LENGTH_SHORT).show(); } } /** * Gets the number from the end of a string * @param string the sting to try and get a number from * @return the number from the end of the string, or -1 if the end of the string is not a number * @since 1.0 */ private String getIntFromEndOfString(String string) { // Try getting the number from the end of the string try { String num = string.split(" ")[string.split(" ").length - 1]; return num; } // Couldn't do it, so return -1 catch (Exception e) { System.out.println("Could not get int from end of string"); return "-1"; } } }