2014-01-09 2 views
1

나는 응용 프로그램에서 다양한 활동을 열 수있는 간단한 Android 위젯을 만들 예정입니다.버튼이있는 Android 위젯, 업데이트 제거

public class ExampleAppWidgetProvider extends AppWidgetProvider { 

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    final int N = appWidgetIds.length; 

    Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds)); 

    // Perform this loop procedure for each App Widget that belongs to this 
    // provider 
    for (int i = 0; i < N; i++) { 
     int appWidgetId = appWidgetIds[i]; 

     // Create an Intent to launch ExampleActivity 
     Intent intentForHome = new Intent(context, WidgetExampleActivity.class); 
     PendingIntent pendingIntentForHome = PendingIntent.getActivity(context, 0, intentForHome, 0); 
     // Get the layout for the App Widget and attach an on-click listener 
     // to the button 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget1); 
     views.setOnClickPendingIntent(R.id.button, pendingIntentForHome); 

     // Create an Intent to launch Second Activity 
     Intent intentForSecond = new Intent(context, SecondActivity.class); 
     PendingIntent pendingIntentForSecond = PendingIntent.getActivity(context, 0, intentForSecond, 0); 
     // Get the layout for the App Widget and attach an on-click listener 
     // to the button 
     views.setOnClickPendingIntent(R.id.button2, pendingIntentForSecond); 


     // Tell the AppWidgetManager to perform an update on the current app 
     // widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    } 
    } 
} 

이렇게하면 각 버튼을 통해 올바른 활동이 열립니다. 그러나 나는 단지 궁금합니다. updatePeriodMillis을위한 것입니다. 위젯에 업데이트가 필요없고 버튼이 애플리케이션을 열길 원한다. 업데이트 부분을 제거 할 수 있습니까? 내 응용 프로그램이 위젯을 계속 업데이트하는 것을 원하지 않습니다.

<?xml version="1.0" encoding="utf-8"?> 
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" 
    android:minWidth="294dp" 
    android:minHeight="72dp" 
    android:updatePeriodMillis="10000" 
    android:initialLayout="@layout/widget1"> 
</appwidget-provider> 

또한이 위젯에서 버튼 클릭을 수행하는 정확한 방법이다. 내가 자습서에서 다르게 수행되는 것을 볼 수 있습니까? 감사합니다.

+0

도우미 : http://stackoverflow.com/questions/5641134/how-to-disable-widget-updateperiodmillis –

답변

0

updatePeriod은 일정 시간 간격으로 위젯을 새로 고치는 데 사용됩니다. 이 기능이 필요하지 않은 경우 updatePeriodMillis0으로 설정하십시오.

+0

감사합니다. 코드 줄 appWidgetManager.updateAppWidget (appWidgetId, views)도 제거 할 수 있습니까? 위젯에서 버튼 클릭을 수행하는 올바른 방법이기도합니다. 내가 자습서에서 다르게 수행되는 것을 볼 수 있습니까? – user3013243

0

This page은 버튼으로 위젯을 만드는 방법을 설명합니다. 위젯을 새로 고칠 필요가없는 경우 updatePeriodMillis을 0으로 수정하십시오. updatePeriodMillis을 15 분보다 짧게 사용할 수 없으며 새로 고침 빈도에 대한 보장이 없습니다 (다소 지연 될 수 있음). 수정 빈도로 새로 고침하려면 Alarm을 사용하거나 Intent.ACTION_TIME_TICK에 등록하여 직접 관리하십시오.

버튼 이벤트를 처리하는 데는 Android 페이지에서 언급 한 것이므로 올바른 방법입니다.