0

我想制作一个测试应用程序,在调用时执行几行代码,然后自动退出。我希望所有这些都在 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();
}

问题是,如何在 onCreate() 中退出应用程序?我试过“this.finish()”但没有用。

任何人都可以帮忙吗?

编辑 我同意问题可能是由线程引起的。解决后会发布我的答案。

4

5 回答 5

1

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.

于 2012-06-06T09:20:19.950 回答
0

您可以在活动中使用 AsynTask,只需将此类放在活动的同一文件中

=> 在 onPostExecute() 方法中,您将测试您的处理是否完成以关闭您的活动。

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();
        }
    }
}
于 2012-06-07T00:30:37.580 回答
0

试试这个

完成正在 onCreate() 和 onResume() 中工作。您的代码中还有另一个问题

 @Override
    protected void onResume()
    {
     super.onResume();
     finish();
    }
于 2012-06-06T09:14:54.367 回答
0

尝试

@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();
}
于 2012-06-06T09:15:12.823 回答
0

也许您正在尝试做的是一项服务?
看看这里:http: //developer.android.com/reference/android/app/Service.html

于 2012-06-06T09:15:45.490 回答