프로그래밍/안드로이드

안드로이드 - appwidget 이 랜덤하게 멈춰요 해결책

가카리 2014. 1. 11. 13:10
반응형

출처 : http://stackoverflow.com/questions/13052138/android-widget-stops-working-randomly

번역본입니다.

 

I have been working with this problem for three days now and I've looked at every single question on here for an answer. I have a widget with a button on it and all I would like it to do is start a service everytime it is clicked. The problem is that the button stops working randomly. I can't recreate it at all and I have no idea what causes it. My service calls stopSelf(); but I have had this problem with a broadcast receiver also so I believe the problem to be in the widget and not in the service. Here is the code

 

저는 이문제를 3일동안 못해결 하고 있어요. 여기서 답변을 매일매일 찾고있어요.

 

저는 위젯을 만들고있는데 버튼이 하나 있어요 그리고 제가 클릭하면 서비스를 시작하고 싶어요.

 

 문제는 버튼이 랜덤하게 동작을 멈춘다는거에요. 저는 이것에 대한 원인을 잘 모르겠어요

 

저의 서비스 콜은 stopSelf(); 에서 부르는데요 그러나 저는 이 문제는 브로드캐스트 리시버에서 난다고 생각해요. 그래서 저는 이 문제가

 

위젯안에 있고 서비스가 아니란 소리죠.

 

여기 제 코드입니다.

 

 

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[]     appWidgetIds)

{

 

Log.i("Widget", "onUpdate");

 

final int N = appWidgetIds.length;

 

    for (int i=0; i<N; i++)

    {

 

        int appWidgetId = appWidgetIds[i];

 

        Log.i("Widget", "onUpdateLoop");

 

 

        Intent intent = new Intent(context, ServiceWidgetAction.class);

        intent.setAction(Long.toString(System.currentTimeMillis()));

        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

 

        PendingIntent pendingIntent = PendingIntent

                .getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 

        RemoteViews views = new RemoteViews(context.getPackageName(),

               R.layout.widget);

 

        views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);

 

        views.setTextViewText(

                R.id.widgetTextView,

                "Some text");

 

        appWidgetManager.updateAppWidget(appWidgetId, views);

 

    }

 

}

 

 

I've checked this code over and over and I have no idea why it is doing this. It does this on 3 separate phones and I can't recreate it. I've tried force stopping the app, clearing it from a task manager adding new widgets etc. to get the widget to stop working and it won't. However when I woke up this morning the widget no longer works. Any help would be GREATLY appreciated

 

제가 코드를 체크하고 체크해봤지만 모르겠어요.ㅠㅠ 이것을 3개의 스마트폰에 돌려봤는데 똑같아요.

 

이것을 태스크매니저로 새로운 위젯이 더해지는것도 클리어해봤지만 역시 안되네요.

 

그러나 제가 아침에 깨어났을때도 위젯이 멈추는 현상을 봤네요 ㅠㅠ 누가 좀 도와주세요.

 

해결책

 

I ended up figuring out the problem. I was updating the widget from other places in the app and I did not have the complete code for each one. In other words my app updated the textviews in the widget like this

 

이 문제의 해결법은 앱의 다른곳에서 위젯을 업데이팅할때 저는 코드를 완벽하게 끝내지 못했다는 의미입니다.

 

즉 다른말로 저의 앱은 텍스트뷰가 이런식으로 업데이트됩니다.

 

RemoteViews views = new RemoteViews(context.getPackageName(),

           R.layout.widget);

 

    views.setTextViewText(

            R.id.widgetTextView,

            "Some text");

 

    appWidgetManager.updateAppWidget(appWidgetId, views);

 

 

 

but it needed the entire code to keep the button registered like this

 

 

근데 이것은 전체의 코드가 필요해요. 즉 버튼을 등록하는 다음과 같은 코드요.

 

 

Intent intent = new Intent(context, ServiceWidgetAction.class);

    intent.setAction(Long.toString(System.currentTimeMillis()));

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

 

    PendingIntent pendingIntent = PendingIntent

            .getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

 

    RemoteViews views = new RemoteViews(context.getPackageName(),

           R.layout.widget);

 

    views.setOnClickPendingIntent(R.id.widgetButton, pendingIntent);

 

    views.setTextViewText(

            R.id.widgetTextView,

            "Some text");

 

    appWidgetManager.updateAppWidget(appWidgetId, views);

 

 

So bottom line is whenever you update a widget from within your app, make sure it looks exactly like the code in the widget itself. Don't leave anything out.

 

그래서 위와같은 코드를 위젯을 업데이트 할때 무조건 불러야되요. 이것은 확실히 위젯을 바꾸겠다는 요청을 하게되는거지요.

 

이것을 따로두지마시고 계속 불러주세요

반응형