2017-08-11 3 views
1

Je construis actuellement une application Android en utilisant BroadcastReceiver, c'est une application WifiDetection qui recherche un SSID unique, Ma notification fonctionne bien mais se déclenche toujours toutes les 1 secondes (ce qui est ennuyeux), je Je veux qu'il soit retardé de 15 minutes ou de 30 minutes. voici quelques-uns de mes codes. Je l'ai utilisé Thread.sleep() et les gestionnaires, mais il ralentit et s'écrase ma demandeComment retarder NofificationManager pour déclencher toutes les 15 minutes dans Android

public class WifiReceiver extends BroadcastReceiver { 

private void receiveNotification(Context context, String ssid) { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
    Intent intent = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    builder.setSmallIcon(R.drawable.wifilogosmaller); 
    builder.setContentTitle("Free " + ssid + ", Click for more info"); 
    builder.setContentText("Wifi.com.ng services avaliable "); 
    builder.setAutoCancel(true); 
    builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.wifilogosmaller)); 
    builder.setSubText("Tap to view"); 
    builder.setContentIntent(pendingIntent); 
    builder.setVisibility(Notification.VISIBILITY_PUBLIC); 
    Notification notification = builder.build(); 
    NotificationManager mgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 
    mgr.notify(1001, notification); 

}

@Override 
public void onReceive(Context context, Intent intent) 
    { 
    //Checked for the particular SSID am looking for 
    if(condition is met){ 
    receiveNotification(Context context, "My unique SSID") 
    }  
    } 

Merci à l'avance ...

Répondre

0
@Override 
public void onReceive(Context context, Intent intent) 
    { 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 

if(!prefs.getBoolean("isNotificationFired", false)) 
    { 
    receiveNotification(Context context, "My unique SSID") 
    prefs.edit().putBoolean("isNotificationFired", true).commit(); 
    //start service here 
    Intent intent=new Intent(context, MyService.class); 
    context.startService(intent);  
    } 
    } 

private void receiveNotification(Context context, String ssid) { 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
    Intent intent = new Intent(context, MainActivity.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    builder.setSmallIcon(R.drawable.wifilogosmaller); 
    builder.setContentTitle("Free " + ssid + ", Click for more info"); 
    builder.setContentText("Wifi.com.ng services avaliable "); 
    builder.setAutoCancel(true); 
    builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.wifilogosmaller)); 
    builder.setSubText("Tap to view"); 
    builder.setContentIntent(pendingIntent); 
    builder.setVisibility(Notification.VISIBILITY_PUBLIC); 
    Notification notification = builder.build(); 
    NotificationManager mgr = (NotificationManager) 
    context.getSystemService(NOTIFICATION_SERVICE); 
    mgr.notify(1001, notification); 
} 

Faites votre propre service qui va changer le statut de la valeur booléenne "isNotificationFired" sauvegardée dans pref après 15 min ...

public class MyService extends Service { 
    private final LocalBinder mBinder = new LocalBinder(); 
    protected Handler handler; 

    public class LocalBinder extends Binder { 
     public MyServicegetService() { 
      return MyService.this; 
     } 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return mBinder; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     handler = new Handler(); 
     handler.postDelayed(runnable, 15*60*1000);//**15 min(time in millisecond)** 
     return Service.START_STICKY; 
    } 

    Runnable runnable = new Runnable() { 
     @Override 
     public void run() { 

     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(); 
     prefs.edit().putBoolean("isNotificationFired", true).commit(); 

     } 
    }; 
+0

J'ai essayé votre code, mais il n'a pas fonctionné comme prévu, il a seulement mis en pause le temps pour l'exécution de début, quand la tâche commence, il continue à courir toutes les 1sec. –

+0

Veuillez utiliser le même code mais utilisez IntentService au lieu d'étendre Service ... et écrivez ssid = intention.getStringExtra ("ssid"); handler = nouveau gestionnaire(); handler.postDelayed (exécutable, 15 * 60 * 1000); // ** 15 min (temps en millisecondes) ** return Service.START_STICKY; à l'intérieur de la méthode onHandleIntent –

+0

onHandleIntent renvoie void plutôt qu'int, de sorte que START_STICKY renvoie une erreur. l'utilisation de votre code sans type de retour fait planter l'application. –