Android小部件通过服务更新,即执行http请求

Android小部件通过服务更新,即执行http请求,android,android-widget,android-service,Android,Android Widget,Android Service,以下情况下的最佳策略是什么: 我的应用程序将定期向运行PHP脚本的服务器发送和接收http请求。 我尝试了由AlarmManager启动的服务或直接单击其小部件。设备上的直接操作工作正常(如随机数,请参阅) 对于请求,这些应该在异步任务中完成。我试图从异步任务的postexecute部分更新小部件(remoteView),但它不起作用 有人能给我一些提示或者片段吗 先谢谢你 更新: 具有异步任务的窗口小部件提供程序类: public class MyWidgetProvider extends

以下情况下的最佳策略是什么:

我的应用程序将定期向运行PHP脚本的服务器发送和接收http请求。 我尝试了由AlarmManager启动的服务或直接单击其小部件。设备上的直接操作工作正常(如随机数,请参阅) 对于请求,这些应该在异步任务中完成。我试图从异步任务的postexecute部分更新小部件(remoteView),但它不起作用

有人能给我一些提示或者片段吗

先谢谢你

更新:

具有异步任务的窗口小部件提供程序类:

public class MyWidgetProvider extends AppWidgetProvider {

private static final String ACTION_CLICK = "ACTION_CLICK";
long ddd;
String dddd;
RemoteViews remoteViews;
TextView tvUpdate;


  private static final String LOG = "de.vogella.android.widget.example";

  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {

    Log.w(LOG, "onUpdate method called");
    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
        MyWidgetProvider.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    // Build the intent to call the service
    Intent intent = new Intent(context.getApplicationContext(),
        UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

    // Update the widgets via the service
    context.startService(intent);
  }
 }
带有异步任务的服务类对网站进行ping(仅用于测试目的,稍后将在那里执行请求) 目的是在小部件中显示ping:

public class UpdateWidgetService extends Service {
private static final String LOG = "de.vogella.android.widget.example";
long ddd;
String dddd;
RemoteViews remoteViews;
TextView tvUpdate;

@Override
public void onStart(Intent intent, int startId) {
    Log.i(LOG, "Called");
    // create some random data

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
            .getApplicationContext());

    int[] allWidgetIds = intent
            .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

    ComponentName thisWidget = new ComponentName(getApplicationContext(),
            MyWidgetProvider.class);
    int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
    Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
    Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));

    for (int widgetId : allWidgetIds) {
        // create some random data
        int number = (new Random().nextInt(100));

        remoteViews = new RemoteViews(this
                .getApplicationContext().getPackageName(),
                R.layout.widget_layout);
        Log.w("WidgetExample", String.valueOf(number));
        if (dddd!=null)Log.w("dddd -", dddd);
        new LongOperation().execute("");
        if (dddd!=null)Log.w("dddd +", dddd);
        // Set the text
        remoteViews.setTextViewText(R.id.update,
                "Random: " + String.valueOf(number));

        // Register an onClickListener
        Intent clickIntent = new Intent(this.getApplicationContext(),
                MyWidgetProvider.class);

        clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                allWidgetIds);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                getApplicationContext(), 0, clickIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.add(Calendar.SECOND, 5);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                cal.getTimeInMillis(), 5 * 1000, pendingIntent);

    }
    stopSelf();

    super.onStart(intent, startId);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private class LongOperation extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        ddd = MainActivity.isURLReachable();

        Log.e("ddd", ddd + "");
        dddd = ddd + " ms";
        Log.e("dddd", dddd);

        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Log.e("POST dddd", dddd);
        remoteViews.setTextViewText(R.id.update, dddd);
    }

}
问题是无法从执行后异步任务访问小部件


不需要这样做
onPostExecute()
可以对其调用
updateAppWidget()

“它不工作”——请完整准确地解释您的症状。“不工作”不是一个非常有用的描述你的症状。除此之外,如果您不提供“不工作”代码供我们查看,我们将无法帮助您处理“不工作”代码。最后,服务不应该使用
AsyncTask
——也许您的服务应该是
IntentService
。谢谢您是正确的。我提供了一些有关结构的详细信息。谢谢,但是我如何访问小部件ID?请参见编辑?int[]allWidgetIds=intent.getIntArrayExtra(AppWidgetManager.EXTRA\u APPWIDGET\u id)@user1616685:将它们放入
异步任务的数据成员中。这是一个很好的答案,但我将尝试实现intentservice,以便正确完成它。
@Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(UpdateWidgetService.this
                .getApplicationContext());

        int[] allWidgetIds = intent
                .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

        ComponentName thisWidget = new ComponentName(getApplicationContext(),
                MyWidgetProvider.class);
        int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
        Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
        Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));

        for (int widgetId : allWidgetIds) {
            // create some random data
            int number = (new Random().nextInt(100));

            remoteViews = new RemoteViews(UpdateWidgetService.this
                    .getApplicationContext().getPackageName(),
                    R.layout.widget_layout);
            Log.w("WidgetExample", String.valueOf(number));
            if (dddd!=null)Log.w("dddd -", dddd);
            new LongOperation().execute("");
            if (dddd!=null)Log.w("dddd +", dddd);
            // Set the text
            remoteViews.setTextViewText(R.id.update,
                    "Random: " + dddd);

            // Register an onClickListener
            Intent clickIntent = new     Intent(UpdateWidgetService.this.getApplicationContext(),
                        MyWidgetProvider.class);

            clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
                    allWidgetIds);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    getApplicationContext(), 0, clickIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }


    }