【问题标题】:Delete Android's Built-In browser's History using AsyncTask使用 AsyncTask 删除 Android 内置浏览器的历史记录
【发布时间】:2013-07-03 11:26:05
【问题描述】:

您好我正在尝试开发一个可以删除浏览器历史记录(内置或默认)的 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) {
            Browser.clearHistory(getContentResolver());
            return null;
        }

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

请帮助我,在此先感谢

这是我的 LogCat

W/ActivityManager(   61):   Force finishing activity com.example.handlerlearn/.SecondClass

W/InputManagerService(   61): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40720228

E/WindowManager( 2089): Activity com.example.handlerlearn.SecondClass has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40525230 that was originally added here

E/WindowManager( 2089): android.view.WindowLeaked: Activity com.example.handlerlearn.SecondClass has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40525230 that was originally added here

E/WindowManager( 2089):     at android.view.ViewRoot.<init>(ViewRoot.java:258)

E/WindowManager( 2089):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)

E/WindowManager( 2089):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)

E/WindowManager( 2089):     at android.view.Window$LocalWindowManager.addView(Window.java:424)

【问题讨论】:

  • AsyncTask 代码有什么问题?
  • 在介绍AsyncTask之前,您是如何清理历史记录的?
  • 小小帮助:您可以不带参数调用 execute(),请封装您的变量(私有)。
  • @g00dy 我只是在btn.setOnClick 中调用Browser.clearHistory(getContentResolver());
  • 那么在AsyncTask 中使用它有什么问题。如果它给您带来一些问题,您可以像这样使用它:Browser.clearHistory(getApplicationContext().getContentResolver()); 当某些事件发生时或仅当 AsyncTask 启动时。请尝试并返回结果。

标签: android browser android-asynctask


【解决方案1】:

还有一件事发生了变化:

pd = ProgressDialog.show(this, "Loading..", "Please Wait", true,false);

pd = ProgressDialog.create().show(this, "Loading..", "Please Wait", true,false);

这可能是缺少的对话屏幕,尚未创建。

【讨论】:

  • 您删除了doInBackground() 中的返回并同时放入了.create(),它仍然在LogCat 中显示相同的错误,即使在Project->Clean 之后也是如此?
猜你喜欢
  • 2023-03-13
  • 2010-10-20
  • 2011-11-14
  • 1970-01-01
  • 2015-07-29
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
相关资源
最近更新 更多