finding errors

6
package com.map.alarm; //sending msg prgm// import java.io.IOException; import java.text.DecimalFormat; import java.util.ArrayList; import android.app.Activity; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.StrictMode; import android.telephony.SmsManager; import android.util.Log; import android.widget.Toast; public class MyService extends Service implements LocationListener { private final Handler handler = new Handler(); Intent intent; int counter = 0, alarmcount = 0;; SessionManager sManager; // flag for GPS status boolean isGPSEnabled = false; // flag for network status boolean isNetworkEnabled = false; // flag for GPS status boolean canGetLocation = false; Location location; // location double latitude, complatitude,longitude, complongitude; // latitude // The minimum distance to change Updates in meters private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters // The minimum time between updates in milliseconds private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute // Declaring a Location Manager protected LocationManager locationManager; @Override

Upload: vinoth1128

Post on 26-May-2017

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Finding Errors

package com.map.alarm; //sending msg prgm//import java.io.IOException;import java.text.DecimalFormat;import java.util.ArrayList;import android.app.Activity;import android.app.PendingIntent;import android.app.Service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.media.AudioManager;import android.media.MediaPlayer;import android.media.RingtoneManager;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.os.IBinder;import android.os.StrictMode;import android.telephony.SmsManager;import android.util.Log;import android.widget.Toast;public class MyService extends Service implements LocationListener {

private final Handler handler = new Handler();Intent intent;int counter = 0, alarmcount = 0;;SessionManager sManager;// flag for GPS statusboolean isGPSEnabled = false;// flag for network statusboolean isNetworkEnabled = false;// flag for GPS statusboolean canGetLocation = false;Location location; // locationdouble latitude, complatitude,longitude, complongitude; // latitude// The minimum distance to change Updates in metersprivate static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10

meters// The minimum time between updates in millisecondsprivate static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1

minute// Declaring a Location Managerprotected LocationManager locationManager;@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stubif (android.os.Build.VERSION.SDK_INT > 9) {

StrictMode.ThreadPolicy policy =new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);}

int i = 0;

Page 2: Finding Errors

while (i < 101) {if (i > 100) {

this.onDestroy();} else {

counter = i;i++;handler.removeCallbacks(sendUpdatesToUI);handler.postDelayed(sendUpdatesToUI, 1 * 1000); // 1

sec}

}return super.onStartCommand(intent, flags, startId);

}@Overridepublic void onCreate() {

// TODO Auto-generated method stubsuper.onCreate();sManager = new SessionManager(App.context);// intent = new Intent(BROADCAST_ACTION);

}@Overridepublic void onDestroy() {

// TODO Auto-generated method stubsuper.onDestroy();if (sendUpdatesToUI != null) {

handler.removeCallbacks(sendUpdatesToUI);}

}@Overridepublic IBinder onBind(Intent intent) {

// TODO Auto-generated method stubreturn null;

}private Runnable sendUpdatesToUI = new Runnable() {

public void run() {getLocation();compLocation();handler.postDelayed(this, 1 * 1000); // 1 sec

}};

public Location getLocation() {try {

locationManager = (LocationManager) App.context.getSystemService(LOCATION_SERVICE);

// getting GPS statusisGPSEnabled = locationManager

.isProviderEnabled(LocationManager.GPS_PROVIDER);

// getting network statusisNetworkEnabled = locationManager

.isProviderEnabled(LocationManager.NETWORK_PROVIDER);if (!isGPSEnabled && !isNetworkEnabled) {

// no network provider is enabled} else {

Page 3: Finding Errors

this.canGetLocation = true;// First get location from Network Providerif (isNetworkEnabled) {

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_DISTANCE_CHANGE_FOR_UPDATES,

this);if (locationManager != null) {

location = locationManager.getLastKnownLocation(Locatio

nManager.NETWORK_PROVIDER);if (location != null) {

latitude = location.getLatitude();longitude =

location.getLongitude();}

}}// if GPS Enabled get lat/long using GPS Servicesif (isGPSEnabled) {

if (location == null) {locationManager.requestLocationUpdates(

LocationManager.GPS_PROVIDER,MIN_TIME_BW_UPDATES,

MIN_DISTANCE_CHANGE_FOR_UPDATES, this);if (locationManager != null) {

location = locationManager.getLastKnownLocation(L

ocationManager.GPS_PROVIDER);if (location != null) {latitude =location.getLatitude();longitude =

location.getLongitude();}

}}

}}

} catch (Exception e) {e.printStackTrace();

}return location;

}double roundTwoDecimals(double d) {

DecimalFormat twoDecimals = new DecimalFormat("#.######"); return Double.valueOf(twoDecimals.format(d));}

public void compLocation() {complatitude = roundTwoDecimals(sManager.getDouble("setLat", 0));complongitude = roundTwoDecimals(sManager.getDouble("setLong",

0));double roundlong=roundTwoDecimals(longitude);double roundlat=roundTwoDecimals(latitude);

Page 4: Finding Errors

if (complatitude == roundlat || complongitude == roundlong) {try {

beepalram();sendSMS("8428164329", "your reached dangerous border!

please dont go further");} catch (IllegalStateException e) {

// TODO Auto-generated catch blocke.printStackTrace();

} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();

}}}

@Overridepublic void onLocationChanged(Location location) {

// TODO Auto-generated method stub}@Overridepublic void onProviderDisabled(String provider) {

// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {

// TODO Auto-generated method stub}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras)

{// TODO Auto-generated method stub}

/* Sending SMS method */private void sendSMS(String phoneNumber, String message) {

SmsManager smsManager = SmsManager.getDefault();String SENT = "SMS_SENT";String DELIVERED = "SMS_DELIVERED";SmsManager sms = SmsManager.getDefault();ArrayList<String> parts = sms.divideMessage(message);int messageCount = parts.size();Log.i("Message Count", "Message Count: " + messageCount);\ArrayList<PendingIntent> deliveryIntents = new

ArrayList<PendingIntent>();ArrayList<PendingIntent> sentIntents = new

ArrayList<PendingIntent>();PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new

Intent(SENT), 0);

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0);

for (int j = 0; j < messageCount; j++) {sentIntents.add(sentPI);deliveryIntents.add(deliveredPI);

}// ---when the SMS has been sent---

registerReceiver(new BroadcastReceiver() {@Overridepublic void onReceive(Context arg0, Intent arg1) {

switch (getResultCode()) {case Activity.RESULT_OK:

Toast.makeText(getBaseContext(), "SMS sent",

Page 5: Finding Errors

Toast.LENGTH_SHORT).show();break;

case SmsManager.RESULT_ERROR_GENERIC_FAILURE:Toast.makeText(getBaseContext(), "Generic

failure",Toast.LENGTH_SHORT).show();

break;case SmsManager.RESULT_ERROR_NO_SERVICE:

Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_SHORT).show();

break;case SmsManager.RESULT_ERROR_NULL_PDU:

Toast.makeText(getBaseContext(), "Null PDU",Toast.LENGTH_SHORT).show();

break;case SmsManager.RESULT_ERROR_RADIO_OFF:

Toast.makeText(getBaseContext(), "Radio off",Toast.LENGTH_SHORT).show();

break;}

}}, new IntentFilter(SENT));// ---when the SMS has been delivered---registerReceiver(new BroadcastReceiver() {

@Overridepublic void onReceive(Context arg0, Intent arg1) {

switch (getResultCode()) {case Activity.RESULT_OK:

Toast.makeText(getBaseContext(), "SMS delivered",

Toast.LENGTH_SHORT).show();break;

case Activity.RESULT_CANCELED:Toast.makeText(getBaseContext(), "SMS not

delivered",Toast.LENGTH_SHORT).show();

break;}

}}, new IntentFilter(DELIVERED));smsManager.sendTextMessage(phoneNumber, null, message, null,

null);}private void beepalram() throws IllegalStateException, IOException {

Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

MediaPlayer mMediaPlayer = new MediaPlayer();mMediaPlayer.setDataSource(this, soundUri);final AudioManager audioManager = (AudioManager) this

.getSystemService(Context.AUDIO_SERVICE);if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0)

{mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);mMediaPlayer.setLooping(true);mMediaPlayer.prepare();

Page 6: Finding Errors

mMediaPlayer.start();}}}