Android 如何从电源按钮自动启动应用程序页面

How to launch app page from power button automatically on Android

我正在 VS 2017 中使用 Xamarin Android 为我的侄女制作一个小应用程序。我希望它在应用程序上打开一个页面,每次她打开 phone。我希望它 运行 作为前台服务,所以当应用程序最小化时它仍然会这样做。我有一个广播接收器,它在应用程序获得焦点时检测到按钮事件,我已将其移动到 运行 的服务。

目前我按下切换按钮,然后 5-10 秒后它抛出错误。在 VS 中看不到错误,因为它是外部的。我假设那是 Genymotion 模拟器捕捉到的?

有什么想法吗?

更新:如果将来对其他人有用,我想我会来更新它。 Oreo 更新后 NotificationCompat.Builder 现在需要提供频道 ID。我已经在代码中反映了这一点,它现在可以正确启动服务。

我仍然需要通过该服务让广播接收器正常工作,但我已经做到了!

更新 2:工作完成!

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    Intent startServiceIntent;
    Intent stopServiceIntent;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        // Difficulty Radio Buttons
        RadioButton radioEasy = FindViewById<RadioButton>(Resource.Id.radioEasy);
        radioEasy.Click += RadioButton_Click;
        RadioButton radioMedium = FindViewById<RadioButton>(Resource.Id.radioMedium);
        radioMedium.Click += RadioButton_Click;
        RadioButton radioHard = FindViewById<RadioButton>(Resource.Id.radioHard);
        radioHard.Click += RadioButton_Click;

        // Set/Remove Lock
        Button ToggleLock = FindViewById<Button>(Resource.Id.ToggleLock);
        ToggleLock.Click += ToggleLock_Click;

        startServiceIntent = new Intent(this, typeof(LockService));
        stopServiceIntent = new Intent(this, typeof(LockService));
    }

    private void RadioButton_Click(object sender, EventArgs e)
    {
        var radiobtn = sender as RadioButton;
        Toast.MakeText(this, radiobtn.Text, ToastLength.Long).Show();
    }
    private void ToggleLock_Click(object sender, EventArgs e)
    {
        var togglebtn = sender as ToggleButton;
        if (togglebtn.Checked)
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                StartForegroundService(startServiceIntent);
            else
                StartService(startServiceIntent);

            Toast.MakeText(this, "On", ToastLength.Long).Show();
        }
        else
        {
            Toast.MakeText(this, "Off", ToastLength.Long).Show();
        }
    }
}


/// <summary>
/// ////////////////////////////////////////////////////////////////////////////
/// </summary>

[Service]
public class LockService : Service
{
    private PowerBroadcastReceiver receiver;

    public override void OnCreate()
    {
    }

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        NotificationManager mngr = (NotificationManager)GetSystemService(NotificationService);
        NotificationChannel chnl = new NotificationChannel("Channel", "Name", NotificationImportance.Default);
        mngr.CreateNotificationChannel(chnl);

        NotificationCompat.Builder foregroundNotification = new NotificationCompat.Builder(this);
        foregroundNotification.SetOngoing(true);
        foregroundNotification.SetContentTitle("My Foreground Notification")
                .SetChannelId("Channel")
                .SetContentText("This is the first foreground notification Peace");

        StartForeground(101, foregroundNotification.Notification);

        // Broadcast Receiver
        receiver = new PowerBroadcastReceiver();
        RegisterReceiver(receiver, new IntentFilter(Intent.ActionScreenOn));

        return StartCommandResult.Sticky;
    }

    public override IBinder OnBind(Intent intent)
    {
        // Return null because this is a pure started service. A hybrid service would return a binder that would
        // allow access to the GetFormattedStamp() method.
        return null;
    }

}

/// <summary>
/// ////////////////////////////////////////////////////////////////////////////////
/// </summary>

[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionScreenOn, Intent.ActionScreenOff })]
public class PowerBroadcastReceiver : BroadcastReceiver
{
    public PowerBroadcastReceiver()
    {
    }

    public override void OnReceive(Context context, Intent intent)
    {
        if (intent.Action.Equals(Intent.ActionScreenOn))
        {
            // TODO: Launch Question Page

            Toast.MakeText(context, "On", ToastLength.Long).Show();
        }
        else if (intent.Action.Equals(Intent.ActionScreenOff))
            Toast.MakeText(context, "Off", ToastLength.Long).Show();
    }
}

谢谢。

你可以创建一个监听器class来监听android锁屏和解锁事件,使用广播接收屏幕状态。由于是系统广播,我觉得没必要用Service。

你可以参考这个: