Android 满足条件时自动启动异步任务

Android 满足条件时自动启动异步任务,android,android-asynctask,android-alertdialog,Android,Android Asynctask,Android Alertdialog,当你点击一个按钮时,这个应用程序会搜索你周围的名胜。流程如下: 你点击搜索按钮 它检查互联网连接,如果没有,你会得到一个对话框 您需要启用internet 你点击搜索按钮 它会检查定位服务,如果没有,就会出现一个对话框询问您 启用位置跟踪并进入Gps设置屏幕 然后返回,单击“搜索”按钮,一个异步任务开始 执行作业(搜索和显示) 我需要做的是消除按钮搜索,所以它会一步一步地自动完成所有操作。因此,这就像: 启动应用程序 它检查互联网连接,如果没有,你会得到一个对话框 您需要启用interne

当你点击一个按钮时,这个应用程序会搜索你周围的名胜。流程如下:

  • 你点击搜索按钮
  • 它检查互联网连接,如果没有,你会得到一个对话框 您需要启用internet
  • 你点击搜索按钮
  • 它会检查定位服务,如果没有,就会出现一个对话框询问您 启用位置跟踪并进入Gps设置屏幕
  • 然后返回,单击“搜索”按钮,一个异步任务开始 执行作业(搜索和显示)
我需要做的是消除按钮搜索,所以它会一步一步地自动完成所有操作。因此,这就像:

  • 启动应用程序
  • 它检查互联网连接,如果没有,你会得到一个对话框 您需要启用internet
  • 启用internet时,它会检查位置服务,如果没有,则会出现一个对话框,要求您启用位置跟踪,并将您带到Gps设置屏幕
  • 然后启动异步任务
我原以为一个警报对话框会使活动暂停,我可以检查所有条件,但看起来没有。我应该如何解决这个问题?请随时询问更多细节

忘了提及我只希望在应用程序第一次启动时完成一次

这是我的密码:

public class MainActivity extends Activity {
    //variables

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.list);
        button = (Button) findViewById(R.id.btn_show_map);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                cd = new ConnectionDetector(MainActivity.this);

                // Check if Internet present
                isInternetPresent = cd.isConnectingToInternet();
                if (!isInternetPresent) {
                    // Internet Connection is not present
                    alert.showAlertDialog(MainActivity.this, "Internet Connection Error", "Please connect to a working Internet connection", false);
                    return;
                }

                // creating GPS Class object
                gps = new GPSTracker(MainActivity.this);

                // check if GPS location can get
                if (gps.canGetLocation()) {
                    Log.d("Your Location", "latitude:" + gps.getLatitude() + ", longitude: " + gps.getLongitude());
                } else {
                    // Can't get user's current location
                    gps.showSettingsAlert();
                    return;
                }

                new LoadPlaces().execute();
            }
        });
    }

    class LoadPlaces extends AsyncTask<String, String, String> {
        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage(Html.fromHtml("<b>Search</b><br/>Loading Places..."));
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            Long t = Calendar.getInstance().getTimeInMillis();
            while (!gps.hasLocation && Calendar.getInstance().getTimeInMillis() - t < 60000) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            // get the places

            return null;
        }

        protected void onPostExecute(String file_url) {

            // dismiss the dialog after getting all products
            pDialog.dismiss();
            gps.stopUsingGPS();

            // display the results
        }
    }
}
公共类MainActivity扩展活动{
//变数
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.list);
按钮=(按钮)findViewById(R.id.btn\u show\u map);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
cd=新的ConnectionDetector(MainActivity.this);
//检查互联网是否存在
isInternetPresent=cd.isConnectingToInternet();
如果(!isInternetPresent){
//Internet连接不存在
alert.showAlertDialog(MainActivity.this,“Internet连接错误”,“请连接到工作的Internet连接”,false);
返回;
}
//创建GPS类对象
gps=新的GP斯特拉克(MainActivity.this);
//检查是否可以获得GPS定位
if(gps.canGetLocation()){
Log.d(“您的位置”,“纬度:+gps.getLatitude()+”,经度:+gps.getLatitude());
}否则{
//无法获取用户的当前位置
gps.showSettingsAlert();
返回;
}
新加载位置().execute();
}
});
}
类LoadPlaces扩展了异步任务{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MainActivity.this);
pDialog.setMessage(Html.fromHtml(“搜索
加载位置…”); pDialog.setUndeterminate(假); pDialog.setCancelable(假); pDialog.show(); } 受保护的字符串doInBackground(字符串…args){ Long t=Calendar.getInstance().getTimeInMillis(); 而(!gps.hasLocation&&Calendar.getInstance().getTimeInMillis()-t<60000){ 试一试{ 睡眠(1000); }捕捉(中断异常e){ e、 printStackTrace(); } } //去那些地方 返回null; } 受保护的void onPostExecute(字符串文件\u url){ //获取所有产品后关闭对话框 pDialog.disclose(); 停止使用gps(); //显示结果 } } }
更新:我在想可能有一个更好的选择,使用不同的线程,就像我在下面写的那样。或者可能是通过服务或接收器。。有什么想法吗

Thread th = new Thread() {
            boolean allEnabled = false;

            @Override
            public void run() {
                Long t = Calendar.getInstance().getTimeInMillis();
                while (!allEnabled && Calendar.getInstance().getTimeInMillis() - t < 120000) {
                    try {
                        isInternetPresent = cd.isConnectingToInternet();
                        if (!isInternetPresent) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    alert.showAlertDialog(MainActivity.this, "Internet Connection Error", "Please connect to a working Internet connection", false);
                                }
                            });
                        } else if (!gps.canGetLocation()) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    gps.showSettingsAlert();
                                }
                            });
                        } else {
                            allEnabled = true;
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    new LoadPlaces().execute();
                                }
                            });
                        }
                        Thread.sleep(20000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        th.start();
Thread th=新线程(){
布尔allEnabled=false;
@凌驾
公开募捐{
Long t=Calendar.getInstance().getTimeInMillis();
而(!allEnabled&&Calendar.getInstance().getTimeInMillis()-t<120000){
试一试{
isInternetPresent=cd.isConnectingToInternet();
如果(!isInternetPresent){
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
alert.showAlertDialog(MainActivity.this,“Internet连接错误”,“请连接到工作的Internet连接”,false);
}
});
}如果(!gps.canGetLocation()),则为else{
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
gps.showSettingsAlert();
}
});
}否则{
allEnabled=true;
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
新加载位置().execute();
}
});
}
睡眠(20000);
}捕捉(中断异常e){
e、 printStackTrace();
    new AlertDialog.Builder(this)
            .setTitle("Problem")
            .setMessage(
                    "Please Check your *whatever is wrong* ")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    //Do the next activity

                    dialog.dismiss();
                }
            }).show();