2013-07-14 2 views
0

브로드 캐스트를 시작하는 앱이 있습니다. 내가 아무리 앱이 실행되지 다음 경우에하는 통지 여기활동 호출 Alert Box from Broadcast Receiver android

를 밀어하면 생각하는 활동에 팝업을 표시하지 싶은 새로운 활동을 만드는 방법에 대한 내 코드

public class AlarmReceiver extends BroadcastReceiver 
{ 
    private static int NOTIFICATION_ID = 1; 
    private NotificationManager mNotificationManager; 

    @Override 
    public void onReceive(final Context ctx, Intent intent) 
    { 
     if (isRunning(ctx)) 
     { 
      Intent myIntent = new Intent (ctx, NotifyTagabilityCounter.class); 
      myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      ctx.startActivity(myIntent); 
     } 
     else 
     { 
      mNotificationManager = (NotificationManager) 
         ctx.getSystemService(Context.NOTIFICATION_SERVICE); 

      Intent i = new Intent(ctx, Game.class); 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 
      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      i.putExtra(Constants.TAG_TIMESTAMP, true); 
      PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, 
        i, PendingIntent.FLAG_CANCEL_CURRENT);  

      NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(ctx) 
        .setSmallIcon(R.drawable.launcher) 

      Notification notification = mBuilder.getNotification(); 
      notification.flags = Notification.FLAG_AUTO_CANCEL; 

      mBuilder.setContentIntent(contentIntent); 
      mNotificationManager.notify(NOTIFICATION_ID, notification); 

      SharedPreferences customSharedPreference = ctx.getSharedPreferences(
        "UserSharedPrefs", Activity.MODE_PRIVATE); 

      SharedPreferences.Editor editor = customSharedPreference.edit(); 
      editor.putBoolean(Constants.TAG_NOTIFICATION, true); 
      editor.commit(); 
     } 
    } 

    public boolean isRunning(Context ctx) 
    { 
      ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE); 
      List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE); 

      for (RunningTaskInfo task : tasks) 
      { 
       if (ctx.getPackageName().equalsIgnoreCase(
             task.baseActivity.getPackageName())) 
        return true;         
      } 

      return false; 
     } 

입니다 NotificyTagabilityCounter

ublic class NotifyTagabilityCounter extends Activity 
{ 
    public static final int NOTIFICATION_ID_RECEIVED = 0x1221; 
    static final String ACTION = "com.trib.broadcast"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     IntentFilter filter = new IntentFilter(ACTION); 
     this.registerReceiver(mReceivedReceiver, filter); 
    } 

private final BroadcastReceiver mReceivedReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

      if (ACTION.equals(action)) 
      { 
       displayAlert(); 
      } 
     } 
    };  

나는 검은 화면을 얻는다. 내 마지막 활동을 표시하고 그 위에 팝업 AlertDisplay가 표시됩니다. 이게 어떻게 .. 할 수 있니?

답변

1

다음을 수행하십시오.
1- 낮은 우선 순위의 매니페스트에서 첫 번째 항목 2 개와 우선 순위가 높은 앱의 각 활동 (런타임)에서 두 번째 항목 1을 정의하십시오.
첫 번째 은 1 번째 BroadcastReceivers이고 두 번째 브로드 캐스트는 브로드 캐스트 abort()이어야하며 대화 상자를 표시해야합니다.

2- 주문 된 방송을 보냅니다.

PS : 당신은 마녀 활동을 확장하는 BaseActivtiy를 작성하고,
을 후하게 (등록 onStart()onStop()에 방송 등록을 취소 foget 해달라고) 거기에 2 차 방송을 정의 할 수 있습니다 모든 Activity`s는 BaseActivtiy

를 확장
+0

동일한 작업을 수행했지만 기본 작업에서 팝업 및 서버 호출을 정의해야합니까? 어떻게하면 제대로 호출 할 수 있습니까? 예를 들어 주시겠습니까? 내 브로드 캐스트 수신기에서 BaseActivty 브로드 캐스트로 브로드 캐스트를 보내고 있지만 토큰 null로 팝업을 만드는 동안 오류가 발생합니다. –