2013-07-03 17 views
0

您好我正在嘗試開發一個可以刪除短信的安卓應用,到目前爲止我能夠正確刪除短信,但是當移動手機有很多短信時,強制關閉提醒盒子來了,我知道我在UI線程中做所有的東西,所以我通過AsyncTask文章,但無法正確理解在哪裏把我的短信刪除代碼請幫助我,在此先感謝。如何在Android中使用AsyncTask刪除SMS

這裏是我的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)); 
      } 
     }); 
    } 
} 

這裏是我的二等代碼

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); 
    } 
} 

請指引我。謝謝。

+0

它看起來合法,請詳細說明,以便我可以幫助,異步任務是否正常工作? –

+0

如果我刪除它的SMS刪除代碼的工作原理,但是當我添加刪除代碼強制關閉出現時,SMS刪除代碼也工作正常,沒有AsyncTask。 –

+0

你的問題是在'doInBackground()'中使用**敬酒**檢查我的答案。 –

回答

2

你的問題是這些代碼:)

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

不能使用內部doInBackground UI線程,你可以使用publishProgress(方法:

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

代碼引發此異常更新UI線程。

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(); 
    } 
}; 


}