2017-03-03 29 views
-2

在将横向更改为纵向或反之时的以下代码中,“OUT Attendence Marked”显示为Toast并将空值插入数据库。为什么将屏幕更改为横向或纵向将插入值到数据库。从横向切换为纵向或反之亦然时,自动将值发送到服务器

import android.Manifest; 
import android.app.Dialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.support.v4.app.ActivityCompat; 
import android.support.v4.content.ContextCompat; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

import com.loopj.android.http.AsyncHttpClient; 
import com.loopj.android.http.AsyncHttpResponseHandler; 
import com.loopj.android.http.JsonHttpResponseHandler; 
import com.loopj.android.http.RequestParams; 

import org.json.JSONObject; 

import cz.msebera.android.httpclient.Header; 

public class Attendence extends AppCompatActivity { 
    Button tv,tv1; 
    String response; 
    double originLat=9.9967683,originLon=76.2875376; 
    Button btnShowLocation; 
    private Context context = this; 
    private TextView taska,taskb,usernametitle; 
    // GPSTracker class 
    GPSTracker gps; 
    private static final int CODE_GPS = 10; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_attendence); 



     if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { 
       ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, CODE_GPS); 
      } else { 
       ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, CODE_GPS); 
      } 
     } else { 

     } 






    tv= (Button) findViewById(R.id.in); 
     tv1=(Button) findViewById(R.id.out); 
     usernametitle= (TextView) findViewById(R.id.usernametitle); 
     SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
/* 
     SharedPreferences.Editor editor = pref.edit(); 
*/ 
     final String userid=pref.getString("userid", null); 
     final String usernamedisplay=pref.getString("username",null); 
     usernametitle.setText(usernamedisplay); 
     Log.e("userid","userid"+userid); 




     RequestParams params = new RequestParams(); 
     params.put("user_id", userid); 
     AsyncHttpClient client=new AsyncHttpClient(); 
     client.post("attendance.php", params, new JsonHttpResponseHandler() { 
      @Override 
      public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) { 
       response=responseBody.toString(); 
       Log.e("login","login"+response); 
       if(responseBody.optString("message").equals("TRUE")) { 
        String task1= responseBody.optString("task1"); 
        String task2= responseBody.optString("task2"); 
        taska = (TextView) findViewById(R.id.taska); 
        taska.setText(task1); 
        taskb = (TextView) findViewById(R.id.taskb); 
        taskb.setText(task2); 
/* 
        Log.e("task1","task2"+task1+task2); 
*/ 
        Toast.makeText(Attendence.this,"OUT Attendence Marked",Toast.LENGTH_SHORT).show(); 
       } 
       else{ 
        Toast.makeText(Attendence.this,"Cannot Be Marked",Toast.LENGTH_SHORT).show(); 
       } 
      } 

      @Override 
      public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) { 

      } 
     }); 

     tv.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Incall();//incall 
     } 

      private void Incall() { 
       gps = new GPSTracker(Attendence.this); 
       if(gps.canGetLocation()){ 

        double latitude = gps.getLatitude(); 
        double longitude = gps.getLongitude(); 

        if(latitude==0.0||longitude==0.0){ 
         Toast.makeText(Attendence.this,"Try Marking Again After Turning GPS ON",Toast.LENGTH_SHORT).show(); 
        } 
        float[] results = new float[1]; 
        Location.distanceBetween(latitude, longitude, originLat, originLon, results); 
        Log.e("lat"+latitude,"long"+longitude); 

        float distanceInMeters = results[0]; //radius 
        boolean isWithinRange = distanceInMeters < 60; 

        if (isWithinRange) { 
         //write your code what ever you wanna perform 
         RequestParams params = new RequestParams(); 
         params.put("user_id", userid); 
         params.put("status", "1"); 
         AsyncHttpClient client=new AsyncHttpClient(); 
         client.post("attendance.php", params, new JsonHttpResponseHandler() { 
          @Override 
          public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) { 
           response=responseBody.toString(); 
           Log.e("login","login"+response); 
           if(responseBody.optString("message").equals("TRUE")) { 
            Toast.makeText(Attendence.this,"Attendence Marked",Toast.LENGTH_SHORT).show(); 
            /* AlertDialog alertDialog = new AlertDialog.Builder(Attendence.this).create(); 
            alertDialog.setTitle("Attendence"); 
            alertDialog.setMessage("Attendence Marked"); 
            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", 
              new DialogInterface.OnClickListener() { 
               public void onClick(DialogInterface dialog, int which) { 
                dialog.dismiss(); 
               } 
              }); 
            alertDialog.show();*/ 
           } 
           else{ 
            Toast.makeText(Attendence.this,"Cannot Be Marked",Toast.LENGTH_SHORT).show(); 
           } 
          } 

          @Override 
          public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) { 
           Toast.makeText(Attendence.this,"failed",Toast.LENGTH_SHORT).show(); 
          } 
         }); 

         Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
/* 
         Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
*/ 

        }else{ 
         Toast.makeText(getApplicationContext(), "Not in DenInfotech. Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
        } 
        // \n is for new line 

       }else{ 
        // can't get location 
        // GPS or Network is not enabled 
        // Ask user to enable GPS/network in settings 
        gps.showSettingsAlert(); 
       } 



      } 
     }); 
     tv1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       gps = new GPSTracker(Attendence.this); 

       // check if GPS enabled 
       if(gps.canGetLocation()){ 

        double latitude = gps.getLatitude(); 
        double longitude = gps.getLongitude(); 
        float[] results = new float[1]; 
        Location.distanceBetween(latitude, longitude, originLat, originLon, results); 
        float distanceInMeters = results[0]; 
        boolean isWithinRange = distanceInMeters < 60; 

        if (isWithinRange) { 
         /* final Dialog openDialog = new Dialog(getApplicationContext()); 
         openDialog.setContentView(R.layout.dialog); 
         openDialog.setTitle("Custom Dialog Box"); 
         TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.dialog_text); 
         ImageView dialogImage = (ImageView)openDialog.findViewById(R.id.dialog_image); 
         Button dialogCloseButton = (Button)openDialog.findViewById(R.id.dialog_button); 
         dialogCloseButton.setOnClickListener(new View.OnClickListener(){ 
          @Override 
          public void onClick(View v) { 
           // TODO Auto-generated method stub 
           openDialog.dismiss(); 
          } 
         }); 
         openDialog.show();*/ 

         LayoutInflater inflater = LayoutInflater.from(context); 
         View dialogLayout = inflater.inflate(R.layout.dialog, null); 
         AlertDialog.Builder builder = new AlertDialog.Builder(context); 
         builder.setView(dialogLayout); 

         TextView indtruction = (TextView)dialogLayout.findViewById(R.id.text_instruction); 

         final EditText work1 = (EditText)dialogLayout.findViewById(R.id.task1); 
         final EditText work2 = (EditText)dialogLayout.findViewById(R.id.task2); 
         builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           // TODO Auto-generated method stub 
           dialog.dismiss(); 
          } 
         }); 
         builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           // TODO Auto-generated method stub 
           String workjob = work1.getText().toString(); 
           String workjob1 = work2.getText().toString(); 
           Log.e("userInputContent"+workjob,"userInputContent"+workjob); 
           if(workjob.equals("")){ 
            Toast.makeText(context, "You must enter a name in the field", Toast.LENGTH_LONG).show(); 
            return; 
           } 

           RequestParams params = new RequestParams(); 
           params.put("user_id", userid); 
           params.put("status", 1); 
           params.put("work1",workjob); 
           params.put("work2",workjob1); 
           Log.e("workjob","As"+workjob+workjob1); 
           AsyncHttpClient client=new AsyncHttpClient(); 
           client.post("time_out.php", params, new JsonHttpResponseHandler() { 
            @Override 
            public void onSuccess(int statusCode, Header[] headers, JSONObject responseBody) { 
             response=responseBody.toString(); 
             Log.e("login","login"+response); 
             if(responseBody.optString("message").equals("TRUE")) { 
              Toast.makeText(Attendence.this,"OUT Attendence Marked",Toast.LENGTH_SHORT).show(); 
             } 
             else{ 
              Toast.makeText(Attendence.this,"Cannot Be Marked",Toast.LENGTH_SHORT).show(); 
             } 
            } 

            @Override 
            public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable error) { 
             Toast.makeText(Attendence.this,"failed",Toast.LENGTH_SHORT).show(); 

            } 
           }); 


          } 
         }); 
         AlertDialog customAlertDialog = builder.create(); 
         customAlertDialog.show(); 

         WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); 
         lp.copyFrom(customAlertDialog.getWindow().getAttributes()); 
         lp.width = LinearLayout.LayoutParams.MATCH_PARENT; 
         lp.height = 700; 
         customAlertDialog.getWindow().setAttributes(lp); 


         //write your code what ever you wanna perform 
         Toast.makeText(getApplicationContext(), "Success Complete. Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
        }else{ 
         Toast.makeText(getApplicationContext(), " Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 
        } 

        // \n is for new line 
       }else{ 
        // can't get location 
        // GPS or Network is not enabled 
        // Ask user to enable GPS/network in settings 
        gps.showSettingsAlert(); 
       } 

      } 
     }); 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case CODE_GPS: { 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       } else { 
       } 
      } 
     } 
    } 
} 

回答

1

当您更改方向时重新创建活动。正如我所看到的,您已经编写了用于将数据发送到OnCreate内的服务器的代码,每当您改变方向时都会发生这种情况。

简单解决方法是每次更改方向时停止活动重新加载。

这可以通过在manifest.xml文件

<activity 
    android:name=".Attendence" 
    android:configChanges="orientation" 
    android:label="" /> 

加入android:configChanges="orientation"你的活动代码来完成我希望这将有助于。

0

V-Coder是正确的,但它似乎你已经在onCreate()中声明了你的Incall()方法,这将导致编译错误。

相关问题