[英]How to get foreground running app in Android by BroadcastReceiver
[英]How to check app is running in background of foreground in BroadcastReceiver
我想檢查我的應用程序是在后台還是在Foreground中,我還想要打開或不使用BroadcastReceiver檢查應用程序
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;
}
}
對此有什么解決方案,幫助我,謝謝。
即使應用程序處於后台,BroadcastReceiver也能正常工作,因為接收方接收的事件是全局發送的,並且每個應用程序都已注冊以監聽這些應用程序,無論應用程序是否正在運行。
要處理此問題,請在BroadcastReceiver的onReceive代碼中檢查您的應用是否位於前台。
我知道有一個 - 也是唯一一個 - 持續有效的方法來做到這一點。 您需要跟蹤應用程序的暫停/恢復操作。 確保在每項活動中檢查此項。
這個答案中有一些示例代碼。 在您的情況下,您需要在從BroadcastReceiver執行任何操作之前檢查MyApplication.isActivityVisible()== true作為驗證。
在您的活動注冊廣播接收器中檢查活動是在前台還是后台。
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.