2015-04-02 27 views
0

我想我的应用程序自动启动一个活动时标涉及在一定距离内(在它1米,我的情况)自动当信标来在一定距离

我的活动被当我插上推出发起活动插入或关闭充电器,当我启动设备时,但是当我关闭应用程序并且信标在1米内时没有自动启动。

我想要的是如果信标在1米内,那么活动应该自行启动。

我使用的Android灯塔库,下面就 https://altbeacon.github.io/android-beacon-library/samples.html

我的清单文件代码中提到的相同步骤是

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="org.altbeacon.beaconreference" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="17" 
     android:targetSdkVersion="21" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.BLUETOOTH"/> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> 
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" 
     android:name="org.altbeacon.beaconreference.MyApplicationName"> 

    <activity 
      android:launchMode="singleInstance" 
      android:name="org.altbeacon.beaconreference.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 

</manifest> 

我的应用程序类的代码是:

import java.util.Collection; 
import android.app.Application; 
import android.content.Intent; 
import android.os.RemoteException; 
import android.util.Log; 
import android.widget.Toast; 
import org.altbeacon.beacon.powersave.BackgroundPowerSaver; 
import org.altbeacon.beacon.startup.BootstrapNotifier; 
import org.altbeacon.beacon.startup.RegionBootstrap; 
import org.altbeacon.beacon.Beacon; 
import org.altbeacon.beacon.BeaconManager; 
import org.altbeacon.beacon.BeaconParser; 
import org.altbeacon.beacon.RangeNotifier; 
import org.altbeacon.beacon.Region; 

public class MyApplicationName extends Application implements BootstrapNotifier, RangeNotifier { 
    private static final String TAG = ".MyApplicationName"; 
    private RegionBootstrap regionBootstrap; 
    private BeaconManager mBeaconManager; 
    private Region region; 
    private Region mAllBeaconsRegion; 
    private BackgroundPowerSaver mBackgroundPowerSaver; 
    private RegionBootstrap mRegionBootstrap; 
    @Override 
    public void onCreate() { 

     mAllBeaconsRegion = new Region("all beacons", null, null, null); 
     mBeaconManager = BeaconManager.getInstanceForApplication(this); 
     mBackgroundPowerSaver = new BackgroundPowerSaver(this);  
     mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion); 

     // By default the AndroidBeaconLibrary will only find AltBeacons. If you wish to make it 
     // find a different type of beacon, you must specify the byte layout for that beacon's 
     // advertisement with a line like below. The example shows how to find a beacon with the 
     // same byte layout as AltBeacon but with a beaconTypeCode of 0xaabb 
     //   
     Log.d(TAG, " region. starting ranging"); 

     mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); 
     mBeaconManager.setBackgroundScanPeriod(11000l); 

    } 

    @Override 
    public void didDetermineStateForRegion(int arg0, Region arg1) { 
     // Don't care 
    } 

    @Override 
    public void didEnterRegion(Region arg0) { 

     mRegionBootstrap.disable(); 
     // This call to disable will make it so the activity below only gets launched the first time a beacon is seen (until the next time the app is launched) 
     // if you want the Activity to launch every single time beacons come into view, remove this call. 
     try { 

      mBeaconManager.startRangingBeaconsInRegion(new Region("all beacons", null, null, null)); 
      mBeaconManager.setRangeNotifier(this); 
     } catch (RemoteException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     Log.d(TAG, "Got a didEnterRegion call"); 

    } 

    @Override 
    public void didExitRegion(Region arg0) { 
     // Don't care 
    } 

    @Override 
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) { 
     // TODO Auto-generated method stub 
     if (beacons.size() > 0) { 

      for (Beacon beacon: beacons) {    

       if(beacon.getDistance()<1) 
       { 
        Log.d(TAG, "within 1 minute call"); 
        Intent intent = new Intent(this, MainActivity.class); 
        // IMPORTANT: in the AndroidManifest.xml definition of this activity, you must set android:launchMode="singleInstance" or you will get two instances 
        // created when a user launches the activity manually and it gets launched from here. 
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         this.startActivity(intent);    
       }  

      } 

     } 
    }   
} 

我主要活动等级是:

import android.app.Activity; 
import android.os.Bundle; 

public class MainActivity extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
    } 

} 
+0

好像当你关闭你的应用程序灯塔扫描线停止以及所以它永远不会调用任何检测。我可以现在能看到灯塔的监察代码等。 – Herry 2015-04-02 06:35:02

+0

您是否检查过是否可以触发事件didDetermineStateForRegion,didExitRegion和didRangeBeaconsInRegion?你是如何注册的?代码在哪里? – yushulx 2015-04-02 07:07:00

+0

@Herry:即使关闭应用程序,我也想自动启动我的活动,它仍然会扫描。 – 2015-04-02 08:48:36

回答

0

应用程序关闭后Android Beacon库的行为因其关闭方式而异。如果您使用后退按钮,信标扫描将继续以背景速率进行(,Android 4.x每5分钟一次)。如果使用任务切换器杀死它,它将尽快恢复扫描(电源连接/断开或重新启动)。

全部细节在这里:http://altbeacon.github.io/android-beacon-library/resume-after-terminate.html

你的代码看起来OK做你想要的是上述的参数范围内的东西。当应用程序处于后台时,您可能只会看到五分钟的检测延迟。在后台每五分钟扫描一次以节省电池,但可配置。在Android 5.x上,如果您从无可见的信标到可见的信标,则此延迟不存在。

看到这里的细节:http://altbeacon.github.io/android-beacon-library/battery_manager.html

+0

我想如果我关闭任务切换器的应用程序,它仍然检测到信标,并且如果任何信标在1米的范围内,则活动(在我的情况下为MainActivity)启动。是否有可能 – 2015-04-02 12:01:37

+0

不,如果您从任务切换器中终止应用程序,它会停止所有进程。这就是Android操作系统的工作原理。应用程序只能在外部事件(例如,外部事件)下重新启动。电源连接/断开连接,库重启会自动执行)直到其中一个事件发生,应用程序将不会运行,并且它不可能检测到信标。 – davidgyoung 2015-04-02 14:43:33

+0

大卫您好,我试图实现上面的代码在相同的情况下在后台运行应用程序。我得到一个错误,“BeaconManager不绑定到这项服务”。请让我知道我在这里做了什么错误。 – ssnegi 2017-07-07 12:12:13

相关问题