0

您好我正在尝试开发一个可以删除短信的android应用程序,到目前为止我能够正确删除短信,但是当手机有很多短信时,强制关闭警报框来了,我知道我正在做所有UI线程中的东西,所以我浏览了AsyncTask文章,但无法正确理解将我的短信删除代码放在哪里请帮助我,提前致谢。

这是我的 MainActivity

public class MainActivity extends Activity {

    Button btn;
    /** Called when the activity is first created. */

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

        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,SecondClass.class));
            }
        });
    }
}

这是我的 SecondClass 代码

public class SecondClass extends Activity {
    ProgressDialog pd;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_class);

        new AsyncTask<Void, Void, Void>()
        {
            @Override
            protected void onPreExecute() {
                pd = ProgressDialog.show(this, "Loading..", "Please Wait", true,false);
            }

            @Override
            protected Void doInBackground(Void... params) {
                Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), null, null, null,null);

                try 
                {
                    while (c.moveToNext()) 
                    {
                        int id = c.getInt(0);
                        getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
                    }
                    //Toast.makeText(SecondClass.this, "Messages Deleted", Toast.LENGTH_SHORT).show();
                }
                catch(Exception e)
                {
                    Log.e(this.toString(),"Error deleting sms",e);
                    //Toast.makeText(SecondClass.this, "Error deleting sms", Toast.LENGTH_SHORT).show();
                }
                finally 
                {
                    c.close();
                }
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                pd.dismiss();
            }
        }.execute((Void[])null);
    }
}

请指导我。谢谢。

4

2 回答 2

2

您的问题在于这些代码行:

 Toast.makeText(SecondClass.this, "Messages Deleted", Toast.LENGTH_SHORT).show();
 Toast.makeText(SecondClass.this, "Error deleting sms", Toast.LENGTH_SHORT).show();

您的代码引发此异常:

Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

你不能使用 doInBackground 里面的 UI 线程,你可以使用 publishProgress() 方法来更新 UI 线程。

于 2013-07-03T10:24:10.130 回答
1

尝试更改您的 SecondClass 代码,如下所示。

public class SecondClass extends Activity {
ProgressDialog pd;
ProgressDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second_class);

    // execute AsyncTask from here
    new DemoAsyncTask().execute();

}   

// Create new AsyncTask class
private class DemoAsyncTask extends AsyncTask<Void, Void, Void>
{
    @Override
    protected void onPreExecute() {
             pd = ProgressDialog.show(this, "Loading..", "Please Wait", true,false);
    }

    @Override
    protected Void doInBackground(Void... params) {
        Cursor c = getApplicationContext().getContentResolver().query(Uri.parse("content://sms/"), null, null, null,null);

        try 
        {
              while (c.moveToNext()) 
              {
                 int id = c.getInt(0);
                 getApplicationContext().getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
              }               
        }
        catch(Exception e)
        {
             Log.e(this.toString(),"Error deleting sms",e);              
        }
        finally 
        {
            c.close();
        }
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
            pd.dismiss();
    }
};


}
于 2013-07-03T10:24:55.047 回答