2012-12-11 4 views
0

주 활동 (OceanintelligenceActivity)이 있습니다. 이 활동에서 푸시 알림을위한 장치를 등록하고 또한 대화 상자를 표시하고 내 서버에서 보낸 정보에 따라 적절한 활동을 시작하는 수신기를 등록했습니다.BroadcastReceiver에서 현재 보이는 활동에 대화 상자를 표시하려면 어떻게해야합니까?

private final BroadcastReceiver mHandleMessageReceiver = 
     new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String newMessage = intent.getExtras().getString(EXTRA_MESSAGE); 
     Log.d("","BroadcastReceiver onReceive"); 
     notificationIntent = GCMIntentService.getNotificationIntent(context); 

     new AlertDialog.Builder(context) 
     .setMessage(newMessage+". Would you like to see it right now?") 
     .setCancelable(false) 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) {              
       // Show update                
       startActivity(notificationIntent);                
      } 
     }) 
     .setNegativeButton("No", null).show();          
    } 
}; 

GCMIntentService.getNotificationIntent(context) :

protected void gcmRegistration(){ 

    PMApplication thisApp = PMApplication.getInstance(); 
    AppDelegate delegate = thisApp.getAppDelegate(); 
    final Context context = this; 
    // Make sure the device has the proper dependencies. 
    GCMRegistrar.checkDevice(this);  
    // Make sure the manifest was properly set - comment out this line 
    // while developing the app, then uncomment it when it's ready.  
    GCMRegistrar.checkManifest(this); 

    // Let's declare our receiver 
    registerReceiver(mHandleMessageReceiver,new IntentFilter(DISPLAY_MESSAGE_ACTION)); 

    final String regId = GCMRegistrar.getRegistrationId(this); 
    if (regId.equals("")) {  
     Log.d("", "Lets register for Push"); 
     GCMRegistrar.register(this, SENDER_ID);  
    }else { 

     if(GCMRegistrar.isRegisteredOnServer(this)) { 
      // Skips registration.       
      String apnsToken = delegate.sso.getAPNSToken();   
      if(!apnsToken.equals(regId)){ 

      Log.d("", "The Device RegId has changed on GCM Servers"); 
      // We should let our servers know about this 
      ServerUtilities.update(regId, context); 
      } 


     } else {   

      Log.d("","Is not register on PM Server");    
      // Try to register again, but not in the UI thread. 
      // It's also necessary to cancel the thread onDestroy(), 
      // hence the use of AsyncTask instead of a raw thread.    
      mRegisterTask = new AsyncTask<Void, Void, Void>() { 

       @Override 
       protected Void doInBackground(Void... params) { 

        boolean registered = ServerUtilities.register(context, regId); 
        // At this point all attempts to register with the app 
        // server failed, so we need to unregister the device 
        // from GCM - the app will try to register again when 
        // it is restarted. Note that GCM will send an 
        // unregistered callback upon completion, but 
        // GCMIntentService.onUnregistered() will ignore it. 
        if (!registered) { 
         GCMRegistrar.unregister(context); 
        } 
        return null; 
       } 

       @Override 
       protected void onPostExecute(Void result) { 
        mRegisterTask = null; 
       } 

      }; 
      mRegisterTask.execute(null, null, null); 

     } 
    }       
} 

이 내가 수신기를 설정하는 방법입니다 : 이것은 내가 장치와 수신기를 등록하는 데 사용하고 코드입니다. 이 행은 시작하려는 활동이있는 의도를 리턴합니다.

알림이있을 때마다 onReceive가 호출되지만 주 활동 중이면 대화 상자 만 표시됩니다. 따라서 앱이 다른 활동에있는 경우 onReceive이 여전히 호출되지만 대화 상자가 표시되지 않으므로 적절한 작업을 시작할 수 없습니다.

BroadcastReceiver에서 현재 보이는 활동에 대한 대화 상자를 표시하려면 어떻게해야합니까?

답변

0

Google에서 검색하고 검색하여 해결 방법을 찾았습니다. 그것은 최고의 것이 아니지만 작동합니다. Android에서 현재 컨텍스트를 쉽게 얻을 수있는 방법이 없다고 생각합니다. 그래서 이것은 현재 액티비티에 관계없이 Dialog를 보여 주려고했던 것입니다. 저는 싱글 톤 클래스 (AppDelegate)에서 Context 타입의 public static 속성을 가지고 있으며, 각 액티비티에서 onResume 메서드를 재정의하고 컨텍스트를 이 액티비티는 AppDelegate.CURRENT_CONTEXT = this와 같습니다. 그런 다음 내 대화 상자에서 AlertDialog.Builder (AppDelegate.CURRENT_CONTEXT) .....

관련 문제