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