简体   繁体   English

如何执行长作业,然后从小部件onclick方法更新小部件?

[英]How to execute long job and then update widget from widget onclick method?

I'm logging to bank account and getting account balance. 我正在登录银行帐户并获得帐户余额。 I calling this function from onUpdate in widget and running in AsyncTask 我从小部件中的onUpdate调用此函数并在AsyncTask中运行

package com.example.oobe.widget.widgetexample;
public class ExampleAppWidgetProvider extends AppWidgetProvider 
{
    (...)

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]  appWidgetIds) 
    {
        (...)
        if (notFromAsyncTask)
            new MyAsyncTask().execute(context);
        (...)
    }
    (...)
}

In method onPostExecute I want to call onUpdate widget and putExtra strings. 在方法onPostExecute中,我想调用onUpdate小部件和putExtra字符串。 How can I do this? 我怎样才能做到这一点?

package com.example.oobe.widget.widgetexample;
public class MyAsyncTask extends AsyncTask<Object, Void, BGZ> 
{
    Context context;

    @Override
    protected BGZ doInBackground(Object... params) 
    {
        this.context = (Context)params[0];
        return GetSomething();
    }

    protected void onPostExecute(BGZ page)
    {
        Intent intent = new Intent(context, ExampleAppWidgetProvider.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra("result", result);
        intent.putExtra("webpage", webPage);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    (...)
}

If I doing like above I'm getting error: .. Unable to find explicit activity class.. 如果我像上面那样做,我会收到错误消息:..无法找到明确的活动类。

Can I do this from AsyncTask? 我可以从AsyncTask中执行此操作吗? Can I call widget_update (onUpdate) with params to recognize that is from my AsyncTask? 我可以使用参数调用widget_update(onUpdate)来识别来自AsyncTask的信息吗? Please give me little sample code (what to add to manifest if it must be broadcastreceiver and so on). 请给我一些示例代码(如果必须是broadcastreceiver,要在清单中添加些什么)。

I have updated widget in onPostExecute but I think better method is to do that in ExampleAppWidgetProvider class? 我已经在onPostExecute中更新了小部件,但我认为更好的方法是在ExampleAppWidgetProvider类中做到这一点?

If you are getting this error 如果出现此错误

Unable to find explicit activity class.. 找不到明确的活动类别。

It may be due to this line 可能是因为这条线

Intent intent = new Intent(context, ExampleAppWidgetProvider.class); 

make sure ExampleAppWidgetProvider.class exist and 确保ExampleAppWidgetProvider.class存在,并且

Context context = Activity.this;

or 要么

Intent intent = new Intent(Activity.this, ExampleAppWidgetProvider.class); 

UPDATE 更新

public class ExampleAppWidgetProvider extends AppWidgetProvider 
{

   //your code .......
//asynctask as an inner class
   public class MyAsyncTask extends AsyncTask<Object, Void, BGZ> 
{
  doInBackground(){}
onPreExecute(){}
onPostExecute(){

//save result 
// or call your methods you need to run after your async call
}
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM