2017-08-11 8 views
1

BroadcastReceiverを使ってアンドロイドアプリケーションを構築していますが、ユニークなSSIDをスキャンするWifiDetectionアプリケーションですが、通知はうまくいきますが、それが15分または30分遅れることを望む。ここに私のコードのいくつかがあります。 私はのThread.sleep()およびハンドラを使用しているが、それは自分のアプリケーションNofificationManagerをアンドロイドで15分ごとに起動する方法を遅らせる方法

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を使用してください。 ssid = intent.getStringExtra( "ssid"); ハンドラ=新しいハンドラ(); handler.postDelayed(実行可能、15 * 60 * 1000); // ** 15分(ミリ秒単位の時間)** return Service.START_STICKY; onHandleIntentメソッド内 –

+0

onHandleIntentはintではなくvoidを返します。したがって、START_STICKYはエラーをスローします。戻り値の型を持たないコードを使用すると、アプリがクラッシュします。 –

関連する問題