2016-10-27 20 views
2

私はAndroidの開発者には新しく、私のアプリには問題があります。カスタムBroadcastReceiverを作成しましたが、アプリを起動すると自動的に通知が実行されます。私は、AlarmManagerのスケジュールが始まるまで、この通知を実行したくありません。Androidの起動時に通知が自動的に起動します

このアプリは私の論文であるため、本当に助けが必要です。

通知受信:ここ

は私のコードです私AndroidManifestの

public class NotificationReceiver extends BroadcastReceiver { 
    String cnh_vencida; 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (intent.getAction().equals("ALARME_DISPAROU")) { 

      Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
      cnh_vencida = context.getString(R.string.cnh_vencimento); 

      PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, CadastroUser.class), 0); 

      Notification notification = new NotificationCompat.Builder(context) 
        .setTicker("teste") 
        .setCategory(Notification.CATEGORY_ALARM) 
        .setSmallIcon(R.mipmap.car) 
        .setContentTitle("CarMaintenance") 
        .setContentText(cnh_vencida) 
        .setContentIntent(pi) 
        .setAutoCancel(true) 
        .setSound(uri) 
        .setPriority(Notification.PRIORITY_HIGH) 
        .setVisibility(Notification.VISIBILITY_PUBLIC) 
        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) 
        .build(); 
      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(0, notification); 

     } 
    } 
} 

パート

<receiver 
     android:name=".notification.AlarmReceiver" 
     android:enabled="false"> 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name=".notification.NotificationReceiver" 
     android:exported="false" 
     android:enabled="false"> 
     <intent-filter> 
      <action android:name="ALARME_DISPAROU" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </receiver> 
</application> 

AlarmReceiver(ユーザーがデバイスを再起動した場合、このクラスは、アラームのスケジュールを変更します)

public class AlarmReceiver extends BroadcastReceiver { 
    String dataValidade; 
    Date date; 
    String CATEGORIA; 

    public void onReceive(Context context, Intent intent) { 

     if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) { 
      recreateAlert(context); 
     } 
    } 

    public void recreateAlert(Context context) { 

     CadastroUserDAO cadastroUserDAO = new CadastroUserDAO(context); 

     List<CadastroUserModel> registros; 
     registros = cadastroUserDAO.listarTodos(); 
     if (registros.size() > 0) { 
      Log.i(CATEGORIA, "Existe CNH Cadastrada"); 

      for (int i = 0; i < registros.size(); i++) { 
       dataValidade = (registros.get(i).getVALIDADE_CNH()); 
      } 

      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
      try { 
       date = simpleDateFormat.parse(dataValidade); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } 
      Calendar calendar = Calendar.getInstance(); 
      calendar.setTime(date); 
      calendar.add(calendar.DAY_OF_MONTH, -45); 

      AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
      Intent alarmIntent = new Intent("ALARME_DISPAROU"); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 
      alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 5, pendingIntent); 
     } 
    } 
} 

ご協力ありがとうございます!

+0

私の答えはあなたのために機能しましたか?はいの場合はそれを受け入れることを検討する – Marat

答えて

0

オプションの1つは、<intent-filter>を取り除き、NotificationReceiverを直接インテントで指定することです。マニフェストで

:AlarmReceiverで

<receiver android:name=".notification.NotificationReceiver" /> 

AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
Intent alarmIntent = new Intent(context, NotificationReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0); 
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 5, pendingIntent); 

そしてNotificationReceiverファイルにif文を削除してください。

関連する問題