问题描述
我使用的报警服务,让应用程序运行时,我的手机被锁定,屏幕关闭,但它不能正常工作。
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那么你需要在开机启动服务。可能是这个环节是有益的给你。 http://www.androidcompetencycenter.com/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.http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
先阅读和做一步你自己的一步。我希望这是有益的给你。
first read and do your self step by step. i hope it is helpfull to you.
这篇关于我如何运行我的应用程序时,设备处于锁定状态和屏幕处于关闭状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!