2017-08-11 33 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抛出一个错误。使用您的代码而不使用返回类型会导致应用程序崩溃。 –