简体   繁体   中英

Android: auto start an app and exit when finish in onCreate()

I want to make a test app, that when called, execute a few lines of code, and then exit automatically. I want all these done within onCreate().

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("onCreate()");
    try {
        initSocket();        //connect to server
    } catch (IOException e) {
        e.printStackTrace();
    }
    new Thread(new AudioRecordThread()).start();
}

The problem is, how can I exit the app in onCreate()? I tried "this.finish()" but didn't work.

Any one can help?

EDIT I agree that the problem may be caused by thread. Will post my answer when solved.

I think the Problem is your AudioRecordThread which shares the same Process with your App. You could definetly call this.finish() inside onCreate (done this often, never had problems), but that doesn't mean neccesarily that your AudioRecordThread gets killed the same time as your Activity. So without further Information about your Thread, and if it should stay alive on finish of your Activity I cannot give you any advise. If You want that Thread to be alive, after your Activity finishes, a Service is the way to go.

try this one

finish is working in onCreate() & onResume().. there is another problem in your code

 @Override
    protected void onResume()
    {
     super.onResume();
     finish();
    }

try

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.out.println("onCreate()");
    try {
        initSocket();        //connect to server
    } catch (IOException e) {
        e.printStackTrace();
    }
    new Thread(new AudioRecordThread()).start();
    finish();
}

Perhaps what you are trying to do is a service?
Have a look here: http://developer.android.com/reference/android/app/Service.html

You can use an AsynTask in your activity, juste put this class in the Same file of your activity

=> In onPostExecute() methode you will test if your handling is finished to close your activity.

class MyAsynTask extends AsyncTask<Void, Integer, Boolean> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        // Init your variables, in my case it's my Progress Bar
        myprogress = new ProgressDialog(mcontext);
        myprogress.setTitle("Update");
        myprogress.setMessage("Update running ....");

        myprogress.show();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO Auto-generated method stub
        boolean test = false;
        int j = 0;
        for (int i = 0; i < 50; i++) {
            j = i + 10;
            publishProgress(i);
            // Your implemention code to connect to the server
            if (i == 49)
                test = true;
        }

        return test;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        myprogress.setProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (myprogress.isShowing())
            myprogress.dismiss();
        if (result){
            Toast.makeText(mcontext, "I Finished",Toast.LENGTH_SHORT).show();
            yourActivity.this.finish();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM