自动启动一个活动时标是在一个一定的距离 [英] Auto launch an activity when beacon comes in a certain distance

查看:438
本文介绍了自动启动一个活动时标是在一个一定的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的应用程序自动启动一个活动时谈到灯塔在一定距离内(在我的情况下,它是1米)

当我插上插头或断开充电器和我的活动被推出时,我引导设备,但它并没有得到自动启动,当我关闭应用程序和信标是1米。

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

我采用了android灯塔库,以下提到的相同的步骤
https://altbeacon.github.io/android-beacon-library/samples.html

我的清单文件code是

 <清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=org.altbeacon.beaconreference
    安卓版code =1
    机器人:=的versionName1.0>    <用途-SDK
        安卓的minSdkVersion =17
        机器人:targetSdkVersion =21/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>
    <使用许可权的android:NAME =android.permission.BLUETOOTH/>
    <使用许可权的android:NAME =android.permission.BLUETOOTH_ADMIN/>
    <使用特征的android:NAME =android.hardware.bluetooth_le机器人:所需=FALSE/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme
        机器人:名字=org.altbeacon.beaconreference.MyApplicationName>    <活动
            机器人:launchMode =singleInstance
            机器人:名字=org.altbeacon.beaconreference.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
        <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;    < /用途>< /清单>

我的应用程序类code是:

 进口java.util.Collection中;
进口android.app.Application;
进口android.content.Intent;
进口android.os.RemoteException;
进口android.util.Log;
进口android.widget.Toast;
进口org.altbeacon.beacon.powersave.BackgroundPowerSaver;
进口org.altbeacon.beacon.startup.BootstrapNotifier;
进口org.altbeacon.beacon.startup.RegionBootstrap;
进口org.altbeacon.beacon.Beacon;
进口org.altbeacon.beacon.BeaconManager;
进口org.altbeacon.beacon.BeaconParser;
进口org.altbeacon.beacon.RangeNotifier;
进口org.altbeacon.beacon.Region;公共类MyApplicationName扩展应用实现BootstrapNotifier,RangeNotifier {
    私有静态最后弦乐TAG =.MyApplicationName;
    私人RegionBootstrap regionBootstrap;
    私人BeaconManager mBeaconManager;
    专用区域区域;
    专用区域mAllBeaconsRegion;
    私人BackgroundPowerSaver mBackgroundPowerSaver;
    私人RegionBootstrap mRegionBootstrap;
    @覆盖
    公共无效的onCreate(){        mAllBeaconsRegion =新的地区(全信标,NULL,NULL,NULL);
        mBeaconManager = BeaconManager.getInstanceForApplication(本);
        mBackgroundPowerSaver =新BackgroundPowerSaver(本);
        mRegionBootstrap =新RegionBootstrap(这一点,mAllBeaconsRegion);        //默认情况下,AndroidBeaconLibrary只能找到AltBeacons。如果你想使它
        //寻找不同类型的信标,则必须指定该灯塔的字节布局
        //广告中包含一条线。这个例子说明了如何找到与一盏明灯
        //相同的字节布局AltBeacon但0xaabb的beaconType code
        //
        Log.d(TAG,区域开始一应俱全。);        。mBeaconManager.getBeaconParsers()增加(新BeaconParser()setBeaconLayout(M:2-3 = 0215,I:4-19,I:20-21,I:22-23页:24-24)) ;
        mBeaconManager.setBackgroundScanPeriod(11000升);    }    @覆盖
    公共无效didDetermineStateForRegion(INT为arg0,ARG1区){
        //不在乎
    }    @覆盖
    公共无效didEnterRegion(地区为arg0){        mRegionBootstrap.disable();
        //这个调用禁止将使它所以下面只得到活动在第一时间推出一盏明灯看到(应用启动,直到下一次)
       //如果你想在活动推出每一次灯塔映入眼帘,删除此电话。
        尝试{            mBeaconManager.startRangingBeaconsInRegion(新区域(全信标,NULL,NULL,NULL));
            mBeaconManager.setRangeNotifier(本);
        }赶上(RemoteException的E){
            // TODO自动生成catch块
            e.printStackTrace();
        }        Log.d(TAG,得到了didEnterRegion通话);    }    @覆盖
    公共无效didExitRegion(地区为arg0){
        //不在乎
    }    @覆盖
    公共无效didRangeBeaconsInRegion(收集和LT;烽火台>标,地区区){
        // TODO自动生成方法存根
        如果(beacons.size()大于0){            对于(灯塔航标:信标){                如果(beacon.getDistance()&。1)
                {
                    Log.d(TAG,在1分钟的通话);
                    意向意图=新意图(这一点,MainActivity.class);
                    //重要提示:在本次活动的Andr​​oidManifest.xml中定义,必须设置机器人:launchMode =singleInstance还是你会得到两个实例
                    //当用户手动启动该活动创建,而它被从这里启动。
                      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      this.startActivity(意向);
                }            }        }
    }
}

我的主要业务类是:

 进口android.app.Activity;
进口android.os.Bundle;公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }}


解决方案

Android的灯塔图书馆的应用程序后的行为是封闭取决于它是如何关闭是不同的。灯塔扫描保持在背景发生率会(每5在Android 4.x的分钟一次)如果您使用背面的按钮。如果您在任务切换杀它,它就会恢复扫描尽快VAS可能的(电源连接/断开或重启)。

全部细节在这里: HTTP://altbeacon.github。 IO / Android的信标库/恢复-后terminate.html

您code看起来不错,做你想要的是上述的参数范围内的东西。这可能是你只是看到上检测延迟五分钟时,应用程序在后台。扫描一次在后台每隔五分钟做是为了节省电池,而且是可配置的。在Android 5.x的这种延迟是不是present如果你从没有信标去其事信标是可见的可见的。

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

I want my application to auto launch an activity when beacon comes within a certain distance (in my case it is 1 meter)

My activity gets launched when i plug in or plug off the charger and when i boot the device but it didn't get auto launch when i closed the application and beacon is in 1 meter.

what i want is if beacon is in 1 meter then activity should launch by itself.

I am using android beacon library and following the same steps mentioned on https://altbeacon.github.io/android-beacon-library/samples.html

My manifest file code is

 <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>

My application class code is :

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);               
                }       

            }

        }
    }        
}

My Main activity class is:

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);    
    }

}

解决方案

The behavior of the Android Beacon Library after an app is closed is different depending on how it was closed. Beacon scanning keep going at background rates (once every 5 minutes on Android 4.x) if you use the back button. If you kill it with the task switcher, it will resume scanning as soon vas possible (on power connected/disconnected or reboot).

Full details here: http://altbeacon.github.io/android-beacon-library/resume-after-terminate.html

Your code looks OK to do what you want within the parameters of what is described above. It is possible that you are simply seeing a five minute delay on detections when the app is in the background. Scanning once every five minutes in the background is done to save battery, but is configurable. On Android 5.x this delay is not present if you go from no beacons being visible to beacons being visible.

See here for details: http://altbeacon.github.io/android-beacon-library/battery_manager.html

这篇关于自动启动一个活动时标是在一个一定的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆