Android小部件:添加新项目(“作者”)文本视图
各位,我正在开发一个简单的android小部件(“Quotes”)。该项目运行良好,但我想添加一个新的文本视图,显示特定“思想”的作者。我在widget.xml(布局)中创建了一个新的textview。。。。我是新手 代码如下: JavaAndroid小部件:添加新项目(“作者”)文本视图,android,widget,textview,Android,Widget,Textview,各位,我正在开发一个简单的android小部件(“Quotes”)。该项目运行良好,但我想添加一个新的文本视图,显示特定“思想”的作者。我在widget.xml(布局)中创建了一个新的textview。。。。我是新手 代码如下: Java import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; i
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);
}
}
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;
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入android.app.Service;
导入android.appwidget.AppWidgetManager;
导入android.content.Intent;
导入android.content.res.AssetManager;
导入android.os.IBinder;
导入android.util.Log;
导入android.widget.remoteview;
公共类UpdateWidgetService扩展服务{
private static final String TAG=UpdateWidgetService.class.getSimpleName();
@凌驾
公共IBinder onBind(意图arg0){
返回null;
}
@凌驾
公共无效启动(Intent Intent,int startId){
super.onStart(intent,startId);
Log.d(标记“onStart已启动”);
//创建一些随机数据
随机=新随机();
AppWidgetManager AppWidgetManager=AppWidgetManager.getInstance(this.getApplicationContext());
int[]appWidgetIds=intent.getIntArrayExtra(AppWidgetManager.EXTRA\u APPWIDGET\u id);
如果(appWidgetIds.length>0){
for(int-widgetId:appWidgetIds){
List qList=getListFromTxtFile(“quotes.txt”);
int-nextInt=random.nextInt(qList.size());
RemoteViews RemoteView=新的RemoteView(getPackageName(),R.layout.widget);
setextviewtext(R.id.widget_textview,qList.get(nextInt));
updateAppWidgetManager.UpdateAppWidgetId(widgetId,RemoteView);
}
stopSelf();
}
super.onStart(intent,startId);
}
公共列表getListFromTxtFile(字符串TXTFILNAME){
//文件sdcard=Environment.getExternalStorageDirectory();
//获取文本文件
//文件文件=新文件(SD卡,TXT文件名);
AssetManager am=this.getAssets();
List qList=new ArrayList();
//从文件中读取文本
试一试{
InputStream=am.open(“quotes.txt”);
//BufferedReader br=新的BufferedReader(新文件读取器(文件));
BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
弦线;
//逐行获取文本文件中的数据
而((line=br.readLine())!=null){
Log.d(“行”,行);
qList.add(行);
}
}
捕获(IOE异常){
//您需要在此处添加正确的错误处理
{Log.d(“错误”,“有错误”);}
}
返回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>