我有一个应用程序位置监听器,当用户进入列表中任意点的指定距离时,会弹出警报警报。我想调用一个对话框片段,而不是吐司弹出窗口,当用户选择是时,将用户转移到测验活动中,根据他们的位置向他们询问问题。
简而言之,我希望能够在我的位置监听器活动中显示此对话框片段,而不是toast。到目前为止,我已经尝试了
DialogFragment dialog = new LocationDialog();
showDialog(dialog);
代替toast警报,但我收到错误“方法showDialog(DialogFragment)未定义QLocationListener类型。我已经在各种教程,指南和Google的android文档中循环,但没有用,所以一些指导非常感谢。
我的所有代码都作为独立的应用程序运行我只是努力通过对话框片段将位置监听器+主要活动链接到测验。我也希望能够将位置信息传递给捆绑中的测验,以便知道要显示哪些问题和答案,但这是另一天的任务......
主要活动
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
public class MainActivity extends Activity {
private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1;
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000;
private LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
QLocationListener qLL = new QLocationListener();
qLL.parentActivity = this;
// create the hard-coded list of points of interest
ArrayList<QuizContent> pointList = new ArrayList<QuizContent>();
// ExampleA
QuizContent MapPoint = new QuizContent(25,5, "example question?", "a", "b","c","d", "a");
// ExampleB
QuizContent MapPoint2 = new QuizContent(26,5, "example question?", "a", "b","c","d", "a");
// ExampleC
QuizContent MapPoint3 = new QuizContent(27,5, "example question?", "a", "b","c","d", "a");
pointList.add(MapPoint);
pointList.add(MapPoint2);
pointList.add(MapPoint3);
// now set up the location manager and listener
qLL.pointList = pointList;
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATE,
MINIMUM_DISTANCECHANGE_FOR_UPDATE,
qLL
);
}
}
QLocation Listener
import java.util.ArrayList;
import android.app.AlertDialog;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.Toast;
import android.widget.TextView;
public class QLocationListener implements LocationListener {
public MainActivity parentActivity ;
// this is my list of quiz content (questions, answers, locations)
public ArrayList<QuizContent> pointList;
// this method is called when the location is changed
public void onLocationChanged(Location location) {
// now measure distance from all locations in quiz list
for (int i=0;i<pointList.size();i++){
QuizContent gp = pointList.get(i);
Location fixedLoc = new Location("one");
Double lat = Double.valueOf(String.valueOf(gp.getLatitude()));
Double lng = Double.valueOf(String.valueOf(gp.getLongitude()));
fixedLoc.setLatitude(lat);
fixedLoc.setLongitude(lng);
Log.i("location",lat+" "+location.getLatitude());
Log.i("location",lng+" "+location.getLongitude());
// calculate distance
float distance = location.distanceTo(fixedLoc);
if (i == 0) { // this is location a
if (distance < 10) {
DialogFragment dialog = new LocationDialog();
showDialog(dialog);
}
}
if (i == 1) { // this is location b
if (distance < 10) {
Toast.makeText(parentActivity.getBaseContext(),
"Welcome to location b", Toast.LENGTH_LONG).show();
}
}
if (i == 3) { // this is location c
if (distance < 10) {
Toast.makeText(parentActivity.getBaseContext(),
"Welcome to location c", Toast.LENGTH_LONG).show();
}
}
}
}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
对话片段
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class LocationDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setMessage("Quiz Location Found, answer the question?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(getActivity(), Quiz.class));
// take the quiz!
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return alertDialog.create();
}
}
答案 0 :(得分:0)
您需要在活动/应用程序上下文中调用showDialog
尝试:
来自活动:
QLocationListener qLocationListener= new QLocationListener(this)
在QLocationListener中:
Activity act;
QLocationListener(Activity a)
{
//Constructor
act=a;
}
然后最后调用showDialog:
act.showDialog(dialog);