当杀死我的进程时,它进入系统

时间:2016-03-21 11:36:07

标签: android kill-process onbackpressed

假设我目前在主页上。现在我点击后退按钮显示警告对话框询问是否退出。是按钮我的进程被杀死但是没有发生。我重定向到上次开放活动。

Homepage.java

public class HomePage extends BaseActivity{

    String member_type;
    GridviewAdapter mAdapter;
    GridView gridView;
    ArrayList<Item> gridArray = new ArrayList<Item>();
    ArrayList<Item> listviewArray = new ArrayList<Item>();
    ListView lv;
    ListviewAdapter2 adapter;
    listview_data superDistributorlist[],retailerlist[];
    DatabaseHelper db;
    String register,topup,mdebit,smspin,voucher,complaint,hotel,flight,bus,complaint_status,changepwd,reports,change_mobileno,postpaid,prepaid,dth,comingsoon;

    /*public final static Integer[] imageResIds = new Integer[] {
            R.drawable.postpaid,
            R.drawable.prepaid,
            R.drawable.dth2,
            R.drawable.table2,
            R.drawable.memberdebit,
            R.drawable.complaintstatus,
            R.drawable.complaint,
            R.drawable.changepwd,
            R.drawable.changesmspin,
            R.drawable.mobilenochangeblue,
            R.drawable.changesmspin,
            R.drawable.changesmspin,
            R.drawable.changesmspin,};*/



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homepage);
        if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler))
        {
            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(HomePage.this));
        }

        db = new DatabaseHelper(HomePage.this);

        register = getResources().getString(R.string.txt_Registration);
        topup = getResources().getString(R.string.txt_topup);
        mdebit = getResources().getString(R.string.txt_mdebit);
        smspin = getResources().getString(R.string.txt_changesmspin);
        voucher = getResources().getString(R.string.txt_voucher);
        complaint = getResources().getString(R.string.txt_complaint);
        complaint_status = getResources().getString(R.string.txt_complaint_status);
        changepwd = getResources().getString(R.string.txt_changepwd);
        reports = getResources().getString(R.string.txt_reports);
        change_mobileno =  getResources().getString(R.string.changemobileno);
        postpaid = getResources().getString(R.string.lbl_postpaid);
        prepaid = getResources().getString(R.string.lbl_prepaid);
        dth = getResources().getString(R.string.lbl_dth);
        comingsoon = getResources().getString(R.string.comingsoon);
        bus = getResources().getString(R.string.busbooking);
        flight = getResources().getString(R.string.flightbooking);
        hotel = getResources().getString(R.string.hotelbooking);

        lv = (ListView) findViewById(R.id.list);
        gridView = (GridView) findViewById(R.id.gridView1);
        //frame1 = (LinearLayout)findViewById(R.id.frame1);
        //frame2= (LinearLayout)findViewById(R.id.frame2);
        if(required) {
            db.deleteData("ChildUserInfo");
            AsynctaskMemberList atw1 = new AsynctaskMemberList(HomePage.this, "FIRMNAME", "MEMBERNAME", "MOBILENO", "COMMISSION", "BALANCE", "MEMBERID", "MEMBERCODE");
            try {
                ArrayList<MemebrListGeSe> trnreport = atw1.execute("GetMemberList").get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
            required = false;
        }

        member_type = ResponseString.getMemberType();
        try {
            Log.d("Member Type", "" + member_type);
            if (member_type.equals("1") || member_type.equals("2")) {

                gridView.setVisibility(View.GONE);
                //frame2.setVisibility(View.GONE);
                Bitmap registerIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.registration,48,48);
                Bitmap topupIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.topup, 48,48);
                Bitmap memberdebitIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.memberdebit,48,48);
                Bitmap voucherIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.voucher,48,48);
                Bitmap complaintIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.complaint_registration,48,48);
                Bitmap complaint_statusIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.complaint_status,48,48);
                Bitmap mobilenochangeIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.changemoborange,48,48);
                Bitmap changepwdIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.changepassword,48,48);
                Bitmap changesmspinIcon =decodeSampledBitmapFromResource(this.getResources(), R.drawable.changesmspin,48,48);
                Bitmap reportsIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.reports2,48,48);

                listviewArray.add(new Item(registerIcon,register));
                listviewArray.add(new Item(topupIcon,topup));
                listviewArray.add(new Item(memberdebitIcon, mdebit));
                listviewArray.add(new Item(voucherIcon,voucher));
                listviewArray.add(new Item(complaintIcon,complaint));
                listviewArray.add(new Item(complaint_statusIcon,complaint_status));
                listviewArray.add(new Item(mobilenochangeIcon, change_mobileno));
                listviewArray.add(new Item(changepwdIcon, changepwd));
                listviewArray.add(new Item(changesmspinIcon, smspin));
                listviewArray.add(new Item(reportsIcon, reports));

                /*superDistributorlist = new listview_data[]
                        {
                                new listview_data(R.drawable.adduser, register),
                                new listview_data(R.drawable.topuptransfer, topup),
                                new listview_data(R.drawable.memberdebit, mdebit),
                                new listview_data(R.drawable.voucher, voucher),
                                new listview_data(R.drawable.complaint, complaint),
                                new listview_data(R.drawable.complaintstatus, complaint_status),
                                new listview_data(R.drawable.changesmspin, smspin),
                                new listview_data(R.drawable.changepwd, changepwd),
                                new listview_data(R.drawable.mobilenochange, change_mobileno),
                                new listview_data(R.drawable.table2, reports)
                        };*/
                adapter = new ListviewAdapter2(HomePage.this, R.layout.listview_item_row,listviewArray);
                lv.setAdapter(adapter);
                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        LinearLayout ll = (LinearLayout) view; // get the parent layout view
                        TextView tv = (TextView) ll.findViewById(R.id.txtTitle); // get the child text view
                        final String item = tv.getText().toString();
                        Intent i;

                        if (item.equals(getResources().getString(R.string.txt_Registration))) {
                            i = new Intent(HomePage.this, Registration.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_topup))) {
                            i = new Intent(HomePage.this, TopupTransfer.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_changesmspin))) {
                            Log.d("Item2 ", "" + item);
                            i = new Intent(HomePage.this, ChangeSmspin.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_changepwd))) {
                            Log.d("Item3 ", "" + item);
                            i = new Intent(HomePage.this, ChangePwd.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_complaint))) {
                            i = new Intent(HomePage.this, ComplaintRegister.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_reports))) {
                            i = new Intent(HomePage.this, ReportList.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_complaint_status))) {
                            i = new Intent(HomePage.this, ComplaintStatus.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_mdebit))) {
                            i = new Intent(HomePage.this, memberdebit.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.changemobileno))) {
                            i = new Intent(HomePage.this, ChangeMobNo.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }

                    }

                });
            }
            else
            {
                lv.setVisibility(View.INVISIBLE);
                //frame1.setVisibility(View.GONE);

                if(required1) {
                    db.deleteData("OperatorList");
                    AsynctaskOperatorList atw1 = new AsynctaskOperatorList(HomePage.this, "SERVICEID", "SERVICENAME", "SMSCODE", "SERVICETYPE");
                    ArrayList<OperatorListGeSe> trnreport = atw1.execute("GetServiceList").get();
                    required1 = false;
                }
                Bitmap postpaidIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.postpaid,48,48);
                Bitmap prepaidIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.prepaidorange, 48,48);
                Bitmap dthIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.dthblue,48,48);
                Bitmap reportIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.reportsorange,48,48);
                Bitmap complaint_statusIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.complaint_status,48,48);
                Bitmap complaintIcon =decodeSampledBitmapFromResource(this.getResources(), R.drawable.complaint_registration,48,48);
                Bitmap chngmobnoIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.changemob,48,48);
                Bitmap chngpwdIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.changepassword,48,48);
                Bitmap chngsmspinIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.changesmspin,48,48);
                Bitmap placeholderIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.placeholder,48,48);
                Bitmap busIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.bus,48,48);
                Bitmap flightIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.flight,48,48);
                Bitmap hotelIcon = decodeSampledBitmapFromResource(this.getResources(), R.drawable.hotel,48,48);


                /*Bitmap postpaidIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.postpaid);
                Bitmap prepaidIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.prepaid);
                Bitmap dthIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.dth2);
                Bitmap reportIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.table2);
                Bitmap debitIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.memberdebit);
                Bitmap complaint_statusIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.complaintstatus);
                Bitmap complaintIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.complaint);
                Bitmap chngpwdIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.changepwd);
                Bitmap chngsmspinIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.changesmspin);
                Bitmap chngmobnoIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.mobilenochangeblue);*/

                gridArray.add(new Item(prepaidIcon,prepaid));
                gridArray.add(new Item(dthIcon, dth));
                gridArray.add(new Item(postpaidIcon,postpaid));
                gridArray.add(new Item(chngmobnoIcon, change_mobileno));
                gridArray.add(new Item(chngsmspinIcon, smspin));
                gridArray.add(new Item(chngpwdIcon, changepwd));
                gridArray.add(new Item(complaintIcon, complaint));
                gridArray.add(new Item(complaint_statusIcon,complaint_status));
                gridArray.add(new Item(reportIcon,reports));
                gridArray.add(new Item(busIcon, bus));
                gridArray.add(new Item(flightIcon, flight));
                gridArray.add(new Item(hotelIcon, hotel));

                /*retailerlist = new listview_data[]
                        {
                                new listview_data(R.drawable.postpaid, postpaid),
                                new listview_data(R.drawable.prepaid, prepaid),
                                new listview_data(R.drawable.dth2, dth),
                                new listview_data(R.drawable.table2, reports),
                                new listview_data(R.drawable.memberdebit, mdebit),
                                new listview_data(R.drawable.complaintstatus, complaint_status),
                                new listview_data(R.drawable.complaint, complaint),
                                new listview_data(R.drawable.changepwd, changepwd),
                                new listview_data(R.drawable.changesmspin, smspin),
                                new listview_data(R.drawable.mobilenochange, change_mobileno),
                                new listview_data(R.drawable.mobilenochange, change_mobileno),
                                new listview_data(R.drawable.mobilenochange, change_mobileno),
                                new listview_data(R.drawable.mobilenochange, change_mobileno),
                                new listview_data(R.drawable.mobilenochange, change_mobileno),
                                new listview_data(R.drawable.mobilenochange, change_mobileno),
                                new listview_data(R.drawable.changepwd, changepwd),
                                new listview_data(R.drawable.changepwd, changepwd),
                                new listview_data(R.drawable.changepwd, changepwd),
                                new listview_data(R.drawable.changepwd, changepwd),
                                new listview_data(R.drawable.changepwd, changepwd),
                                new listview_data(R.drawable.changepwd, changepwd),
                                new listview_data(R.drawable.changepwd, changepwd)
                        };*/

                gridView = (GridView) findViewById(R.id.gridView1);
                mAdapter = new GridviewAdapter(HomePage.this,R.layout.gridview_row, gridArray);
                gridView.setAdapter(mAdapter);

                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        LinearLayout ll = (LinearLayout) view;
                        TextView tv = (TextView) ll.findViewById(R.id.item_text);
                        final String item = tv.getText().toString();
                        Intent i;
                        if (item.equals(getResources().getString(R.string.lbl_postpaid))) {
                            i = new Intent(HomePage.this, PostpaidList.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                        } else if (item.equals(getResources().getString(R.string.lbl_prepaid))) {
                            i = new Intent(HomePage.this, PrepaidList.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                        } else if (item.equals(getResources().getString(R.string.lbl_dth))) {
                            i = new Intent(HomePage.this, DTHList.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                        } else if (item.equals(getResources().getString(R.string.txt_reports))) {
                            i = new Intent(HomePage.this, ReportList.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                        } else if (item.equals(getResources().getString(R.string.txt_changepwd))) {
                            Log.d("Item3 ", "" + item);
                            i = new Intent(HomePage.this, ChangePwd.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        } else if (item.equals(getResources().getString(R.string.txt_complaint))) {
                            i = new Intent(HomePage.this, ComplaintRegister.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();

                        } else if (item.equals(getResources().getString(R.string.txt_changesmspin))) {
                            Log.d("Item2 ", "" + item);
                            i = new Intent(HomePage.this, ChangeSmspin.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.changemobileno))) {
                            i = new Intent(HomePage.this, ChangeMobNo.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_mdebit))) {
                            i = new Intent(HomePage.this, memberdebit.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }else if (item.equals(getResources().getString(R.string.txt_complaint_status))) {
                            i = new Intent(HomePage.this, ComplaintStatus.class);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
                            startActivity(i);
                            finish();
                        }
                    }
                });
            }
        }catch (Exception e)
        {
            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(HomePage.this));
        }
    }
    /*@Override
      public boolean onKeyDown(int keyCode,KeyEvent event) {

          if(keyCode == KeyEvent.KEYCODE_BACK) {
              doExit();
          }
          return super.onKeyDown(keyCode, event);
      }*/

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        //doExit();
        new AlertDialog.Builder(this)
                .setTitle(R.string.app_name)
                .setMessage("Are you sure you want to exit?")
                .setNegativeButton(android.R.string.no, null)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        finish();
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                }).create().show();

    }
    private void doExit() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomePage.this);
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
                android.os.Process.killProcess(android.os.Process.myPid());

            }
        });
        alertDialog.setNegativeButton("No", null);
        alertDialog.setMessage("Do you want to exit?");
        alertDialog.setTitle(R.string.app_name);
        alertDialog.show();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_signout:
                logout(HomePage.this);
                break;
            default:
                break;
        }
        return true;
    }
    @Override
    protected void onResume() {
        super.onResume();

        required1 = true;
        required = true;

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        required1 = true;
        required = true;

    }
    @Override
    protected void onPause() {
        super.onPause();
        required1 = true;
        required = true;
        if ((BaseActivity.pleaseWaitDialog != null) && BaseActivity.pleaseWaitDialog.isShowing()) {
            BaseActivity.pleaseWaitDialog.dismiss();
            BaseActivity.pleaseWaitDialog = null;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

当你打电话给你写的家庭活动时

Intent intent = new Intent(getApplicationContext(), Home.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);

其他明智的用途,你使用finish();在哪里使用

System.exit(1);

完成申请

答案 1 :(得分:0)

您可以在活动代码

中的清单文件中尝试此nohistory属性
android:noHistory="true"
像这样

<activity
    android:name=".Homeactivity"
    android:label="" 
    android:noHistory="true">
</activity>

<强>(或)

然后onbackpress函数指定以下命令

Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
finish();

<强>(或)

如果您非常需要退出,请使用System.exit(0)强烈不推荐,并且通常会说您有应用程序的架构问题。