프로그래밍/안드로이드

안드로이드 - 새로운 다른 앱이 설치될때 app widget 멈출 경우 해결법

가카리 2014. 1. 11. 12:24
반응형

출처 : http://stackoverflow.com/questions/10972677/toggle-widget-stops-working-after-installing-any-new-app

 

의 번역본입니다.

 

저는 toggle 위젯을 디자인하고 있는데요 미디어 볼륨을 음소거시키고 안시키고 하는 토글 스위치에요. 어떠너 새로운 앱이 설치되기 전까

 

지는 잘 동작하는데. 그런데 새로운 앱이 설치 될때마다 동작을 멈춰요.  어떤 강제적으로 닫고 그런건 없고 버튼 누르면 동작이 안되요.

 

(만약 위젯을 지우고 다시 만들면 다시 동작 잘하네요 ㅡㅡ;)

 

log cat을 보았지만 이에 관련된 메시지를 본게 없네요.

 

코드는 아래와 같구요 누가 개념적으로 해결책좀 설치해주실분 있나요? 그리고 안드로이드 4.04 갤럭시 넥서스를 개발폰으로 쓰고있습니다.

 

public class JCrashWidget extends AppWidgetProvider {

public static String ACTION_WIDGET_RECEIVER = "com.JCrash.widget.ACTION_WIDGET_RECEIVER";

public static String ACTION_WIDGET_CONFIGURE = "com.JCrash.widget.ACTION_WIDGET_CONFIGURE";

private RemoteViews remoteViews = new RemoteViews("com.JCrash", R.layout.widgetstyle1 );

 

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

    super.onUpdate(context, appWidgetManager, appWidgetIds);   

 

    Intent mediaClick = new Intent(context, JCrashWidget.class);

    mediaClick.setAction(ACTION_WIDGET_RECEIVER);

 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  0, mediaClick, 0);      

    remoteViews.setOnClickPendingIntent(R.id.headphonesid, pendingIntent);

 

    appWidgetManager.updateAppWidget(appWidgetIds,remoteViews );

}

 

public void onReceive(Context context, Intent intent)

{

    super.onReceive(context, intent);

 

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    ComponentName cn = new ComponentName(context, JCrashWidget.class);

 

    int cVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

    SharedPreferences myPrefs = context.getSharedPreferences("myPrefs", Context.MODE_WORLD_WRITEABLE);

    SharedPreferences.Editor prefsEditor = myPrefs.edit();

 

        if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {

            if (cVol == 0)

            {

                cVol = myPrefs.getInt("PREVIOUS_VOLUME", 0);

                    remoteViews.setImageViewResource(R.id.headphonesid,R.drawable.headphones);

                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, cVol,

                    AudioManager.FLAG_SHOW_UI);

                    cVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

            }

 

            else

            {

                prefsEditor.putInt("PREVIOUS_VOLUME", cVol);

                prefsEditor.commit();

                cVol = myPrefs.getInt("PREVIOUS_VOLUME", 0);

 

                remoteViews.setImageViewResource(R.id.headphonesid,R.drawable.headphones_off);

                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);

 

                cVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

            }

            AppWidgetManager.getInstance(context).updateAppWidget(cn, remoteViews);            

        }

 

}

}

 

해결책

 

알아냈어요!! OnClickPendingIntent를 OnReceive와 OnUpdate 메소드 모두에 해줘야됬어요. 그냥 이런식으로

 

pending intent를 만들어주니까 잘되네요.

 

 

 

 

반응형