0
私は共有設定で1つのBBSIDを持っています。ユーザーがWifiネットワークに接続するたびに、サービスはSharedPreferencesに格納されているものと同じBBSIDであるかどうかをチェックする必要があります。私は以下のコードを実装しましたが、問題は、無線ネットワークに接続したときに通知が複数回作成されることです。通知を送信する方法特定のWifi BBSID(Shared Preference内)のモバイルが、サービス付きのBBSIDに接続していますか?
public class WifiConnectionNotificationService extends Service {
BroadcastReceiver mReceiver;
String bssid = "64:66:b3:f0:ce:81";
/*
WifiConnectionNotificationService(String ssid) {
this.ssid = ssid;
}*/
// use this as an inner class like here or as a top-level class
public class IsWifiConnectedBroadcastReceiver extends BroadcastReceiver {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
if (intent.getAction().contentEquals("android.net.wifi.STATE_CHANGE") || intent.getAction().contentEquals("android.net.conn.CONNECTIVITY_CHANGE")) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
if (wifiManager.isWifiEnabled()) {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
if (wifiInfo.getBSSID().contentEquals(bssid)) {
PushNotification(context);
} else {
if (Context.NOTIFICATION_SERVICE != null) {
notificationManager.cancelAll();
}
}
}
} else {
if (Context.NOTIFICATION_SERVICE != null) {
notificationManager.cancelAll();
}
}
}
}
}
@Override
public void onCreate() {
// get an instance of the receiver in your service
IntentFilter filter = new IntentFilter();
filter.addAction("android.net.wifi.STATE_CHANGE");
filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
mReceiver = new IsWifiConnectedBroadcastReceiver();
registerReceiver(mReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void PushNotification(Context context) {
NotificationManager nm = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
Intent notificationIntent = new Intent(context, Register.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
//set
builder.setContentIntent(contentIntent);
builder.setSmallIcon(android.R.drawable.ic_notification_clear_all);
builder.setContentText("Do you want me to start your Bike ?");
builder.setContentTitle("Keyless Moto");
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
nm.notify((int) System.currentTimeMillis(), notification);
}
}
ありがとうございます:) – user3739891