我如何运行我的应用程序时,设备处于锁定状态和屏幕处于关闭状态?

编程入门 行业动态 更新时间:2024-10-23 17:39:44
本文介绍了我如何运行我的应用程序时,设备处于锁定状态和屏幕处于关闭状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的报警服务,让应用程序运行时,我的手机被锁定,屏幕关闭,但它不能正常工作。

I am using Alarm services to keep my application running when mobile is locked and screen turned off but it does not work.

我的报警服务类是:

public class AlarmService extends BroadcastReceiver { // Restart service every 60 seconds private static final long REPEAT_TIME = 1000 * 60; @Override public void onReceive(Context context, Intent intent) { AlarmManager service = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); Intent i = new Intent(context, StartUp_broadcast.class); PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT); Calendar cal = Calendar.getInstance(); // Start 60 seconds after boot completed cal.add(Calendar.SECOND, 60); // // Fetch every 60 seconds // InexactRepeating allows Android to optimize the energy consumption service.setRepeating (AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending); // service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), // REPEAT_TIME, pending); } }

和我brodcast服务类是:

and my brodcast service class is:

public class StartUp_broadcast extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent myIntent = new Intent(context, AlarmService.class); PendingIntent pendingIntent = PendingIntent. getService(context, 0, myIntent, 0); AlarmManager alarmManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000, 1000, pendingIntent); Intent Startup_Intent = new Intent(context, LocationService.class); Startup_Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startService(Startup_Intent); } }

我的主要活动类有PowerManager的服务为WakeLock

My main Activity class has PowerManager Service for WakeLock

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeUp"); wl.acquire(1000); //wake up the screen setContentView(R.layout.main); this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

请帮我。如何保持应用程序运行时,我的手机被锁定,scrren熄灭?

Kindly help me. How to keep my application running when mobile is locked and scrren is off ?

推荐答案

如果你想在运行应用程序的屏幕锁定,并运行alaram那么你需要在开机启动服务。可能是这个环节是有益的给你。 www.androidcompetencycenter/2009/06/start-服务在启动/

If you want to run application when screen lock and also run alaram then you need to start service at boot. may be this link is helpfull to you. www.androidcompetencycenter/2009/06/start-service-at-boot/

先阅读和做一步你自己的一步。我希望这是有益的给你。

first read and do your self step by step. i hope it is helpfull to you.

更多推荐

我如何运行我的应用程序时,设备处于锁定状态和屏幕处于关闭状态?

本文发布于:2023-11-05 04:59:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   应用程序   屏幕   设备

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!