伙计们,我开发了一个带有imageview和单个textview的android小部件。它工作正常,但在一些更新后,来自我的外部文件的消息与字符串(引号)不会更新它。它只是显示没有任何引用(消息)的图像。为什么会这样? 他是我的全部代码。我是新手。
Main.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
UpdateWidgetService.java
public class UpdateWidgetService extends Service {
private static final String TAG = CopyOfUpdateWidgetService.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;
}
}
WidgetQuotes.Java
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);
// 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);
// Finally update all widgets with the information about the click
// listener
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
// Update the widgets via the service
context.startService(intent);
}
public void onDeleted(Context context, int[] appWidgetIds) {
// Toast.makeText(context, "onDelete", Toast.LENGTH_SHORT).show();
super.onDeleted(context, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.esmeralda"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<supports-screens android:smallScreens="true" android:largeScreens="true" android:normalScreens="true"/>
<application android:icon="@drawable/icone" android:label="@string/app_name">
<receiver android:name="com.esmeralda.WidgetQuotes" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_quotes_info" />
</receiver>
<service android:name="com.esmeralda.UpdateWidgetService"></service>
<activity
android:name="com.esmeralda.Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="com.esmeralda.ACTION_WIDGET_CONFIGURE"/>
</intent-filter>
</activity>
</application>
</manifest>
widget_quotes_info.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dp"
android:minHeight="250dp"
android:updatePeriodMillis="1800020"
android:initialLayout="@layout/widget" />
答案 0 :(得分:0)
也许你应该在使用后关闭BufferedReader。
while ((line = br.readLine()) != null) {
Log.d("line",line);
qList.add(line);
}
br.close(); // this is what you need to add