2014-09-02 1 views
0

얘들 아, 나는 imageview와 하나의 textview로 안드로이드 위젯을 개발했다. 그것은 잘 작동하지만 일부 업데이트 후 문자열 (따옴표) 내 외부 파일에서 메시지를 업데이트하지 않습니다. 따옴표 (메시지)가없는 이미지 만 보여줍니다. 왜 이런 일이 일어나는 걸까요? 그는 내 전체 코드입니다. 나는 초보자입니다.Android Widget : 일정 기간이 지난 후에 업데이트되지 않습니다.

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

어쩌면 당신은 그것을 사용 후의 BufferedReader를 닫아야합니다.

while ((line = br.readLine()) != null) { 

    Log.d("line",line); 

    qList.add(line); 
} 

br.close(); // this is what you need to add 
+0

어떻게하면됩니까? 저는 초보자입니다 ... 어떻게 BufferedReader를 닫을 수 있습니까? 미리 감사드립니다. –

+0

내 업데이트 된 답변 확인 – Iriminage

+0

생각대로 ... 나는 이것을했지만 여전히 작동하지 않습니다./ –

관련 문제