当我的手机处于睡眠模式时,不会显示简单的通知

时间:2016-05-05 09:41:21

标签: android android-notifications android-wake-lock

我正在实施地图位置闹钟,当我即将到达目的地时我成功收到通知,但问题是我的手机处于睡眠模式,我无法收到通知,我不知道如何使用系统,PLZ帮助我。

这是我的代码..

public class AreWeThereIntentService extends IntentService {

  private final String TAG = AreWeThereIntentService.class.getName();

  private SharedPreferences prefs;
  private Gson gson;

  public AreWeThereIntentService() {
    super("AreWeThereIntentService");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    prefs = getApplicationContext().getSharedPreferences(
            Constants.SharedPrefs.Geofences, Context.MODE_PRIVATE);
    gson = new Gson();

// 1. Get the event
    GeofencingEvent event = GeofencingEvent.fromIntent(intent);
    if (event != null) {
      if (event.hasError()) {
        onError(event.getErrorCode());
      } else {

        // 2. Get the transition type
        int transition = event.getGeofenceTransition();
        if (transition == Geofence.GEOFENCE_TRANSITION_ENTER ||
                transition == Geofence.GEOFENCE_TRANSITION_DWELL ||
                transition == Geofence.GEOFENCE_TRANSITION_EXIT) {
          List<String> geofenceIds = new ArrayList<>();

          // 3. Accumulate a list of event geofences
          for (Geofence geofence : event.getTriggeringGeofences()) {
            geofenceIds.add(geofence.getRequestId());
          }
          if (transition == Geofence.GEOFENCE_TRANSITION_ENTER ||
                  transition == Geofence.GEOFENCE_TRANSITION_DWELL) {
            // 4. Pass the geofence list to the notification method
            onEnteredGeofences(geofenceIds);
          }
        }
      }

    }
  }


  private void onEnteredGeofences(List<String> geofenceIds) {
    // 1. Outer loop over all geofenceIds
    for (String geofenceId : geofenceIds) {
      String geofenceName = "";

      // 2, Loop over all geofence keys in prefs and retrieve NamedGeofence from SharedPreferences
      Map<String, ?> keys = prefs.getAll();
      for (Map.Entry<String, ?> entry : keys.entrySet()) {
        String jsonString = prefs.getString(entry.getKey(), null);
        NamedGeofence namedGeofence = gson.fromJson(jsonString, NamedGeofence.class);
        if (namedGeofence.id.equals(geofenceId)) {
          geofenceName = namedGeofence.name;
          break;
        }
      }

      // 3. Set the notification text and send the notification
      String contextText =
              String.format(this.getResources().getString(R.string.Notification_Text), geofenceName);
      // 1. Create a NotificationManager
      NotificationManager notificationManager =
              (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

// 2. Create a PendingIntent for AllGeofencesActivity
      Intent intent = new Intent(this, AllGeofencesActivity.class);
      intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
      PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

      Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      long[] pattern = { 0, 100, 200, 300 };
// 3. Create and send a notification
      Notification notification = new NotificationCompat.Builder(this)
              .setSmallIcon(R.mipmap.map_marker_icon)
              .setContentTitle(this.getResources().getString(R.string.Notification_Title))
              .setContentText(contextText)
              .setContentIntent(pendingNotificationIntent)
              .setStyle(new NotificationCompat.BigTextStyle().bigText(contextText))
              .setPriority(NotificationCompat.PRIORITY_HIGH)
              .setAutoCancel(true)
              .setSound(alarmSound)
              .setVibrate(pattern)
              .build();
      notificationManager.notify(0, notification);
    }
  }

  private void onError(int i) {
    Log.e(TAG, "Geofencing Error: " + i);
  }

}

我找到了解决方案,我必须使用唤醒锁,但我不知道如何使用和我必须实现的地方,我需要的是,如果我的通知仍然未决,那么我不希望我的手机睡觉,当我收到通知然后释放唤醒锁。我该怎么做。

1 个答案:

答案 0 :(得分:0)

启动通知时尝试此方法

acquireWakeLock():收到任何新推送通知时设备唤醒。

private PowerManager.WakeLock wakeLock;

    public  void acquireWakeLock(Context context) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) 
                          context.getSystemService(Context.POWER_SERVICE);

        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "WakeLock");

        wakeLock.acquire();
    }

    public  void releaseWakeLock() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }