如何从服务发送数据甚至关闭应用程序

时间:2016-11-11 10:37:13

标签: android service altbeacon beacon android-ibeacon

我正在使用Alt信标库进行信标扫描。我正在服务类中扫描信标。我想继续扫描信标甚至应用程序也关闭。但是如果关闭应用程序服务停止并且信标不扫描。我试过但我没有得到。请帮助我,谢谢你 // MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 setContentView(R.layout.activity_main);

    Intent intent = new Intent(MainActivity.this, ScaningService.class);
    startService(intent);//starting service
    getApplicationContext().bindService(intent, mConnection, Context.BIND_AUTO_CREATE);//bounding service
}  

@Override
protected void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
}

@Override
protected void onDestroy() {
    super.onDestroy();

}

private ServiceConnection mConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName className,
                                   IBinder service) {           


    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {


    }
};

//服务类

public class ScaningService extends Service implements BeaconConsumer {
public BeaconManager beaconManager;
public MainActivity activity;  

private final IBinder mBinder = new SimpleServiceBinder();  


public class SimpleServiceBinder extends Binder {
    public ScaningService getService() {
        return ScaningService.this;
    }
}

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


@Override
public void onCreate() {
    super.onCreate();
    handler = new Handler();
    beaconManager = BeaconManager.getInstanceForApplication(getBaseContext());
    beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    beaconManager.setBackgroundMode(true);
    beaconManager.setBackgroundScanPeriod(1100);//this will set how long a bluetooth should scan
    beaconManager.setBackgroundBetweenScanPeriod(2000);//this will set bluetooth scanning interval        
    beaconManager.bind(ScaningService.this);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Override
public boolean onUnbind(Intent intent) {
    return super.onUnbind(intent);
}

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

@Override
public void onBeaconServiceConnect() {

    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

            if (beacons.size() > 0) {

                beacon = beacons.iterator().next();
                Log.e(TAG, " UUID: " + beacon.getId1());// this is not showing and i tried toast also                 
        }
    });

    try {           

        beaconManager.startRangingBeaconsInRegion(new Region("sBeacon", null, null, null));            

    } catch (RemoteException e) {
        Log.i(TAG, "RemoteException: " + e);
    }
}

}

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。正确配置后,Android Beacon Library将在您的应用被杀后五分钟内在后台恢复扫描。在测距回调中,您可以使用扫描结果将数据发送到服务器,尽管您可能需要在新线程上执行此操作(使用Handler或类似构造。)

上面项目网站上的例子应该可以帮到你。您需要在自定义Application类中组合后台启动和范围。

编辑:看到发布的代码后,显然问题是扫描是从自定义服务触发的。虽然这可以工作,但要让它正常工作更难以扫描重新启动应用程序被杀死。通过使用RegionBootstrap使用自定义Application类触发背景检测的示例,可以更加简单。