Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

Android Widget: adding a new item ("author") textview

Everyone, I am developing a simple android widget ("Quotes"). The project is working fine, but I would like to add a new textview that shows the author of the specific "thought". I created a new textview in widget.xml (layout).... I'm a newbie!!!

Here the code is:

WidgetQuotes.Java

  import android.app.PendingIntent;
  import android.appwidget.AppWidgetManager;
  import android.appwidget.AppWidgetProvider;
  import android.content.Context;
  import android.content.Intent;
  import android.widget.RemoteViews;

   public class WidgetQuotes extends AppWidgetProvider {

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

    // Build the intent to call the service
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

    RemoteViews remoteViews2 = new RemoteViews(context.getPackageName(), R.layout.widget);
    Intent intent2 = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

    // To react to a click we have to use a pending intent as the
    // onClickListener is
    // excecuted by the homescreen application
    PendingIntent pendingIntent = PendingIntent.getService(
            context.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    remoteViews.setOnClickPendingIntent(R.id.widget_textview, pendingIntent);


    PendingIntent pendingIntent2 = PendingIntent.getService(
            context.getApplicationContext(), 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

    remoteViews2.setOnClickPendingIntent(R.id.widget_textview2, pendingIntent2);


    // Finally update all widgets with the information about the click
    // listener
    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

    appWidgetManager.updateAppWidget(appWidgetIds, remoteViews2);

    // Update the widgets via the service
    context.startService(intent);
    context.startService(intent2);
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

}
public void onReceive1(Context context, Intent intent2) {   
    super.onReceive(context, intent2);
}



}

UpdateWidgetService.Java

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;



   import android.app.Service;
   import android.appwidget.AppWidgetManager;
   import android.content.Intent;
   import android.content.res.AssetManager;
   import android.os.IBinder;
   import android.util.Log;
   import android.widget.RemoteViews;

   public class UpdateWidgetService extends Service {
private static final String TAG = UpdateWidgetService.class.getSimpleName();

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

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    Log.d(TAG, "onStart started");

    // Create some random data
    Random random = new Random();

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

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

    if (appWidgetIds.length > 0) {

        for (int widgetId : appWidgetIds) {
            List<String> qList = getListFromTxtFile("quotes.txt");
            int nextInt = random.nextInt(qList.size());

            RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
            remoteViews.setTextViewText(R.id.widget_textview, qList.get(nextInt));
            appWidgetManager.updateAppWidget(widgetId, remoteViews);
        }
        stopSelf();
    }
    super.onStart(intent, startId);
}

public List<String> getListFromTxtFile(String txtFileName){

//  File sdcard = Environment.getExternalStorageDirectory();
// Get the text file
// File file = new File(sdcard,txtFileName);

AssetManager am = this.getAssets();

List<String> qList = new ArrayList<String>();

//Read text from file

try {
    InputStream is = am.open("quotes.txt");
          //BufferedReader br = new BufferedReader(new FileReader(file));
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;

     // get data in text file line by line
    while ((line = br.readLine()) != null) {

        Log.d("line",line);

       qList.add(line);
    }
}
catch (IOException e) {
    //You'll need to add proper error handling here

    {Log.d("Error","There are an error");}
}
return qList;

}

   }

Widget.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:orientation="vertical"
android:background="@drawable/widget_frame3"
android:layout_gravity="center">

<TextView
    android:id="@+id/widget_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|center_horizontal"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp"
    android:padding="10dip"
    android:scrollbars="vertical"
    android:text="@string/widget_text"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/widget_textview2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|center_horizontal"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="20dp"
    android:padding="10dip"
    android:scrollbars="vertical"
    android:text="@string/widget_text2"
    android:textColor="@android:color/white" />

    </LinearLayout>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I Think you can not create a text view in a widget. Only tap and touch is possible in the widget


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...