2017-08-11 62 views
1

上午每隔15分鐘目前正在建設使用廣播接收器Android應用程序觸發,這是一個WifiDetection應用程序,它可以掃描一個獨特的SSID,我的通知工作正常,但始終觸發每1秒(這是惱人的),我希望延遲15分鐘或30分鐘。這裏是我的一些代碼。 我已經使用Thread.sleep()方法和處理程序,但它減緩和崩潰我的應用程序如何推遲NofificationManager在安卓

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

在此先感謝...

回答

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

使自己的服務這將改變布爾值「isNotificationFired」 15分鐘後,保存在PREF的狀態...

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

我想你的代碼,但它沒有按預期方式工作,它只是暫停執行開始,任務啓動時,它會繼續運行每1秒的時間。 –

+0

請使用相同的代碼,但使用IntentService而不是擴展Service ...並寫入 ssid = intent.getStringExtra(「ssid」); handler = new Handler(); handler.postDelayed(可運行,15 * 60 * 1000); // ** 15分鐘(在毫秒時間)** 返回Service.START_STICKY; 裏面onHandleIntent方法 –

+0

onHandleIntent返回void而不是int,所以START_STICKY拋出一個錯誤。使用您的代碼而不使用返回類型會導致應用程序崩潰。 –