2

내 Android 시간 위젯이 자체적으로 업데이트되지 않는 이유는 무엇입니까? 홈 화면에 대해 & 시간 위젯을 만들었지 만 문제는 화면 방향이 가로 방향에서 세로 방향으로 변경된 경우에만 내 시간 위젯이 업데이트된다는 것입니다. 실시간 모드에서 자동 업데이트되어야합니다. 여기에 내 코드가 있습니다 :Android 시간 위젯이 업데이트되지 않는 이유가 무엇인가요?

public class PointlessWidget extends AppWidgetProvider { 
    Time mCalendar; 
    float mMinutes; 
    float mHour; 

    @Override 
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
      int[] appWidgetIds) { 
     // TODO Auto-generated method stub 
     super.onUpdate(context, appWidgetManager, appWidgetIds); 

     mCalendar = new Time(); 

     // Line below sets time that will be displayed - modify if needed. 
     mCalendar.setToNow(); 


     int hour = mCalendar.hour; 
     int minute = mCalendar.minute; 
     int second = mCalendar.second; 

     mMinutes = minute + second/60.0f; 
     mHour = hour + minute/60.0f; 

     final int N = appWidgetIds.length; 

     for (int i = 0; i < N; i++) { 
      int awID = appWidgetIds[i]; 
      RemoteViews v = new RemoteViews(context.getPackageName(), 
        R.layout.widget); 

      v.setTextViewText(R.id.textHour, " " + hour); 
      v.setTextViewText(R.id.textMinute, " " + minute); 

      appWidgetManager.updateAppWidget(awID, v); 
     } 
    } 

    @Override 
    public void onDeleted(Context context, int[] appWidgetIds) { 
    // TODO Auto-generated method stub 
    super.onDeleted(context, appWidgetIds); 

    Toast.makeText(context, "Widget Deleted!", Toast.LENGTH_SHORT).show(); 
    } 

} 

그리고 여기 내 매니페스트입니다 :

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

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="15" /> 

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

     <receiver android:name=".PointlessWidget" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.appwidget.provider" 
       android:resource="@xml/widget_stuff" /> 
     </receiver> 
     <service android:name="TimeSettableAnalogClockService"/> 

     <activity android:name=".WidgetsConfig" > 
      <intent-filter> 
       <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

그리고 이것은 내 widget_stuff XML이다. updatePeriodMillis = "1000"또는 이와 동등한 변환을 이미 1 초로 설정했습니다.

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:initialLayout="@layout/widget" 
    android:minHeight="30dp" 
    android:minWidth="274dp" 
    android:updatePeriodMillis="1000" > 

</appwidget-provider> 
+0

확인이 솔루션을 http://stackoverflow.com/questions/2078122/android-widget-not-updating 30 분을 기다려야한다고 생각합니다. –

답변

1

은 이미 .. 내 문제에 해결책을 알아 냈 그리고 광산과 같은 문제를 가진 다른 사람의 이익을 위해 내 코드를 공유합니다 :

import java.text.SimpleDateFormat; 
import java.util.Calendar; 

import android.os.Bundle; 
import android.os.Handler; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.Activity; 

public class DigitalClock extends Activity { 
    private Runnable updateTimeTask; 
    private Handler handler; 

    public static final int TIME_UPDATE_INTERVAL_MSEC = 1000; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     if (updateTimeTask == null) { 
      updateTimeTask = new Runnable() { 
       @Override 
       public void run() { 
        // if (!DigitalClock.this.isActive()) return; 

        timeAndDate(); 
        handler.postDelayed(this, TIME_UPDATE_INTERVAL_MSEC); 
       } 
      }; 
     } 
     if (handler == null) { 
      handler = new Handler(); 
     } 
     handler.removeCallbacks(updateTimeTask); 
     handler.postDelayed(updateTimeTask, TIME_UPDATE_INTERVAL_MSEC); 
    } 

    public void timeAndDate() { 
     Calendar c = Calendar.getInstance(); 

     SimpleDateFormat time = new SimpleDateFormat("hh:mm:ss"); 
     SimpleDateFormat date = new SimpleDateFormat("MMMM dd yyyy"); 
     SimpleDateFormat day = new SimpleDateFormat("EEEE"); 
     SimpleDateFormat am_pm = new SimpleDateFormat("aa"); 
     String sTime = time.format(c.getTime()); 
     String sDate = date.format(c.getTime()); 
     String sDay = day.format(c.getTime()); 
     String sAm_Pm = am_pm.format(c.getTime()); 

     // formattedDate have current date/time 
     Toast.makeText(this, sDate + " | " + sTime, Toast.LENGTH_SHORT).show(); 

     // Now we display value in TextView 
     TextView txtTime = (TextView) findViewById(R.id.txtHour); 
     TextView txtDay = (TextView) findViewById(R.id.txtDay); 
     TextView txtDate = (TextView) findViewById(R.id.txtDate); 
     TextView txtAm_Pm = (TextView) findViewById(R.id.txtAm_Pm); 

     txtTime.setText(sTime); 
     txtDate.setText(sDate); 
     txtDay.setText(sDay); 
     txtAm_Pm.setText(sAm_Pm); 
    } 
} 
1

업데이트하기 위해 시간 위젯은 AlarmManager 클래스를 사용합니다.

또는

시간 서비스를 업데이트하기 위해 백그라운드 서비스를 사용할 수 있습니다.

+0

아이디어를 주셔서 감사합니다. –

관련 문제