首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >完成()在结束活动时不工作

完成()在结束活动时不工作
EN

Stack Overflow用户
提问于 2014-05-04 10:49:54
回答 2查看 2.2K关注 0票数 0

我正在做的事情 ::我试图使用代码关闭这个活动

代码语言:javascript
复制
if (getIntent().getBooleanExtra("LOGOUT", false))
{
      SplashActivity.this.finish();
}

正在发生的事情::

  • 我的控制到达了这一行(我正在传递以前活动的意图),但是finish不起作用。
  • 为什么会发生这种事?
  • 我怎样才能克服这一切?

SplashActivity.java

代码语言:javascript
复制
public class SplashActivity extends FragmentActivity {
    /** Called when the activity is first created. */


    JSONObject jsonobject;  
    JSONArray jsonarray;
    private String Content;
    DatabaseAdapter databaseHelper;
    TextView txtSplashTitle,txtSplashDesc;
    Cursor cursor;
    LocationManager locationManager;
    LocationListener locationListener;
    public static String srcLatitude;
    public static String srcLongitude;
    public static String destLatitude;
    public static String destLongitude;
    public static String buffetOfferId;
    private String tag_json_obj = "jobj_req";
    private ProgressDialog pDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getIntent().getBooleanExtra("LOGOUT", false))
        {
            SplashActivity.this.finish();
        }

        //Remove title bar
        this.requestWindowFeature((int) Window.FEATURE_NO_TITLE);
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //set content view AFTER ABOVE sequence (to avoid crash)
        setContentView(R.layout.splash);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            showDialog();
        }
        //Delete database if it exists
        SplashActivity.this.deleteDatabase(FindMyBuffetConstants.DATABASE_NAME);
        databaseHelper = new DatabaseAdapter(this);


        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);

        JsonObjReqWithVolly();
    }

    private void JsonObjReqWithVolly() {
        showProgressDialog();
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
                Const.URL_JSON_OBJECT, null,
                new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d("VollyResponse", response.toString());
                parseBuffetTableJSON(response);
                distanceCalculation();
                parseBuffetDetailsTableJSON(response);
                hideProgressDialog();
                startingNewActivity();
            }

            private void parseBuffetTableJSON(JSONObject response) {
                //jsonobject = new JSONObject(response);
                try {
                    jsonobject = response.getJSONObject("findmybuffet");
                    jsonarray = jsonobject.getJSONArray("buffets");
                    for (int i = 0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        map.put("buf_off_id", jsonobject.getString("buf_off_id"));
                        map.put("from_time", jsonobject.getString("from_time"));
                        map.put("to_time", jsonobject.getString("to_time"));
                        map.put("online_price", jsonobject.getString("online_price"));
                        map.put("reserved_price", jsonobject.getString("reserved_price"));
                        map.put("buf_image", jsonobject.getString("buf_image"));
                        map.put("res_name", jsonobject.getString("res_name"));
                        map.put("rating", jsonobject.getString("rating"));
                        map.put("latitude", jsonobject.getString("latitude"));
                        map.put("longitude", jsonobject.getString("longitude"));
                        map.put("buf_type_name", jsonobject.getString("buf_type_name"));
                        map.put("from_date", jsonobject.getString("from_date"));
                        map.put("to_date", jsonobject.getString("to_date"));
                        map.put("city_id", jsonobject.getString("city_id"));
                        map.put("city_name", jsonobject.getString("city_name"));
                        map.put("meal_type_id", jsonobject.getString("meal_type_id"));
                        map.put("meal_type_name", jsonobject.getString("meal_type_name"));
                        map.put("buf_desc", jsonobject.getString("buf_desc"));
                        map.put("distance", jsonobject.getString("distance"));
                        //Log.d("----$$$----", map.toString());
                        //Calling database 
                        databaseHelper.addDataToBuffetTable(map);
                    }   
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            private void parseBuffetDetailsTableJSON(JSONObject response) {
                //jsonobject = new JSONObject(response);
                try {
                    jsonobject = response.getJSONObject("findmybuffet");
                    jsonarray = jsonobject.getJSONArray("buf_meal_det");
                    for (int i = 0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        map.put("buf_off_id", jsonobject.getString("buf_off_id"));
                        map.put("menu_type_id", jsonobject.getString("menu_type_id"));
                        map.put("menu_type_name", jsonobject.getString("menu_type_name"));
                        map.put("cuisine_type_name", jsonobject.getString("cuisine_type_name"));
                        map.put("item_name", jsonobject.getString("item_name"));
                        //Log.d("----$$$----", map.toString());
                        //Calling database 
                        databaseHelper.addDataToBuffetDetailsTable(map);
                    }   
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            private void distanceCalculation() {
                String myQuery="SELECT * FROM buffets";
                Cursor cursor = (Cursor) databaseHelper.getAllDataFrmBuffetTable(myQuery);
                cursor.moveToFirst();
                if(cursor.moveToFirst()){
                    do{
                        buffetOfferId=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))); 
                        destLatitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(9)));
                        destLongitude=cursor.getString(cursor.getColumnIndex(cursor.getColumnName(10)));
                        srcLatitude="12.918286";
                        srcLongitude="77.669493";
                        Log.d("---Source------", destLatitude+","+destLongitude);
                        Log.d("---Destination-", srcLatitude+","+srcLongitude);
                        float CalculateDistance=CalculateDistance(srcLatitude,srcLongitude,destLatitude,destLongitude);
                        Log.d("---Result---", CalculateDistance+"");
                        //round float to nearest whole number and then convert into double and converting to kilometers
                        double CalculateDistanceRounded = Math.round(CalculateDistance)/1000;
                        databaseHelper.updateSqLiteDistance(CalculateDistanceRounded,buffetOfferId);
                    }while(cursor.moveToNext());
                }
            }

            private float CalculateDistance(String srcLatitude,
                    String srcLongitude, String destLatitude, String destLongitude) {

                Location locationA = new Location("point A");
                //Convert from string to double and then process
                locationA.setLatitude(Double.parseDouble(destLatitude));
                locationA.setLongitude(Double.parseDouble(destLongitude));
                Location locationB = new Location("point B");
                locationB.setLatitude(Double.parseDouble(srcLatitude));
                locationB.setLongitude(Double.parseDouble(srcLongitude));
                return locationA.distanceTo(locationB);
            }

            private void startingNewActivity() {
                Intent intent=new Intent(SplashActivity.this,MainActivity.class);
                startActivity(intent);
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("VollyResponse", "Error: " + error.getMessage());
                hideProgressDialog();
            }
        }) {
        };
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);
        // Cancelling request
        // ApplicationController.getInstance().getRequestQueue().cancelAll(tag_json_obj);       
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        //Setting fonts for textviews
        checkForGpsAndTurnItOn();
    }

    public void showDialog(){
        //GPS-Dialog
        GpsEnablingDialog gpsAlert=new GpsEnablingDialog();
        gpsAlert.show(getSupportFragmentManager(), "GpsAlert_Tag");
    }

    private void checkForGpsAndTurnItOn() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            Log.d("My-Log-Msg","$-before-showDialog-$");
            showGpsDialog();
            Log.d("My-Log-Msg","$-after-showDialog-$");
        }
    }

    public void showGpsDialog(){
        //GPS-Dialog
        GpsEnablingDialog gpsAlert;
        try {
            gpsAlert = new GpsEnablingDialog();
            gpsAlert.show(getSupportFragmentManager(), "GpsAlert_Tag");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d("My-Log-Msg",e.toString());
            e.printStackTrace();
        }
    }

    private void showProgressDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hideProgressDialog() {
        if (pDialog.isShowing())
            pDialog.hide();
    }

}

{编辑}

代码语言:javascript
复制
Intent intent = new Intent(getActivity(), SplashActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                intent.putExtra("LOGOUT", true);
                startActivity(intent); 
  • 上面的片段来自splash.java旁边的第二个活动
  • 如何在启动SplashActivity.java时将布尔值作为true启动
  • 我所做的就是关闭我的应用程序&返回到android主屏幕。
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-04 11:05:57

尝试将getBoolean方法与Intent一起使用,如:

代码语言:javascript
复制
if ( getIntent().getExtras().getBoolean("LOGOUT") ) {
    SplashActivity.this.finish();
} else {
    Log.v("Boolean LOGOUT","The value is false...");
}  

此外,在启动boolean时,请确保您的SplashActivity的值不是“空”(应该是true)。如果这能解决问题,请告诉我。

票数 0
EN

Stack Overflow用户

发布于 2014-05-04 11:00:03

你所说的“无所事事”是什么意思?你期望会发生什么?

finish()是异步的-它将请求传递给ActivityManager,这反过来将触发整个完成流(完成活动在堆栈-> onPause() -> onStop() -> onSaveInstance().( -> onFinish() )。它不会突然杀死你的应用程序。

这是件好事。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23455326

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档