2013-03-27 2 views
0

텍스트 [] 내의 다른 메시지에 자체 업데이트되는 Android 위젯을 쓰려고합니다. 첫 번째 메시지 "Test1"을 출력하지만 계속 진행하지는 않습니다. 홈 화면에 배치 할 새 위젯을 선택하면 업데이트됩니다. 그것은 logcat에 아무것도 밖으로 침을하지 않습니다. 5 초마다 위젯을 업데이트해야합니다. 테스트 목적으로 5 초를 선택했습니다.Android 위젯이 업데이트되지 않음

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.messageswidget" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 

    <receiver android:name="main.MessagesWidget" 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/widgetinfo" /> 
    </receiver> 

</application> 

MessagesWidget.java

package main; 


import android.appwidget.AppWidgetManager; 
import android.appwidget.AppWidgetProvider; 
import android.content.ComponentName; 
import android.content.Context; 
import android.util.Log; 
import android.widget.RemoteViews; 

import com.example.messageswidget.R; 

public class MessagesWidget extends AppWidgetProvider { 
    private static int n = 0; 
    String[] text = {"Test1", "More2", "Good3"}; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
     int[] appWidgetIds) { 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 
     Log.d("test", "good"); 
     // TODO Auto-generated method stub 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout); 
     views.setTextViewText(R.id.msg, text[n]); 
     n = (n + 1) % text.length; 

     ComponentName cname = new ComponentName(context, MessagesWidget.class); 
     appWidgetManager.updateAppWidget(cname, views); 
    } 
} 

widgetlayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<TextView android:id="@+id/msg" 
    android:textIsSelectable="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

</LinearLayout> 

widgetinfo.xml

,745,
<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="72dip" 
    android:minHeight="72dip" 
    android:updatePeriodMillis="5000" > 

</appwidget-provider> 
+0

언제 TextView 텍스트를 업데이트 하시겠습니까? –

+0

나는 학습 결과를보기 위해 매 5 초마다 업데이트하고 싶었습니다. – awonderer

답변

0

당신은 위젯을 업데이트 할 수 없습니다 매 5 초마다 android:updatePeriodMillis 30 분으로 제한됩니다 (당신은 적어도 그것을 일을하지 않는 방법).

+0

대략 말하면, 서비스가 필요합니까? 'updatePeriodMillis' 만 내재적이며 onUpdate 메소드를 자동으로 실행하지 않아야합니까? – awonderer

+0

updatePeriodMillis는 30000 ms (30 분)로 제한됩니다. AlarmManager를 사용하여 더 자주 업데이트를 예약 할 수 있습니다. – Ahmad

+0

그건 분명해. 고맙습니다. – awonderer

1

오늘도 비슷한 문제가있었습니다. 그것은 일종의 black magic ritual을 거쳤습니다.

다음 번에 문제가 발생했을 때 그 내용과 수행 방법을 모릅니다.

관련 문제