0

当手机被锁定并且屏幕关闭但它不起作用时,我正在使用警报服务来保持我的应用程序运行。

我的警报服务类是:

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);

  }
} 

我的广播服务类是:

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);
}

}

我的主要 Activity 类有用于 WakeLock 的 PowerManager 服务

 @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);

请帮助我。当手机被锁定并且屏幕关闭时如何保持我的应用程序运行?

4

3 回答 3

1

如果您想在屏幕锁定时运行应用程序并运行警报,那么您需要在启动时启动服务。可能这个链接对你有帮助。 http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/

首先阅读并一步一步地做你自己。我希望它对你有帮助。

于 2012-11-19T06:04:58.257 回答
0

我可以建议您一件事..当您的应用程序启动或服务启动时,您可以尝试解锁键盘锁。

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

因此,当您的应用程序运行时,它将解锁密钥保护并保持屏幕打开。并且当服务停止时,再次锁定钥匙护罩。

注意:不要使用 PowerManager 类。因为它会很快耗尽电池。我遇到了这个问题。

谢谢。

于 2012-11-19T05:59:55.393 回答
0

您的代码很好,只需要使用静态修饰符将您的警报变量更改为类级别,如下所示,

public class AlarmService extends BroadcastReceiver 
{
     private static Intent myIntent;
     private static AlarmManager service;
     private static PendingIntent pendingIntent;
     .
     . 
     .
}
于 2012-11-19T05:41:44.700 回答