Android iBeacon应用程序无法在后台运行

Android iBeacon应用程序无法在后台运行,第1张

概述我开发了Android应用程序,以在客户接近1mt距离时检测到信标.我正在使用AndroidBeaconLibrary开发应用程序.当我们处于前台时,我会收到通知,但是一旦应用程序进入后台,通知就会开始工作.你能帮我哪里错了吗?下面是BeaconApp和MainActivity的代码.BeaconApp.javapublicclassB

我开发了Android应用程序,以在客户接近1 mt距离时检测到信标.我正在使用AndroID Beacon library开发应用程序.当我们处于前台时,我会收到通知,但是一旦应用程序进入后台,通知就会开始工作.你能帮我哪里错了吗?
下面是BeaconApp和MainActivity的代码.

BeaconApp.java

public class BeaconApp extends Application implements bootstrapNotifIEr {    private static final String TAG = "BeaconApp";    private Regionbootstrap regionbootstrap;    private Region allbeaconsregions;    private BackgroundPowerSaver bgSaver;    BeaconManager beaconManager;    @OverrIDe    public voID onCreate() {        super.onCreate();        Log.d(TAG, "App started up");        // To detect proprIEtary beacons, you must add a line likebelowcorresponding to your beacon        // type.  Do a web search for "setBeaconLayout" to get the proper Expression.        // beaconManager.getBeaconParsers().add(new BeaconParser().        //        setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));        // wake up the app when any beacon is seen (you can specify specific ID filers in the parameters below)        beaconManager = BeaconManager.getInstanceForApplication(this);        Beacon.setHarDWareEqualityEnforced(true);        bgSaver = new BackgroundPowerSaver(this);        beaconManager.getBeaconParsers().add(new BeaconParser().                setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));        allbeaconsregions = new Region("treewalker", null, null, null);        regionbootstrap = new Regionbootstrap(this, allbeaconsregions);        //beaconManager.bind(this);    }    @OverrIDe    public voID dIDDetermineStateForRegion(int arg0, Region arg1) {        // Don't care        Log.d(TAG, "Enter in  dIDDetermineStateForRegion call");    }    @OverrIDe    public voID dIDEnterRegion(Region arg0) {        Log.d(TAG, "Got a dIDEnterRegion call");        // 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.        regionbootstrap.disable();        Intent intent = new Intent(this, MainActivity.class);        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        // important:  make sure to add androID:launchMode="singleInstance" in the manifest        // to keep multiple copIEs of this activity from getting created if the user has        // already manually launched the app.        this.startActivity(intent);    }    @OverrIDe    public voID dIDExitRegion(Region arg0) {        // Don't care        Log.d(TAG, "Enter in  dIDExitRegion call");    }}

在MainActivity.java中

public class MainActivity extends AppCompatActivity implements BeaconConsumer,RangeNotifIEr {    protected static final String TAG = "MainActivity";    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        mBeaconManager = BeaconManager.getInstanceForApplication(this);        Beacon.setHarDWareEqualityEnforced(true);        BackgroundPowerSaver bgSaver = new BackgroundPowerSaver(this);        mBeaconManager.getBeaconParsers().add(new BeaconParser().                setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));        mBeaconManager.bind(this);    }    private BeaconManager mBeaconManager;    public voID onResume() {        super.onResume();        mBeaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());        // Detect the main Eddystone-UID frame:        mBeaconManager.getBeaconParsers().add(new BeaconParser().                setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));        mBeaconManager.bind(this);    }   public voID onBeaconServiceConnect() {           try {               mBeaconManager.startRangingBeaconsInRegion(new Region("treewalker", null, null, null));               mBeaconManager.addRangeNotifIEr(this);           }catch (remoteexception e){               e.printstacktrace();           }   }    @OverrIDe    public voID dIDRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {        if(beacons.size() > 0) {            for (Beacon beacon : beacons) {                if (beacon.getdistance() < 1.0) {                    Log.d(TAG, "I see a beacon transmitting a : " +                            " approximately " + beacon.getdistance() + " meters away.");                    Log.d(TAG, "BEACON DATA : " +beacon.getBluetoothAddress()+":"+beacon.getBluetoothname()+":"+beacon.getID1());                    showNotification("Treewalker","You are near beacon range");                    Intent intent = new Intent(this,MainActivity.class);                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                    this.startActivity(intent);                }            }        }    }    public voID showNotification(String Title, String message) {        Intent notifyIntent = new Intent(this, DashboardActivity.class);        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_top);        PendingIntent pendingIntent = PendingIntent.getActivitIEs(this, 0,                new Intent[] { notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);        Notification notification = new Notification.Builder(this)                .setSmallicon(androID.R.drawable.ic_dialog_info)                .setContentTitle(Title)                .setContentText(message)                .setautoCancel(true)                .setContentIntent(pendingIntent)                .build();        notification.defaults |= Notification.DEFAulT_SOUND;        notificationmanager notificationmanager =                (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);        notificationmanager.notify(1, notification);    }    @OverrIDe    public voID onPause() {        super.onPause();        //mBeaconManager.unbind(this);    }}

解决方法:

问题在于,当您的应用程序进入后台时,MainActivity将调用onPause,而该方法中的代码将调用mBeaconManager.unbind(this);.方法,可有效停止信标测距.

如果要在后台继续进行测距,最简单的方法是移动以下两行:

mBeaconManager.startRangingBeaconsInRegion(new Region("treewalker", null, null, null));mBeaconManager.addRangeNotifIEr(this);

到BeaconApp类的dIDDetermineStateForRegion方法.您还需要将dIDRangeBeaconsInRegion和showNotification方法也移到那里.

总结

以上是内存溢出为你收集整理的Android iBeacon应用程序无法在后台运行全部内容,希望文章能够帮你解决Android iBeacon应用程序无法在后台运行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1095447.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存