Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Intent;
- import android.os.AsyncTask;
- import android.os.Handler;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends ActionBarActivity {
- EditText UsernameEt, PasswordEt;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- UsernameEt = (EditText)findViewById(R.id.etUserName);
- PasswordEt = (EditText)findViewById(R.id.etPassword);
- }
- public void OnLogin(View view) {
- String username = UsernameEt.getText().toString();
- String password = PasswordEt.getText().toString();
- String type = "login";
- BackgroundWorker backgroundWorker = new BackgroundWorker(this);
- AsyncTask<String, Void, String> execute = backgroundWorker.execute(type, username, password);
- }
- public void registerB(View view) {
- Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
- startActivity(intent);
- }private Boolean exit = false;
- @Override
- public void onBackPressed(){
- if (exit) {
- finish();
- }
- else {
- Toast.makeText(this, "Press Back again to Exit.",
- Toast.LENGTH_SHORT).show();
- exit = true;
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- Intent a = new Intent(Intent.ACTION_MAIN);
- a.addCategory(Intent.CATEGORY_HOME);
- a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(a);
- }
- }, 1000);
- //Backgroundworker class//
- public class BackgroundWorker extends AsyncTask<String,Void,String> {
- Context ctx;
- AlertDialog alertDialog;
- BackgroundWorker (Context ctx) {
- this.ctx=ctx;
- }
- @Override
- protected String doInBackground(String... params) {
- String type = params[0];
- String login_url = "http://localhost/~myprojec//login.php";
- String register_url = "http://localhost/~myprojec//register.php";
- if(type.equals("login")) try {
- String user_name = params[1];
- String password = params[2];
- URL url = new URL(login_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- String post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&"
- + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
- bufferedWriter.write(post_data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
- String result = "";
- String line = "";
- while ((line = bufferedReader.readLine()) != null) {
- result += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }else if(type.equals("register")){
- try {
- String name = params[1];
- String surname = params[2];
- String age = params[3];
- String username = params[4];
- String password = params[5];
- URL url = new URL(register_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- String post_data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&"
- + URLEncoder.encode("surname", "UTF-8") + "=" + URLEncoder.encode(surname, "UTF-8") + "&"
- + URLEncoder.encode("age", "UTF-8") + "=" + URLEncoder.encode(age, "UTF-8") + "&"
- + URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&"
- + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
- bufferedWriter.write(post_data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
- String result = "";
- String line = "";
- while ((line = bufferedReader.readLine()) != null) {
- result += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onPreExecute() {
- alertDialog = new AlertDialog.Builder(ctx).create();
- alertDialog.setTitle("Login Information....");
- }
- @Override
- protected void onPostExecute(String result) {
- if(result == null)
- {
- // do what you want to do
- }else if (result.contains("Incorrect username/password!! Try again")){
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- }
- else if(result.contains("login success !!!!! Welcome user")) // msg you get from success like "Login Success"
- {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- Intent i = new Intent(ctx,TextActivity.class);
- ctx.startActivity(i);
- }else if(result.contains("Registerd succesfully")){
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- Intent i = new Intent(ctx,MainActivity.class);
- ctx.startActivity(i);
- };
- }
- //TextActivity//
- import android.app.Activity;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.CountDownTimer;
- import android.os.Handler;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- public class TextActivity extends AppCompatActivity {
- GPSTracker gps;
- Button locationBt;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_location);
- locationBt = (Button) findViewById(R.id.locationBt);
- locationBt.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- if (gps.canGetLocation()) {
- double latitude = gps.getLatitude();
- double longitude = gps.getLongitude();
- Toast.makeText(
- getApplicationContext(),
- "Your Location is -\nLat: " + latitude + "\nLong: "
- + longitude, Toast.LENGTH_LONG).show();
- } else {
- gps.showSettingsAlert();
- }
- }
- });
- }
- private Boolean exit = false;
- @Override
- public void onBackPressed() {
- if (exit) {
- finish(); // finish activity
- } else {
- Toast.makeText(this, "Press Back again to Exit.",
- Toast.LENGTH_SHORT).show();
- exit = true;
- new Handler().postDelayed(new Runnable() {
- @Override
- public void run() {
- exit = false;
- Intent a = new Intent(Intent.ACTION_MAIN);
- a.addCategory(Intent.CATEGORY_HOME);
- a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(a);
- }
- }, 3 * 1000);
- }
- }
- }
- }}
- //Gpstracker//
- import android.Manifest;
- import android.app.AlertDialog;
- import android.app.Service;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.provider.Settings;
- import android.support.v4.app.ActivityCompat;
- import android.telephony.SmsManager;
- import android.util.Log;
- public class GPSTracker extends Service implements LocationListener {
- private final Context context;
- boolean isGPSEnabled = false;
- boolean isNetworkEnabled = false;
- boolean canGetLocation = false;
- Location location;
- double latitude;
- double longitude;
- private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
- private static final long MIN_TIME_BW_UPDATES = 1000 * 60 ;
- protected LocationManager locationManager;
- public GPSTracker(Context context) {
- this.context = context;
- getLocation();
- }
- public Location getLocation() {
- try {
- locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
- isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- if (!isGPSEnabled && !isNetworkEnabled) {
- } else {
- this.canGetLocation = true;
- if (isNetworkEnabled) {
- locationManager.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER,
- MIN_TIME_BW_UPDATES,
- MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
- if (locationManager != null) {
- location = locationManager
- .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
- if (location != null) {
- latitude = location.getLatitude();
- longitude = location.getLongitude();
- }
- }
- }
- if (isGPSEnabled) {
- if (location == null) {
- locationManager.requestLocationUpdates(
- LocationManager.GPS_PROVIDER,
- MIN_TIME_BW_UPDATES,
- MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
- if (locationManager != null) {
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.
- return location;
- }
- location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- if (location != null) {
- latitude = location.getLatitude();
- longitude = location.getLongitude();
- }
- }
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return location;
- }
- public void stopUsingGPS() {
- if (locationManager != null)
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
- // TODO: Consider calling
- // ActivityCompat#requestPermissions
- // here to request the missing permissions, and then overriding
- // public void onRequestPermissionsResult(int requestCode, String[] permissions,
- // int[] grantResults)
- // to handle the case where the user grants the permission. See the documentation
- // for ActivityCompat#requestPermissions for more details.
- return;
- }
- }
- public double getLatitude() {
- if(location != null) {
- latitude = location.getLatitude();
- }
- return latitude;
- }
- public double getLongitude() {
- if(location != null) {
- longitude = location.getLongitude();
- }
- return longitude;
- }
- public boolean canGetLocation() {
- return this.canGetLocation;
- }
- public void showSettingsAlert() {
- AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
- alertDialog.setTitle("GPS is settings");
- alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
- alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
- context.startActivity(intent);
- }
- });
- alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- }
- });
- alertDialog.show();
- }
- @Override
- public void onLocationChanged(final Location location) {
- updateWithNewLocation(location);
- SendSms(location);
- }
- private void updateWithNewLocation(Location location) {
- }
- public void SendSms(Location location){
- String phoneNo = ("+8801732986828");
- String locationText = "This is my location.. plz help."+"Latitude-"+location.getLatitude() +" "+ "and"+ " "+"Longitude"+" "+ location.getLongitude();
- SmsManager smsManager = SmsManager.getDefault();
- smsManager.sendTextMessage(phoneNo, null,locationText , null, null);
- }
- @Override
- public void onProviderDisabled(String arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onProviderEnabled(String arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
- // TODO Auto-generated method stub
- }
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement