Android 每当用户登录时,只在后台运行一次代码
Ia尝试在用户登录应用程序时获取用户位置。我分别创建了一个类及其工作状态,但当我将其嵌入主项目时,它没有在数据库中提交位置值。我希望每当用户登录时,该类只运行一次,并在数据库中提交坐标 以下是我的位置类的代码: 我想,当用户登录时,这个类只运行一次,我只想在登录时获得用户位置Android 每当用户登录时,只在后台运行一次代码,android,Android,Ia尝试在用户登录应用程序时获取用户位置。我分别创建了一个类及其工作状态,但当我将其嵌入主项目时,它没有在数据库中提交位置值。我希望每当用户登录时,该类只运行一次,并在数据库中提交坐标 以下是我的位置类的代码: 我想,当用户登录时,这个类只运行一次,我只想在登录时获得用户位置 import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import andro
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.util.HashMap;
import java.util.Map;
public class getposition extends AppCompatActivity {
String URL_LOGIN = "api"
Button btn;
TextView tv1,tv2;
LocationManager locationManager;
static final int REQUEST_LOCATION = 1;
String lg, lt , chk;
String lat_long;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getposition);
btn = findViewById(R.id.send);
tv1 = findViewById(R.id.lang);
tv2 = findViewById(R.id.lat);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
chk = getLocation();
loadlocation();
if(chk != "false"){
String foo = chk;
String[] split = foo.split("_");
lt = split[0];
lg = split[1];
Toast.makeText(getApplicationContext(),chk,Toast.LENGTH_LONG).show();
}
}
private void loadlocation() {
final RequestQueue requestQueue = Volley.newRequestQueue(getposition.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
tv1.setText(response);
tv2.setText(response);
requestQueue.stop();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
tv1.setText("Something went wrong///");
error.printStackTrace();
requestQueue.stop();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("user_id","12" );
params.put("user_lat", lt);
params.put("user_lng", lg);
return params;
}
};
VolleySingleton.getInstance(getposition.this).addToRequestQueue(stringRequest);
}
public String getLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
double latt = location.getLatitude();
double lng = location.getLongitude();
lat_long = latt + "_" + lng;
tv1.setText("Latitude: " + latt);
tv2.setText("Longitude: " + lng);
} else {
lat_long = "false";
tv1.setText("Unable to find correct location.");
tv2.setText("Unable to find correct location. ");
}
}
return lat_long;
}
}
导入android.Manifest;
导入android.content.Context;
导入android.content.pm.PackageManager;
导入android.location.location;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.support.v4.app.ActivityCompat;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.android.volley.AuthFailureError;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
导入java.util.HashMap;
导入java.util.Map;
公共类getposition扩展了AppCompatActivity{
字符串URL\u LOGIN=“api”
按钮btn;
文本视图tv1、tv2;
地点经理地点经理;
静态最终int请求_位置=1;
字符串lg、lt、chk;
弦长;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u getposition);
btn=findViewById(R.id.send);
tv1=findViewById(R.id.lang);
tv2=findViewById(R.id.lat);
locationManager=(locationManager)getSystemService(Context.LOCATION\u服务);
chk=getLocation();
loadlocation();
如果(chk!=“假”){
字符串foo=chk;
String[]split=foo.split(“”);
lt=拆分[0];
lg=拆分[1];
Toast.makeText(getApplicationContext(),chk,Toast.LENGTH_LONG.show();
}
}
私有void loadlocation(){
final RequestQueue RequestQueue=Volley.newRequestQueue(getposition.this);
StringRequest StringRequest=new StringRequest(Request.Method.POST,URL\u登录,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
tv1.setText(应答);
tv2.setText(响应);
requestQueue.stop();
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
tv1.setText(“出了点问题/”);
错误。printStackTrace();
requestQueue.stop();
}
})
{
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“用户id”,“12”);
参数put(“用户”,lt);
参数put(“用户液化天然气”,lg);
返回参数;
}
};
getInstance(getposition.this).addToRequestQueue(stringRequest);
}
公共字符串getLocation(){
if(ActivityCompat.checkSelfPermission(这是Manifest.permission.ACCESS\u FINE\u位置)
!=PackageManager.PERMISSION\u已授予和&ActivityCompat.checkSelfPermission(此,Manifest.PERMISSION.ACCESS\u位置)
!=PackageManager.权限(已授予)
ActivityCompat.requestPermissions(这是一个新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},REQUEST\u LOCATION);
否则{
Location Location=locationManager.getLastKnownLocation(locationManager.NETWORK\u提供程序);
如果(位置!=null){
double latt=location.getLatitude();
double lng=location.getLongitude();
lat_long=latt+“_”+lng;
tv1.setText(“纬度:+latt”);
tv2.setText(“经度:+lng”);
}否则{
lat_long=“假”;
tv1.setText(“无法找到正确的位置”);
tv2.setText(“无法找到正确的位置”);
}
}
返回拉特朗;
}
}
这是我的登录类:
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.vshine.neuron.riseshine.VolleySingleton;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.vshine.neuron.riseshine.MainActivity;
import com.vshine.neuron.riseshine.R;
import com.vshine.neuron.riseshine.getregistered;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class login extends AppCompatActivity {
Button login_button;
ImageButton GetRegistered;
EditText userName_edt, password_edt;
String user_name, password;
Context mctx;
String json_response;
String URL_LOGIN = "api"
public static final String PREFS_NAME = "MyPreferenceFiles";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mctx = this;
initBasic();
initLogin();
}
private void initBasic() {
login_button = (Button) findViewById(R.id.login_button);
GetRegistered = (ImageButton) findViewById(R.id.register);
userName_edt = (EditText) findViewById(R.id.user_name);
password_edt = (EditText) findViewById(R.id.password);
}
private void initLogin() {
GetRegistered.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(login.this,getregistered.class);
startActivity(intent);
}
});
login_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user_name = userName_edt.getText().toString();
password = password_edt.getText().toString();
if (user_name.trim().equals("") || password.trim().equals("")) {
ShowToastMessage("Please enter the credentials properly");
}
else {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
//converting response to json object
JSONObject obj = new JSONObject(response);
//if no error in response
// json_response = j_obj.getString("message");
if (response!= null) {
// Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
json_response = obj.getString("message");
// ShowToastMessage(json_response);
//getting the user from the response
JSONObject userJson = obj.getJSONObject("response");
//creating a new user object
String u_id = String.valueOf(userJson.getInt("id"));
// ShowToastMessage(u_id);
//storing the user in shared preferences
SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("User_id", u_id);
editor.commit();
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
} else {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("user_name", user_name);
params.put("password", password);
return params;
}
};
VolleySingleton.getInstance(login.this).addToRequestQueue(stringRequest);
}
}
});
}
private void ShowToastMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.Toast;
导入com.vshine.neuron.riseshine.VolleySingleton;
导入com.android.volley.AuthFailureError;
导入com.android.volley.Request;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.StringRequest;
导入com.vshine.neuron.riseshine.main活动;
导入com.vshine.neuron.riseshine.R;
导入com.vshine.neuron.riseshine.getregistered;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.HashMap;
导入java.util.Map;
公共类登录扩展了AppCompatActivity{
按钮登录按钮;
注册图像按钮;
EditText用户名\u edt,密码\u edt;
字符串用户名、密码;
上下文mctx;
字符串json_响应;
字符串URL\u LOGIN=“api”
公共静态最终字符串PREFS_NAME=“MyPreferenceFiles”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u登录);
mctx=这个;
initBasic();
initLogin();
}
private void initBasic(){
login_按钮=(按钮)findViewById(R.id.login_按钮);
GetRegistered=(ImageButt)