안드로이드에서 위젯을 사용하는 mp3 앱을 만들고 있는데요.
앱을 실행하지 않고도 위젯에서 음악이 실행되도록 하고 음악이 실행되면 noti에 알려주도록 만들었습니다.
이건 noti를 주는 코드구요.
notiManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, A_Tab_Hub.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
noti = builder.setContentTitle("MP3 Player")
.setContentText(songTitle)
.setSmallIcon(R.drawable.noti_mp3_w)
.setContentIntent(pIntent)
.build();
noti.flags = Notification.FLAG_NO_CLEAR;
notiManager.notify(1234, noti);
이 코드에 따르면 상태바를 끌어내려 그 noti를 선택했을 때 앱이 실행되도록 pendingIntent를 줬는데 이건 내가 만든 activity가 백그라운드에서 실행되어 있어도 또 새롭게 activity가 실행되어 2개가 실행되더라구요. ㅡㅡ;;
그래서 원래의 앱이 실행중이었으면 (앱을 실행한 후 home 눌러서 백그라운드에서 돌아가도록요) 실행중이었던 앱을 front로 가져오라고 하고요, 만약 앱 실행리스트에 원래의 앱이 없으면 앱을 새로 실행하라고 startActivity를 주고 싶습니다.
그런데 noti를 처음 만들 때 pendingIntent를 주면 상태바에서 noti를 선택했을 그 시점의 상태에 따라 앱을 실행할수가 없고 무조건 startActivity를 하라고 줘야 하잖아요?
이럴경우 상태에 따라 noti를 선택했을 때 행동을 다르게 하려면 어떻게 해야하나요?
참고로 현재 제가 만든 앱의 메인 액티비티가 실행중이었는지 확인하는건 다음과 같이 코딩했어요.
(그냥 앱이 실행중인지 알아보려니 위젯이 실행중이어도 앱이 실행중이라고 나와서 액티비티까지 비교해줬지요)
ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> task = activityManager.getRunningTasks(10);
for(int i=0 ; i < task.size() ; i++){
ComponentName topActivity = task.get(i).topActivity;
String className = topActivity.getClassName();
Log.i(TAG,"topActivity className = " + className);
if(className.equals("패키지명.액티비티명")){
activityManager.moveTaskToFront(task.get(i).id, 0);
}
이 소스가 정상작동하는지는 위젯에서 버튼을 누를 때 테스트해봤더니 제대로 되더라구요.
그런데 문제는 위젯에서 버튼을 누르는게 아니라 상태바에 떠있는 noti를 누를 때 그 시점에 하려니... 이걸 어찌해야할까요?