如何在android中使用AsyncTask删除短信

时间:2013-07-03 10:14:37

标签: android android-asynctask sms

你好我正在尝试开发一个可以删除短信的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);
    }
}

请指导我。感谢。

2 个答案:

答案 0 :(得分: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 Thread,你可以使用publishProgress()方法来更新UI线程。

答案 1 :(得分: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();
    }
};


}