屏幕关闭后Android应用停止记录到控制台

时间:2016-04-03 09:00:59

标签: android xamarin broadcastreceiver android-broadcastreceiver

我正在创建一个应用程序来检测移动数据状态更改并将某些内容记录到控制台。这个应用程序使用唤醒广播接收器这样做。

当屏幕 时,精细 时, (它没有&# 39; t将任何内容记录到控制台。)

我在 Android 4.0.3 设备上使用 C#/ Xamarin ,但它的代码应该非常熟悉给java用户。此应用还具有以下权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

以下是整个应用:

using System;
using System.Diagnostics;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Support.V4.Content;
using Android.Telephony;

namespace DataChangeDetector
{
    [Activity(Label = "DataChangeDetector", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        //private TelephonyManager tManager;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            SendBroadcast(new Intent(this, typeof(DataChangeHandler)));
        }
    }

    [BroadcastReceiver(Enabled = true)]
    public class DataChangeHandler : WakefulBroadcastReceiver
    {
        public static void SetUp(Context context)
        {
            TelephonyManager tManager = TelephonyManager.FromContext(context);
            tManager.Listen(new CustomPhoneStateListener(context), PhoneStateListenerFlags.DataActivity);
        }

        public override void OnReceive(Context context, Intent intent)
        {
            Console.WriteLine("OnReceive");
            SetUp(context);
        }
    }

    public class CustomPhoneStateListener : PhoneStateListener
    {
        private Context mContext;
        private static readonly TimeSpan MinInterval = TimeSpan.FromSeconds(8); // 8 Seconds
        private readonly Stopwatch stopwatch = new Stopwatch(); // Stopped initially

        public CustomPhoneStateListener(Context context)
        {
            mContext = context;
        }

        public override void OnDataActivity(DataActivity direction)
        {
            base.OnDataActivity(direction);
            if (direction == DataActivity.In)
            {
                stopwatch.Restart();
                Console.WriteLine("Recieved Data");
            }
            else if (direction == DataActivity.None)
            {
                if (stopwatch.IsRunning && stopwatch.Elapsed < MinInterval)
                {
                    return;
                }
                try
                {
                    Console.WriteLine("No Data");
                    // TODO: Reset the data
                }
                finally
                {
                    stopwatch.Restart();
                }
            }
        }
    }
}

修改 当屏幕关闭时,它不会向控制台记录任何内容,甚至不会记录系统日志。 (即使屏幕关闭,我的Nexus 5也会记录系统日志和应用程序日志)

当关闭屏幕从IDE运行应用程序时,它只会加载一些.net程序集。来自控制台的最后一行:

Loaded assembly: System.dll
04-03 13:38:10.510 D/libEGL  (16322): loaded /vendor/lib/egl/libEGL_POWERVR_SGX530_125.so
04-03 13:38:10.522 D/libEGL  (16322): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX530_125.so
04-03 13:38:10.528 D/libEGL  (16322): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX530_125.so
04-03 13:38:10.610 D/OpenGLRenderer(16322): Enabling debug mode 0
04-03 13:38:10.631 V/KeyEvent(16322): Reset: android.view.KeyEvent$DispatcherState@4186b6f8
04-03 13:38:10.682 W/IInputConnectionWrapper(16322): showStatusIcon on inactive InputConnection

此应用只需在此特定设备(LG P970)和操作系统(4.0.3 ICS)上运行。

0 个答案:

没有答案