Java 如何在android中的另一个类中创建警报对话框
我试图在android的另一个类中创建一个警报对话框。这是一个从另一个类传递参数的类。这是一个Set_Location类,它获取一个地方的gps位置,并将这些值传递给BackgroundWorker类 设置位置类别:Java 如何在android中的另一个类中创建警报对话框,java,android,android-alertdialog,Java,Android,Android Alertdialog,我试图在android的另一个类中创建一个警报对话框。这是一个从另一个类传递参数的类。这是一个Set_Location类,它获取一个地方的gps位置,并将这些值传递给BackgroundWorker类 设置位置类别: public class Set_Location extends AppCompatActivity { private ImageButton imgbutton; private TextView textView; private LocationManager locat
public class Set_Location extends AppCompatActivity {
private ImageButton imgbutton;
private TextView textView;
private LocationManager locationManager;
private LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set__location);
imgbutton = (ImageButton) findViewById(R.id.PanicBtn);
textView = (TextView) findViewById(R.id.panictxt);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
String lat= String.valueOf(location.getLatitude());
String lon= String.valueOf(location.getLongitude());
String type="location";
textView.append(" \n "+lat+", "+lon);
BackgroundWorker back = new BackgroundWorker(this);
back.execute(type,lat,lon);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
configureButton();
}
private void configureButton() {
imgbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
locationManager.requestLocationUpdates("gps",5000,0,locationListener);
}
});
}
}
以下是工作人员类的背景:
public class BackgroundWorker extends AsyncTask <String, Void, String>{
AlertDialog alertDialog;
Context ctx;
LocationListener locationListener;
BackgroundWorker(Context ctx){
this.ctx = ctx;
}
public BackgroundWorker(LocationListener locationListener) {
this.locationListener = locationListener;
}
@Override
protected String doInBackground(String... params) {
String type=params[0];
String Login_Url= "http://192.168.43.254/login.php";
String Login_after_registration_Url= "http://192.168.43.254/login2.php";
String Register_Url= "http://192.168.43.254/register.php";
String Register_Url2= "http://192.168.43.254/register2.php";
if(type.equals("location")){
try {
String latitude=params[1];
String longitude=params[2];
URL url= new URL(Register_Url2);
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("latitude","UTF-8")+"="+URLEncoder.encode(latitude,"UTF-8")+"&"
+URLEncoder.encode("longitude","UTF-8")+"="+URLEncoder.encode(longitude,"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();
}
}
@Override
protected void onPreExecute() {
alertDialog= new AlertDialog.Builder(ctx).create();
alertDialog.setTitle(" Status");
}
@Override
protected void onPostExecute(String result) {
if(result == null)
{
Toast.makeText(ctx, "connection error please try again", Toast.LENGTH_LONG).show();
}
else if(result.contains("Registration")) // msg you get from success like "Login Success"
{
alertDialog.setMessage(result);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(ctx,Login_after_Registration.class);
ctx.startActivity(i);
}
});
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
else if(result.contains("Login")) // msg you get from success like "Login Success"
{
Intent i = new Intent(ctx,Welcome.class);
ctx.startActivity(i);
alertDialog.setMessage(result);
alertDialog.show();
}
else if(result.contains("Welcome")) // msg you get from success like "Login Success"
{
Intent i = new Intent(ctx,Set_Location.class);
ctx.startActivity(i);
alertDialog.setMessage(result);
alertDialog.show();
}
else if(result.contains("Invalid")){
alertDialog.setMessage(result);
alertDialog.show();
}
else{
alertDialog.setMessage(result);
alertDialog.show();
}
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
在这里:
alertDialog= new AlertDialog.Builder(ctx).create();
alertDialog.setTitle(" Status");
有人能帮我纠正这个错误吗 只需初始化BackgroundWorker变量,如下所示:
BackgroundWorker back = new BackgroundWorker(Set_Location.this);
back.execute(type,lat,lon);
将您的错误以文本形式与问题共享,而不是以文本形式image@NileshRathod我现在已经更新了这个问题,请看一看为什么在类中有两个构造函数,而您只使用一个构造函数?请尝试我提供的代码作为答案。@M.Prokhorov第一个是上下文,另一个是位置。这样做不对吗?
alertDialog= new AlertDialog.Builder(ctx).create();
alertDialog.setTitle(" Status");
BackgroundWorker back = new BackgroundWorker(Set_Location.this);
back.execute(type,lat,lon);