如果我有一个活动,那么如何在 android 小部件中使用它的一些方法(功能)。我必须重新创建逻辑吗?交互方式有哪些?通过意图还是通过服务?请回复。
更新:我有一个实现 Parcelable 并包含返回列表的方法的 .java 类。我想要我的小部件中的那个列表。
public Stores getStoreListings() {
Log.i("List val","mStoreListings");
return mStoreListings;
}
我可以在我的应用小部件中使用 Intent 来获取此方法或变量吗?这不是活动。。
UPDATE2:使用异步任务,它也不起作用..我哪里出错了?请帮忙..
public class ListViewWidget extends AppWidgetProvider{
@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);
ComponentName thisWidget = new ComponentName(context,ListViewWidget.class);
String url=String.valueOf("https://api.paypal.com/v1/stores?view=local&lat=37.3756096&lng=-121.9239449&radius=50&count=20&start_id=1&country_code=US");
FetchTask fetchTask=new FetchTask();
//fetchTask.appWidgetManager=appWidgetManager;
//fetchTask.onPostExecute(updateViews,context);
fetchTask.execute();
//context.startService(new Intent(context,UpdateService.class));
}
public static class FetchTask extends AsyncTask<URL,Integer,Stores>{
String text=null;
@Override
protected Stores doInBackground(URL... arg0) {
// TODO Auto-generated method stub
HttpClient httpClient=new DefaultHttpClient();
HttpContext localContext=new BasicHttpContext();
HttpGet httpGet=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");
Stores newList=new Stores();
try{
HttpResponse response=httpClient.execute(httpGet,localContext);
HttpEntity entity=response.getEntity();
text=String.valueOf(entity);
//assign the list to the the correct entity type
//newList=..;
Log.i("Inside on update with Http call","Text"+text);
}
catch(Exception e){
e.printStackTrace();
}
return newList;
}
protected void onPostExecute(RemoteViews updateViews,Context context){
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);
// Push update for this widget to the home screen
ComponentName thisWidget = new ComponentName(this, ListViewWidget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(this);
manager.updateAppWidget(thisWidget, updateViews);
}
}
}