在AlramManager中如何检查应用程序是打开还是关闭

时间:2017-04-20 12:15:21

标签: android xamarin.android alarmmanager

根据这个链接(Starting AlarmManager in Android (C#)),我可以看到“Time Up ... Now Vibrating !!!”。 当我的应用程序关闭时,我可以看到“Time Up ... Now Vibrating !!!”。到目前为止一切都很好。

我的问题:

1。 Whit AlramManager,如何检查应用是开放还是关闭?

2. 如果该应用已关闭,我该如何打开它?

编辑:

我编辑了我的应用程序,但是当我的应用程序未处于调试模式时 - 已关闭 - 我再次看到“应用程序正在运行”。为什么?我想在关闭时打开我的应用程序。

namespace AlarmManage
{
    [BroadcastReceiver(Enabled = true)]
    public class MyBroadcastReceiver : BroadcastReceiver 
    {
        private static bool activityVisible;

        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Time Up... Now Vibrating !!!",
                    ToastLength.Long).Show();
            Vibrator vibrator = (Vibrator)context
                    .GetSystemService(Context.VibratorService);
            vibrator.Vibrate(2000);
            //Application.Context.StartActivity(new Intent(context, typeof(MainActivity)));

            if (CheckRunningApplicationReceiver.isAppRunning(context, "AlarmManage.AlarmManage"))
            {
                // App is running
                Toast.MakeText(context, "App is running", ToastLength.Long).Show();
            }
            else
            {
                // App is not running
                // intent to lounch ur app. 
                Toast.MakeText(context, "App is not running", ToastLength.Long).Show();
            }
        }
    }
}


namespace AlarmManage
{
    public class CheckRunningApplicationReceiver
    {
        static bool flag = false;
        public static bool isAppRunning(Context context, String packageName)
        {
            ActivityManager activityManager = (ActivityManager)context.GetSystemService(Context.ActivityService);
            //List<Android.App.ActivityManager.RunningAppProcessInfo> processInfo = activityManager.RunningAppProcesses;
            for (int i = 0; i < activityManager.RunningAppProcesses.Count; i++)
            {
                if (activityManager.RunningAppProcesses[i].ProcessName.Equals("AlarmManage.AlarmManage"))   // make sure your package address of that specific app.
                {
                    //Another App is running
                    Toast.MakeText(context, activityManager.RunningAppProcesses[i].ProcessName.ToString(), ToastLength.Long).Show();
                    flag = true;
                    return flag;
                }
            }
            return flag;
        }
    }
}

编辑:

我看到了这个链接(How to check if activity is in foreground or in visible background?

我编辑了我的代码,现在输出正确。

namespace AlarmManage
{
    public class MyApplication : Application
    {
        private static bool activityVisible;
        public static bool isActivityVisible()
        {
            return activityVisible;
        }

        public static void activityResumed()
        {
            activityVisible = true;
        }

        public static void activityPaused()
        {
            activityVisible = false;
        }
    }
}



namespace AlarmManage
{
    [BroadcastReceiver(Enabled = true)]
    public class MyBroadcastReceiver : BroadcastReceiver 
    {
        private static bool activityVisible;

        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Time Up... Now Vibrating !!!",
                    ToastLength.Long).Show();
            Vibrator vibrator = (Vibrator)context
                    .GetSystemService(Context.VibratorService);
            vibrator.Vibrate(2000);

            if (MyApplication.isActivityVisible() == true)
            {
            }
            else if (MyApplication.isActivityVisible() == false)
            {
                intent = context.PackageManager.GetLaunchIntentForPackage(context.PackageName);
                intent.AddFlags(ActivityFlags.ClearTop);
                context.StartActivity(intent);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

1]在util class中创建一个方法

ncks -X 10,180,10,90 -X -180,0,-90,0 in.nc out.nc

2]检查天气你的应用是否在警报管理器内运行

 public static boolean isAppRunning(final Context context, final String packageName) {
            final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            final List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
            if (procInfos != null)
            {
                for (final ActivityManager.RunningAppProcessInfo processInfo : procInfos) {
                    if (processInfo.processName.equals(packageName)) {
                        return true;
                    }
                }
            }
            return false;
        }