【问题标题】:Android Widget: doesn't update after certain period of timeAndroid Widget:一段时间后不更新
【发布时间】:2014-09-02 12:38:11
【问题描述】:

伙计们,我开发了一个带有 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" />

【问题讨论】:

    标签: android widget android-widget


    【解决方案1】:

    也许你应该在使用后关闭 BufferedReader。

    while ((line = br.readLine()) != null) {
    
        Log.d("line",line);
    
        qList.add(line);
    }
    
    br.close();  // this is what you need to add
    

    【讨论】:

    • 我该怎么做?我是新手...如何关闭 BufferedReader?提前致谢。
    • 正如我所想...我这样做了,但它仍然不起作用:/
    • hm.. idk 如果它是 importnat 但你有 super.onstart() 方法调用了 2 次。
    • 好的...我也这样做了...我删除了 super.onstart() 方法之一,但问题仍然存在。 ://
    猜你喜欢
    • 1970-01-01
    • 2020-06-06
    • 2021-02-07
    • 2015-02-17
    • 1970-01-01
    • 2021-04-09
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多