我正在创建一个执行一些网络调用的小部件。我正在为此使用异步任务。但是在我的 onUpdate() 中,没有创建 ComponentName 对象,产生以下编译器错误:
1)The constructor ComponentName(ListViewWidget,Class<ListViewWidget>) is undefined.
2)The method getInstance(context) in the type AppWidgetManager is not applicable for the arguments(ListViewWidget)
//ListViewWidget.java
public class ListViewWidget extends AppWidgetProvider{
private static String text=null;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.list_layout);
FetchTask fetchTask=new FetchTask(context);
//compiler error in the following 2 lines
ComponentName thisWidget = new ComponentName(this, ListViewWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
fetchTask.execute();
}
public static class FetchTask extends AsyncTask<URL,Integer,String>{
private Context context;
public FetchTask(Context context){
this.context=context;
}
@Override
protected String doInBackground(URL... arg0) {
HttpHeaders headers = new HttpHeaders();
headers.add(Constants.ACCEPT_HEADER, Constants.JSON_MIME_TYPE );
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add(Constants.AUTHORIZATION_HEADER, "Basic ZDNhYWNmNDUwZGQ2YWE5OTJjZmJhNzcwNjc1NjA3MzM6N2NlYmJhMWJmMTRjYjg1OA==");
DefaultHttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet("https://api.paypal.com/v1/stores?view=local&lat=37.3756096&lng=-121.9239449&radius=50&count=20&start_id=1&country_code=US");
//("http://search.twitter.com/search.json?q=twitter");
BasicResponseHandler handler = new BasicResponseHandler();
String response = null;
Date d=null;
//SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
try{
response = client.execute(get,handler);
Log.i("TagAkshita","Response-"+response);
if(response!=null){
JSONObject object = new JSONObject(response);
JSONArray array = object.getJSONArray("results");
if(array.length()>0){
JSONObject list = array.getJSONObject(0);
text = list.getString("view");
Log.i("Stores:","textval"+text);
}
}
}
catch(Exception e){
e.printStackTrace();
}
return text;
}
protected void onPostExecute(RemoteViews updateViews,Context context) {
// TODO Auto-generated method stub
Intent intent=new Intent("android.appwidget.action.APPWIDGET_UPDATE");
PendingIntent pendingIntent=PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setTextViewText(R.id.text_view, text);
updateViews.setOnClickPendingIntent(R.id.next, pendingIntent);
}
}
}