1
    <?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:minHeight="75dp"
    android:minWidth="150dp"
  android:updatePeriodMillis="2000000"
  android:initialLayout="@layout/stackwidget"
  android:autoAdvanceViewId="@id/stackWidgetView"
  android:previewImage="@drawable/stackwidget_preview">
</appwidget-provider



**StackWidgetService.java**

    public class StackWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
    }
}

class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
//    private List<String> mBuzzes = new ArrayList<String>();
    private Context mContext;
    private int mAppWidgetId;
    String timeStamp;
    List<String> mBuzzes = new ArrayList<String>();

    public StackRemoteViewsFactory(Context context, Intent intent) {
        mContext = context;
        mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        InitNewData();
    }

    **//GENERATES NEW RANDOM DATA EVERYTIME THE CONSTRUCTOR IS CALLED**
    private void InitNewData() {
        timeStamp=GetTime();
        int number = (new Random().nextInt(100));
        for(int i=0;i<7;i++){
            mBuzzes.add((++number)+"");
        }
    }

    public String GetTime() {
        Calendar now = Calendar.getInstance();
        String tempDate = (now.get(Calendar.YEAR) + "-"
                + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE)
                + " " + now.get(Calendar.HOUR_OF_DAY) + ":"
                + now.get(Calendar.MINUTE) + ":" + now.get(Calendar.SECOND));
        return tempDate;
    }

    public void onCreate() {
    }

    public void onDestroy() {
        mBuzzes.clear();
    }

    public int getCount() {
        return mBuzzes.size();
    }

    public RemoteViews getViewAt(int position) {
        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.stackwidget_item);

        if (position <= getCount()) {
           String buzz = mBuzzes.get(position);
//           rv.setTextViewText(R.id.stackWidgetItemUsername, buzz);
            rv.setTextViewText(R.id.textView1, Html.fromHtml(timeStamp+"-"+buzz));
            // store the buzz ID in the extras so the main activity can use it
            Bundle extras = new Bundle();
            extras.putString(MainActivity.EXTRA_ID, ""+position);
            Intent fillInIntent = new Intent();
            fillInIntent.putExtras(extras);
            rv.setOnClickFillInIntent(R.id.stackWidgetItem, fillInIntent);
        }

        return rv;
    }

    public RemoteViews getLoadingView() {
        return null;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public long getItemId(int position) {
        return position;
    }

    public boolean hasStableIds() {
        return true;
    }

    public void onDataSetChanged() {

    }
}

**StackWidgetProvider.java**
    public class StackWidgetProvider extends AppWidgetProvider {

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
    }


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

        for (int i = 0; i < appWidgetIds.length; ++i) {
            RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.stackwidget);

            // set intent for widget service that will create the views
            Intent serviceIntent = new Intent(context, StackWidgetService.class);
            serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    appWidgetIds[i]);
            serviceIntent.setData(Uri.parse(serviceIntent
                    .toUri(Intent.URI_INTENT_SCHEME))); // embed extras so they
                                                        // don't get ignored
            remoteViews.setRemoteAdapter(appWidgetIds[i], R.id.stackWidgetView,
                    serviceIntent);
            remoteViews.setEmptyView(R.id.stackWidgetView,
                    R.id.stackWidgetEmptyView);

            // set intent for item click (opens main activity)
            Intent viewIntent = new Intent(context, MainActivity.class);
            viewIntent.setAction(MainActivity.ACTION_VIEW);
            viewIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    appWidgetIds[i]);
            viewIntent.setData(Uri.parse(viewIntent
                    .toUri(Intent.URI_INTENT_SCHEME)));

            PendingIntent viewPendingIntent = PendingIntent.getActivity(
                    context, 0, viewIntent, 0);
            remoteViews.setPendingIntentTemplate(R.id.stackWidgetView,
                    viewPendingIntent);

            // update widget
            appWidgetManager.updateAppWidget(appWidgetIds[i], remoteViews);
        }
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }
}

スタックビューで appwidget を表示するコードは次のとおりです。しかし、約 30 分後に StackView レイアウトに新しい値を表示する必要があります。インターネットで入手できるさまざまなデモを試しましたが、思い通りに動作するものはありませんでした。助けてください!前もって感謝します!

4

0 に答える 0