Issue
I'm logging to bank account and getting account balance. I calling this function from onUpdate in widget and running in 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. 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? Can I call widget_update (onUpdate) with params to recognize that is from my AsyncTask? Please give me little sample code (what to add to manifest if it must be broadcastreceiver and so on).
I have updated widget in onPostExecute but I think better method is to do that in ExampleAppWidgetProvider class?
Solution
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
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
}
}
}
Answered By - Sunny Kumar Aditya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.