代码之家  ›  专栏  ›  技术社区  ›  Parad0X

应用程序未运行时BootstrapNotifier不启动活动

  •  0
  • Parad0X  · 技术社区  · 8 年前

    Alt Library 我想用BootstrapNotifier。 我能探测到信号灯(Kontakt.io公司)当应用程序在前台和后台时。但需要启动创建通知,当手机靠近我的信标时,应用程序不在前台和后台。

    我试着下载 Reference application 当应用程序在前台和后台时,可以扫描信标,但是 BootstrapNotifier

    我在android5.1和8.1上尝试了这个参考应用程序,但是没有工作,仍然只检测前景/背景信标。

    在参考应用程序中,我只更改信标布局Kontakt.io公司“m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25”

    我试过艾迪斯通和伊比康,但都是一样的。

    谢谢你的建议

        implementation 'org.altbeacon:android-beacon-library:2.15'
    

    public class MyApplicationName extends Application implements BootstrapNotifier {
    private static final String TAG = "MyApplicationName";
    private RegionBootstrap regionBootstrap;
    private BackgroundPowerSaver backgroundPowerSaver;
    
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        BeaconManager beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(getApplicationContext());
    
        // 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.  To find the proper
        // layout expression for other beacon types, do a web search for "setBeaconLayout"
        // including the quotes.
        //
        beaconManager.getBeaconParsers().clear();
        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"));
    
    
        // Uncomment the code below to use a foreground service to scan for beacons. This unlocks
        // the ability to continually scan for long periods of time in the background on Andorid 8+
        // in exchange for showing an icon at the top of the screen and a always-on notification to
        // communicate to users that your app is using resources in the background.
        //
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("Scanning for Beacons");
        Intent intent = new Intent(this, MonitoringActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT
        );
        builder.setContentIntent(pendingIntent);
        beaconManager.enableForegroundServiceScanning(builder.build(), 456);
    
        // For the above foreground scanning service to be useful, you need to disable
        // JobScheduler-based scans (used on Android 8+) and set a fast background scan
        // cycle that would otherwise be disallowed by the operating system.
        //
        beaconManager.setEnableScheduledScanJobs(false);
        beaconManager.setBackgroundBetweenScanPeriod(0);
        beaconManager.setBackgroundScanPeriod(1100);
    
        Log.d(TAG, "setting up background monitoring for beacons and power saving");
        // wake up the app when a beacon is seen
        Region region = new Region("backgroundRegion",
                Identifier.parse("2f234454-cf6d-4a0f-adf2-f4911ba9ffa6"), null, null);
        regionBootstrap = new RegionBootstrap(this, region);
    
        // simply constructing this class and holding a reference to it in your custom Application
        // class will automatically cause the BeaconLibrary to save battery whenever the application
        // is not visible.  This reduces bluetooth power usage by about 60%
        backgroundPowerSaver = new BackgroundPowerSaver(getApplicationContext());
    }
    
    
    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        // Don't care
    }
    
    @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, RangingActivity.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);
    }
    
    @Override
    public void didExitRegion(Region arg0) {
        // Don't care
    }
    

    /////////////// 显示

     <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.par.servicebeacontest">
        <uses-permission android:name="android.permission.BLUETOOTH" />
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />    <application
            android:name=".MyApplicationName"
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <!-- Note:  the singleInstance below is important to keep two copies of your activity from getting launched on automatic startup -->
            <activity
                android:name=".RangingActivity"
                android:launchMode="singleInstance">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <activity android:name=".MyActivity" />
            <activity android:name=".test">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    0 回复  |  直到 8 年前