Monday 14 January 2013

Push notification with GCM

Include the library of GCM.jar file in your project

public class GCMIntentService extends GCMBaseIntentService {

    @SuppressWarnings("hiding")
    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super("123456");
    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     */
     private static void generateNotification(Context context, String message)
     {
     int icon = R.drawable.icon;
     long when = System.currentTimeMillis();
     NotificationManager notificationManager = (NotificationManager)
     context.getSystemService(Context.NOTIFICATION_SERVICE);
     Notification notification = new Notification(icon, message, when);
     String title = context.getString(R.string.app_name);
     Intent notificationIntent = new Intent(context, ABCCCC.class);
     // set intent so it does not start a new activity
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
     Intent.FLAG_ACTIVITY_SINGLE_TOP);
     PendingIntent intent =
     PendingIntent.getActivity(context, 0, notificationIntent, 0);
     notification.setLatestEventInfo(context, title, message, intent);
     notification.flags |= Notification.FLAG_AUTO_CANCEL;
     notificationManager.notify(0, notification);
     }

    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }



    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        Log.e("GCM", "RECIEVED A MESSAGE");
        Log.e("GCM", "RECIEVED A MESSAGE");
        Log.e("GCM", "RECIEVED A MESSAGE");
        Log.e("GCM", "RECIEVED A MESSAGE");
        Log.e("GCM", "RECIEVED A MESSAGE");
        Log.e("GCM", "RECIEVED A MESSAGE");
        Log.e("GCM", "RECIEVED A MESSAGE");
       
        generateNotification(getApplicationContext(),arg1.getStringExtra("message"));
    }

    @Override
    protected void onRegistered(Context arg0, String regId) {
        // TODO Auto-generated method stub
       
        ConstantData.DEVICETOKEN=regId;
       
        Log.e("MY_APP_TAG", "Registered: " + regId);

    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

}







SamplePushActivity extends Application {
    /** Called when the activity is first created. */
public void onCreate() {
            super.onCreate();
            //setContentView(R.layout.main);
           
             try{

                 //User device id...
                TelephonyManager tManager = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
                ConstantData.DEVICEID = tManager.getDeviceId();

                //        Log.e("Device id is:-", uid);
               
                }catch (Exception e) {
                    // TODO: handle exception
                }
           
            try {
                       
            Log.e("IN GCM_SAMPLE_PUSH","aa gaya");
            GCMRegistrar.checkDevice(this);
            GCMRegistrar.unregister(this);
            Log.e("info","unregistereddd....." + GCMRegistrar.getRegistrationId(this));
            GCMRegistrar.checkManifest(this);
            if (GCMRegistrar.isRegistered(this)) {
                Log.e("info", GCMRegistrar.getRegistrationId(this));
               
            }
            final String regId = GCMRegistrar.getRegistrationId(this);
   
            Log.e("REG_ID", regId);
   
            if (regId.equals("")) {
                GCMRegistrar.register(this, "CLENT SIDE ID");
                Log.e("info", GCMRegistrar.getRegistrationId(this));
               
            } else {
                Log.e("info", "already registered as" + regId);
            }
   
           
            } catch (Exception e) {
                // TODO: handle exception
            }
        }

}


make reciver in your manifest file

receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />

<category android:name="your project name" />
</intent-filter>
</receiver>

permission in manifest file
internet permission

make service in manifest file
<service android:name=".GCMIntentService" />







Tuesday 8 January 2013

how to get accurate speed in android



static float[] dist1 = new float[1];

Location location = null;
static LocationManager locationmanager;

static double  avg_distance = 0.0;

locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

location = locationmanager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locationmanager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if (location == null) {
location = locationmanager
.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
if (location != null) {
pre_Latitude = location.getLatitude();
pre_Lognitude = location.getLongitude();

}

try {
if (pre_lat_long == 2) {
String pre_lat = getSharedPreferences("TEXT", 0)
.getString("prelat", null);
String pre_long = getSharedPreferences("TEXT", 0)
.getString("prelog", null);
pre_Latitude = Double.valueOf(pre_lat);
pre_Lognitude = Double.valueOf(pre_long);
Log.e("locationpp", "" + pre_Latitude + ".."
+ pre_Lognitude);
}
} catch (Exception e) {
// TODO: handle exception
Log.e("error", e.getMessage().toString());
}

Double lat = 0.00, log = 0.00;
if (location != null) {
lat = location.getLatitude();
log = location.getLongitude();
Log.e("loca", "" + lat + ".." + log);
}

if (pre_lat_long == 0) {

pre_Latitude = lat;
pre_Lognitude = log;
pre_lat_long = 2;
}

present_Longitude = log;
present_Latitude = lat;
SharedPreferences shp = getSharedPreferences("TEXT", 0);
final Editor editor = shp.edit();
editor.putString("prelat", "" + lat);
editor.putString("prelog", "" + log);
editor.commit();


Location.distanceBetween(present_Latitude, present_Longitude,
pre_Latitude, pre_Lognitude, dist1);
double miles = dist1[0] * 0.00062137119;
total_times = location.getTime();
distance = distance + miles;
                               String Distance_string_1 = "" + distance;

         avg_distance = ((distance_cover_*3600)/totaltime_in_sec)*0.621371;