Advertisement
Tonmoy77

after login a button create that shows my location

Dec 24th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.93 KB | None | 0 0
  1. import android.content.Intent;
  2. import android.os.AsyncTask;
  3. import android.os.Handler;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12.  
  13. public class MainActivity extends ActionBarActivity {
  14.     EditText UsernameEt, PasswordEt;
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState) {
  17.         super.onCreate(savedInstanceState);
  18.         setContentView(R.layout.activity_main);
  19.         UsernameEt = (EditText)findViewById(R.id.etUserName);
  20.         PasswordEt = (EditText)findViewById(R.id.etPassword);
  21.     }
  22.  
  23.     public void OnLogin(View view) {
  24.         String username = UsernameEt.getText().toString();
  25.         String password = PasswordEt.getText().toString();
  26.         String type = "login";
  27.         BackgroundWorker backgroundWorker = new BackgroundWorker(this);
  28.         AsyncTask<String, Void, String> execute = backgroundWorker.execute(type, username, password);
  29.  
  30.     }
  31.  
  32.     public void registerB(View view) {
  33.         Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
  34.         startActivity(intent);
  35.     }private Boolean exit = false;
  36.     @Override
  37.  
  38.     public void onBackPressed(){
  39.         if (exit) {
  40.             finish();
  41.         }
  42.         else {
  43.             Toast.makeText(this, "Press Back again to Exit.",
  44.                     Toast.LENGTH_SHORT).show();
  45.             exit = true;
  46.             new Handler().postDelayed(new Runnable() {
  47.  
  48.                 @Override
  49.                 public void run() {
  50.                     // TODO Auto-generated method stub
  51.                     Intent a = new Intent(Intent.ACTION_MAIN);
  52.                     a.addCategory(Intent.CATEGORY_HOME);
  53.                     a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  54.                     startActivity(a);
  55.                 }
  56.             }, 1000);
  57.  
  58. //Backgroundworker class//
  59.  
  60.  
  61. public class BackgroundWorker extends AsyncTask<String,Void,String> {
  62.  
  63.     Context ctx;
  64.     AlertDialog alertDialog;
  65.     BackgroundWorker (Context ctx) {
  66.  
  67.         this.ctx=ctx;
  68.     }
  69.     @Override
  70.     protected String doInBackground(String... params) {
  71.         String type = params[0];
  72.         String login_url = "http://localhost/~myprojec//login.php";
  73.         String register_url = "http://localhost/~myprojec//register.php";
  74.         if(type.equals("login")) try {
  75.             String user_name = params[1];
  76.             String password = params[2];
  77.             URL url = new URL(login_url);
  78.             HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  79.             httpURLConnection.setRequestMethod("POST");
  80.             httpURLConnection.setDoOutput(true);
  81.             httpURLConnection.setDoInput(true);
  82.             OutputStream outputStream = httpURLConnection.getOutputStream();
  83.             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  84.             String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
  85.                     + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  86.             bufferedWriter.write(post_data);
  87.             bufferedWriter.flush();
  88.             bufferedWriter.close();
  89.             outputStream.close();
  90.             InputStream inputStream = httpURLConnection.getInputStream();
  91.             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  92.             String result = "";
  93.             String line = "";
  94.             while ((line = bufferedReader.readLine()) != null) {
  95.                 result += line;
  96.             }
  97.             bufferedReader.close();
  98.             inputStream.close();
  99.             httpURLConnection.disconnect();
  100.             return result;
  101.         } catch (MalformedURLException e) {
  102.             e.printStackTrace();
  103.         } catch (IOException e) {
  104.             e.printStackTrace();
  105.         }else if(type.equals("register")){
  106.             try {
  107.                 String name = params[1];
  108.                 String surname = params[2];
  109.                 String age = params[3];
  110.                 String username = params[4];
  111.                 String password = params[5];
  112.                 URL url = new URL(register_url);
  113.                 HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  114.                 httpURLConnection.setRequestMethod("POST");
  115.                 httpURLConnection.setDoOutput(true);
  116.                 httpURLConnection.setDoInput(true);
  117.                 OutputStream outputStream = httpURLConnection.getOutputStream();
  118.                 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  119.                 String post_data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&"
  120.                 + URLEncoder.encode("surname", "UTF-8") + "=" + URLEncoder.encode(surname, "UTF-8") + "&"
  121.                 + URLEncoder.encode("age", "UTF-8") + "=" + URLEncoder.encode(age, "UTF-8") + "&"
  122.                 + URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&"
  123.                         + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  124.                 bufferedWriter.write(post_data);
  125.                 bufferedWriter.flush();
  126.                 bufferedWriter.close();
  127.                 outputStream.close();
  128.                 InputStream inputStream = httpURLConnection.getInputStream();
  129.                 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  130.                 String result = "";
  131.                 String line = "";
  132.                 while ((line = bufferedReader.readLine()) != null) {
  133.                     result += line;
  134.                 }
  135.                 bufferedReader.close();
  136.                 inputStream.close();
  137.                 httpURLConnection.disconnect();
  138.                 return result;
  139.             } catch (MalformedURLException e) {
  140.                 e.printStackTrace();
  141.             } catch (IOException e) {
  142.                 e.printStackTrace();
  143.             }
  144.         }
  145.         return null;
  146.     }
  147.  
  148.     @Override
  149.     protected void onPreExecute() {
  150.  
  151.         alertDialog = new AlertDialog.Builder(ctx).create();
  152.         alertDialog.setTitle("Login Information....");
  153.     }
  154.  
  155.     @Override
  156.     protected void onPostExecute(String result) {
  157.         if(result == null)
  158.         {
  159.             // do what you want to do
  160.         }else if (result.contains("Incorrect username/password!! Try again")){
  161.             Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  162.         }
  163.  
  164.         else if(result.contains("login success !!!!! Welcome user")) // msg you get from success like "Login Success"
  165.         {
  166.             Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  167.             Intent i = new Intent(ctx,TextActivity.class);
  168.             ctx.startActivity(i);
  169.  
  170.  
  171.         }else if(result.contains("Registerd succesfully")){
  172.             Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
  173.             Intent i = new Intent(ctx,MainActivity.class);
  174.             ctx.startActivity(i);
  175.         };
  176.     }
  177.  
  178. //TextActivity//
  179. import android.app.Activity;
  180. import android.content.DialogInterface;
  181. import android.content.Intent;
  182. import android.os.CountDownTimer;
  183. import android.os.Handler;
  184. import android.support.v7.app.AlertDialog;
  185. import android.support.v7.app.AppCompatActivity;
  186. import android.os.Bundle;
  187. import android.view.View;
  188. import android.widget.Button;
  189. import android.widget.Toast;
  190.  
  191. public class TextActivity extends AppCompatActivity {
  192.     GPSTracker gps;
  193.     Button locationBt;
  194.  
  195.     @Override
  196.     protected void onCreate(Bundle savedInstanceState) {
  197.         super.onCreate(savedInstanceState);
  198.         setContentView(R.layout.activity_location);
  199.         locationBt = (Button) findViewById(R.id.locationBt);
  200.  
  201.         locationBt.setOnClickListener(new View.OnClickListener() {
  202.  
  203.             @Override
  204.             public void onClick(View v) {
  205.  
  206.  
  207.                 if (gps.canGetLocation()) {
  208.                     double latitude = gps.getLatitude();
  209.                     double longitude = gps.getLongitude();
  210.  
  211.                     Toast.makeText(
  212.                             getApplicationContext(),
  213.                             "Your Location is -\nLat: " + latitude + "\nLong: "
  214.                                     + longitude, Toast.LENGTH_LONG).show();
  215.                 } else {
  216.                     gps.showSettingsAlert();
  217.                 }
  218.  
  219.             }
  220.  
  221.         });
  222.  
  223.     }
  224.     private Boolean exit = false;
  225.     @Override
  226.  
  227.     public void onBackPressed() {
  228.         if (exit) {
  229.             finish(); // finish activity
  230.         } else {
  231.             Toast.makeText(this, "Press Back again to Exit.",
  232.                     Toast.LENGTH_SHORT).show();
  233.             exit = true;
  234.  
  235.             new Handler().postDelayed(new Runnable() {
  236.                 @Override
  237.                 public void run() {
  238.                     exit = false;
  239.                     Intent a = new Intent(Intent.ACTION_MAIN);
  240.                     a.addCategory(Intent.CATEGORY_HOME);
  241.                     a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  242.                     startActivity(a);
  243.                 }
  244.             }, 3 * 1000);
  245.  
  246.  
  247.  
  248.         }
  249.     }
  250.  
  251.  
  252.  
  253.         }
  254. }}
  255.  
  256. //Gpstracker//
  257.  
  258. import android.Manifest;
  259. import android.app.AlertDialog;
  260. import android.app.Service;
  261. import android.content.Context;
  262. import android.content.DialogInterface;
  263. import android.content.Intent;
  264. import android.content.pm.PackageManager;
  265. import android.location.Location;
  266. import android.location.LocationListener;
  267. import android.location.LocationManager;
  268. import android.os.Bundle;
  269. import android.os.IBinder;
  270. import android.provider.Settings;
  271. import android.support.v4.app.ActivityCompat;
  272. import android.telephony.SmsManager;
  273. import android.util.Log;
  274.  
  275. public class GPSTracker extends Service implements LocationListener {
  276.  
  277.  
  278.     private final Context context;
  279.  
  280.     boolean isGPSEnabled = false;
  281.     boolean isNetworkEnabled = false;
  282.     boolean canGetLocation = false;
  283.  
  284.     Location location;
  285.  
  286.     double latitude;
  287.     double longitude;
  288.  
  289.  
  290.     private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
  291.     private static final long MIN_TIME_BW_UPDATES = 1000 * 60 ;
  292.  
  293.     protected LocationManager locationManager;
  294.  
  295.     public GPSTracker(Context context) {
  296.         this.context = context;
  297.         getLocation();
  298.     }
  299.  
  300.     public Location getLocation() {
  301.         try {
  302.             locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
  303.  
  304.             isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  305.  
  306.             isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  307.  
  308.             if (!isGPSEnabled && !isNetworkEnabled) {
  309.             } else {
  310.                 this.canGetLocation = true;
  311.  
  312.                 if (isNetworkEnabled) {
  313.  
  314.                     locationManager.requestLocationUpdates(
  315.                             LocationManager.NETWORK_PROVIDER,
  316.                             MIN_TIME_BW_UPDATES,
  317.                             MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  318.  
  319.                     if (locationManager != null) {
  320.                         location = locationManager
  321.                                 .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  322.  
  323.                         if (location != null) {
  324.  
  325.                             latitude = location.getLatitude();
  326.                             longitude = location.getLongitude();
  327.                         }
  328.                     }
  329.  
  330.                 }
  331.  
  332.                 if (isGPSEnabled) {
  333.                     if (location == null) {
  334.                         locationManager.requestLocationUpdates(
  335.                                 LocationManager.GPS_PROVIDER,
  336.                                 MIN_TIME_BW_UPDATES,
  337.                                 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
  338.  
  339.                         if (locationManager != null) {
  340.                             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  341.                                 // TODO: Consider calling
  342.                                 //    ActivityCompat#requestPermissions
  343.                                 // here to request the missing permissions, and then overriding
  344.                                 //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
  345.                                 //                                          int[] grantResults)
  346.                                 // to handle the case where the user grants the permission. See the documentation
  347.                                 // for ActivityCompat#requestPermissions for more details.
  348.                                 return location;
  349.                             }
  350.                             location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  351.  
  352.                             if (location != null) {
  353.                                 latitude = location.getLatitude();
  354.                                 longitude = location.getLongitude();
  355.                             }
  356.                         }
  357.                     }
  358.                 }
  359.             }
  360.  
  361.         } catch (Exception e) {
  362.             e.printStackTrace();
  363.         }
  364.  
  365.         return location;
  366.     }
  367.  
  368.  
  369.     public void stopUsingGPS() {
  370.         if (locationManager != null)
  371.             if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  372.                 // TODO: Consider calling
  373.                 //    ActivityCompat#requestPermissions
  374.                 // here to request the missing permissions, and then overriding
  375.                 //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
  376.                 //                                          int[] grantResults)
  377.                 // to handle the case where the user grants the permission. See the documentation
  378.                 // for ActivityCompat#requestPermissions for more details.
  379.                 return;
  380.             }
  381.     }
  382.  
  383.     public double getLatitude() {
  384.         if(location != null) {
  385.             latitude = location.getLatitude();
  386.         }
  387.         return latitude;
  388.     }
  389.  
  390.     public double getLongitude() {
  391.         if(location != null) {
  392.             longitude = location.getLongitude();
  393.         }
  394.  
  395.         return longitude;
  396.     }
  397.  
  398.     public boolean canGetLocation() {
  399.         return this.canGetLocation;
  400.     }
  401.  
  402.      public void showSettingsAlert() {
  403.         AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
  404.  
  405.         alertDialog.setTitle("GPS is settings");
  406.  
  407.        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
  408.  
  409.         alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
  410.  
  411.             @Override
  412.           public void onClick(DialogInterface dialog, int which) {
  413.                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  414.           context.startActivity(intent);
  415.             }
  416.         });
  417.  
  418.         alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  419.  
  420.            @Override
  421.             public void onClick(DialogInterface dialog, int which) {
  422.                 dialog.cancel();
  423.             }
  424.         });
  425.  
  426.         alertDialog.show();
  427.    }
  428.  
  429.     @Override
  430.     public void onLocationChanged(final Location location) {
  431.  
  432.         updateWithNewLocation(location);
  433.         SendSms(location);
  434.  
  435.     }
  436.  
  437.     private void updateWithNewLocation(Location location) {
  438.  
  439.     }
  440.  
  441.     public void SendSms(Location location){
  442.  
  443.         String phoneNo = ("+8801732986828");
  444.  
  445.  
  446.  
  447.         String locationText = "This is my location.. plz help."+"Latitude-"+location.getLatitude() +" "+ "and"+ " "+"Longitude"+" "+ location.getLongitude();
  448.         SmsManager smsManager = SmsManager.getDefault();
  449.         smsManager.sendTextMessage(phoneNo, null,locationText , null, null);
  450.     }
  451.  
  452.  
  453.  
  454.     @Override
  455.     public void onProviderDisabled(String arg0) {
  456.         // TODO Auto-generated method stub
  457.  
  458.     }
  459.  
  460.     @Override
  461.     public void onProviderEnabled(String arg0) {
  462.         // TODO Auto-generated method stub
  463.  
  464.     }
  465.  
  466.     @Override
  467.     public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  468.         // TODO Auto-generated method stub
  469.  
  470.     }
  471.  
  472.     @Override
  473.     public IBinder onBind(Intent intent) {
  474.         // TODO Auto-generated method stub
  475.         return null;
  476.     }
  477.  
  478.  
  479.  
  480. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement