2016-04-28 47 views
3

我想檢查我的應用程序是否在背景或前景,我也希望您在打開的應用程序或不使用廣播接收器如何檢查應用程序在前臺的後臺運行在廣播接收器

public class CheckRunningApplicationReceiver extends BroadcastReceiver { 
Context mContext; 

public int mId = 1000; 
NotificationManager mNotificationManager; 

@Override 
public void onReceive(Context aContext, Intent anIntent) { 
    mContext = aContext; 
    Boolean isAppOpen = isApplicationSentToBackground(aContext); 
    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
    if (isAppOpen) { 
     //openNotification(); 
    } else { 
     //mNotificationManager.cancel(mId); 
    } 

} 

private void openNotification() {// Instantiate notification with icon and 
            // ticker message 
    Notification notification = new Notification(R.drawable.ic_launcher,"Notification message!", System.currentTimeMillis()); 
    PendingIntent i = PendingIntent.getActivity(mContext, 0, new Intent(mContext,MainActivity.class), 0); 
    notification.setLatestEventInfo(mContext, "Notification Created","Click here to see the message", i); 
    notification.flags = Notification.FLAG_ONGOING_EVENT; 
    mNotificationManager.notify(mId, notification); 
} 

public static boolean isApplicationSentToBackground(final Context context) { 
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); 
    List<RunningTaskInfo> tasks = am.getRunningTasks(1); 
    if (!tasks.isEmpty()) { 
     ComponentName topActivity = tasks.get(0).topActivity; 
     if (!topActivity.getPackageName().equals(context.getPackageName())) { 
      return true; 
     } 
    } 

    return false; 
} 

}

是否有任何解決方案,幫助我, 謝謝。

+0

我不知道,如果這個工程,但你可以試試這個http://stackoverflow.com/questions/3667022/檢查,如果一個Android應用程序正在運行在背景中/ 5862048#5862048 –

+0

我已經取得了巨大的成功與來自史蒂夫Liles在這篇博文中的代碼:[我的Android應用程序當前前景或背景?](http://steveliles.github.io/is_my_android_app_currently_foreground_or_background.html) –

回答

0

即使應用程序處於後臺,由於接收器拾取的事件全局發送,並且每個應用程序都已註冊以偵聽這些應用程序,無論應用程序是否在運行,BroadcastReceiver都能正常工作。

爲了解決這個問題,在您的BroadcastReceiver的onReceive代碼中,檢查您的應用程序是否在前臺。

我知道有一個 - 也是唯一一個 - 始終如一的有效方法來做到這一點。您需要跟蹤您的應用程序的暫停/恢復操作。確保您在每項活動中都檢查了這一點。

這個answer中有一些示例代碼。在你的情況下,你需要先檢查MyApplication.isActivityVisible()== true作爲驗證,然後再從你的BroadcastReceiver中做任何事情。

+0

我已經在Activity中做了一些新的更改,您在應用程序中完成了感謝,現在它將開始鰭e, –

-1

在您的活動註冊廣播接收器檢查活動在前景或背景。

StackAnswer.java 
public class StackAnswer extends Activity { 
    public static final int IS_ALIVE = Activity.RESULT_FIRST_USER; 
    public static final String CHECK_ALIVE_ACTION = "CHECK_ALIVE_ACTION"; 
    private BroadcastReceiver mRefreshReceiver; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mRefreshReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 

       Log.i("onReceive","BroadcastIntent received in MainActivity"); 

       // TODO:     
       // Check to make sure this is an ordered broadcast 
       // Let sender know that the Intent was received 
       // by setting result code to MainActivity.IS_ALIVE 
       setResultCode(MainActivity.IS_ALIVE); 
      } 
     }; 
    } 



    // Register the BroadcastReceiver 
    @Override 
    protected void onResume() { 
     super.onResume(); 

     // TODO: 
     // Register the BroadcastReceiver to receive a 
     // DATA_REFRESHED_ACTION broadcast 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(CHECK_ALIVE_ACTION);   
     registerReceiver(mRefreshReceiver, filter);  

    } 

    @Override 
    protected void onPause() { 

     // TODO: 
     // Unregister the BroadcastReceiver if it has been registered 
     // Note: To work around a Robotium issue - check that the BroadcastReceiver 
     // is not null before you try to unregister it 
     if(mRefreshReceiver!=null){ 
      unregisterReceiver(mRefreshReceiver); 
     } 
     super.onPause(); 
    } 


} 

發送從後臺播放檢查天氣活動是活着還是不

BackgroundTask.java 
    public class BackgroundTask { 

    private void checkIfForeground (Context mApplicationContext){ 
     mApplicationContext.sendOrderedBroadcast(new Intent(
       StackAnswer.CHECK_ALIVE_ACTION), null, 
       new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       // TODO: Check whether the result code is not MainActivity.IS_ALIVE 
       if (getResultCode() != StackAnswer.IS_ALIVE) { 
        //Background 
       }else{ 
        //Foreground 
       } 
      } 
     }, null, 0, null, null); 
    } 
} 
+0

羽絨投票人請給出一個理由。 – H4SN