1

我尝试使用以下类来发出通知,但它非常不可靠。
日志显示

E/inactive_check: set 15.01.16 15:17
E/inactive_check: receive 15.01.16 15:24

有什么可能是错的?

显现

<receiver
        android:name=".controller.receiver.MyBoot"
        android:enabled="true"
        android:exported="true"
        >
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.QUICKBOOT_POWERON"/>

        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>
    </receiver>
    <receiver
        android:name=".controller.receiver.InactiveReceiver"
        android:exported="true"
        android:enabled="true"
        />

班级

public class InactiveReceiver extends BroadcastReceiver {

  public InactiveReceiver() {
  }

  @Override public void onReceive(Context context, Intent intent) {
    SharedPreferences preferences = context.getSharedPreferences("inactive", Context.MODE_PRIVATE);
    long time = preferences.getLong("time", 0);
    int trial = preferences.getInt("trial", 0);
    Log.e("inactive_check", "receive " + new SimpleDateFormat().format(
        new Date(Calendar.getInstance().getTimeInMillis())));

    switch (trial) {
      case 0: {
        makeNotification(context, trial);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, InactiveReceiver.class);
        PendingIntent pIntent =
            PendingIntent.getBroadcast(context, 9977, i, PendingIntent.FLAG_UPDATE_CURRENT);
        if (Build.VERSION.SDK_INT < 19) {
          am.set(AlarmManager.RTC_WAKEUP, time, pIntent);
        } else {
          am.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
        }
        preferences.edit().putInt("trial", trial + 1).commit();
      }
      break;
      case 1: {
        makeNotification(context, trial);

        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, InactiveReceiver.class);
        PendingIntent pIntent =
            PendingIntent.getBroadcast(context, 9977, i, PendingIntent.FLAG_UPDATE_CURRENT);
        if (Build.VERSION.SDK_INT < 19) {
          am.set(AlarmManager.RTC_WAKEUP, time, pIntent);
        } else {
          am.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
        }
        preferences.edit().putInt("trial", trial + 1).commit();
      }
      break;
      case 2: {
        makeNotification(context, trial);
        preferences.edit().putInt("trial", trial + 1).commit();
      }
      break;
    }
  }

  private static void makeNotification(Context context, int trial) {
    Intent intent = new Intent(context, LoginActivity.class);
    // use System.currentTimeMillis() to have a unique ID for the pending intent
    PendingIntent pIntent =
        PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    String subject;
    switch (trial) {
      case 0:
        subject = "Твои новые клиенты - это друзья друзей ";
        break;
      case 1:
        subject = "Номерок. Много клиентов не бывает";
        break;
      case 2:
        subject = "500 новых клиентов за 2 месяца - легко. Начни сейчас!";
        break;
      default:
        subject = "Твои новые клиенты -это друзья друзей ";
    }
    // build notification
    // the addAction re-use the same intent to keep the example short
    Notification n = new NotificationCompat.Builder(context).setContentTitle("Вы давно не заходили")
        .setContentText(subject)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pIntent)
        .setAutoCancel(true)
        .build();

    NotificationManager notificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(9978, n);
  }

  public static void cancellInActiveCheck(Context context) {
    SharedPreferences preferences = context.getSharedPreferences("inactive", Context.MODE_PRIVATE);
    preferences.edit().clear().commit();
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, InactiveReceiver.class);
    PendingIntent intent =
        PendingIntent.getBroadcast(context, 9977, i, PendingIntent.FLAG_UPDATE_CURRENT);
    am.cancel(intent);
  }

  public static void setInActiveCheck(Context context) {
    SharedPreferences preferences = context.getSharedPreferences("inactive", Context.MODE_PRIVATE);
    Calendar c = Calendar.getInstance();
    long time = c.getTimeInMillis();
    SharedPreferences.Editor editor = preferences.edit();
    editor.putLong("time", time);
    editor.putInt("trial", 0);
    editor.commit();

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, InactiveReceiver.class);
    PendingIntent intent =
        PendingIntent.getBroadcast(context, 9977, i, PendingIntent.FLAG_UPDATE_CURRENT);
    time = getTime(1);
    Log.e("inactive_check", "set " + new SimpleDateFormat().format(new Date(time)));

    if (Build.VERSION.SDK_INT < 19) {
      am.set(AlarmManager.RTC_WAKEUP, time, intent);
    } else {
      am.setExact(AlarmManager.RTC_WAKEUP, time, intent);
    }
  }

  private static long getTime(int days) {
    return TimeUnit.MILLISECONDS.convert(days, TimeUnit.MINUTES) + Calendar.getInstance()
        .getTimeInMillis();
  }
}
4

0 回答 0