0

私は 2 つのビーコンを持っており、この最初のビーコンが 1 メートル以内に来ると最初のアクティビティを起動し、2 番目のビーコンが 1 メートル以内に来ると 2 番目のアクティビティを起動しました。

今私が欲しいのは、最初のアクティビティが一度起動された場合、アプリケーションは最初のアクティビティを再度起動するべきではありませんが、問題は、最初のアクティビティから戻るボタンを押すと、最初のビーコンが 1 メートルにあり、同じ面に直面している場合、最初のアクティビティが再び起動されることです2 番目のビーコンの問題

私のマニフェストファイルコードは次のとおりです。

<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.FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
        <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

      <activity
            android:launchMode="singleInstance"  
            android:name="org.altbeacon.beaconreference.SecondActivity"
            android:label="@string/app_name" >
            <intent-filter>
        <action android:name="org.altbeacon.beaconreference.SECONDACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </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;
    private static final String ESTIMOTE_PROXIMITY_UUID = "XYZ";
    @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);        
        mBeaconManager.setBackgroundBetweenScanPeriod(30000l);

    }

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

    @Override
    public void didEnterRegion(Region arg0) {

        //mRegionBootstrap.disable();
        try {
            //mBeaconManager.startRangingBeaconsInRegion(region);
            mBeaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);

            mBeaconManager.setRangeNotifier(this);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }               
        //Toast.makeText(this, "didEnterRegion", Toast.LENGTH_LONG).show();
        //Toast.makeText(this, "didEnterRegion", Toast.LENGTH_LONG).show();
        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.  

    }

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

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

        //mRegionBootstrap.disable();
        // TODO Auto-generated method stub

        if (beacons.size() > 0) {        
            for (Beacon beacon: beacons) {          
                String beconid=beacon.toString();               

                if(beacon.getDistance()<1 && beconid.equals("id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 20155 id3: 28577"))
                {
                    Log.d(TAG, "within 1 minute call");
                    Intent intent = new Intent(this, FirstActivity.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);                     
                } 

                if(beacon.getDistance()<1 && beconid.equals("id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 8128 id3: 59794"))
                {
                    Log.d(TAG, "within 1 minute call");
                    Intent intent = new Intent(this, SecondActivity.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);                     
                } 

            }

        }
    }        
}
4

3 に答える 3

0

ビーコンを見つけたら検索を停止し、特定の遅延の後に検索を再開するか、最後に見つかったビーコンの ID を保存して、しばらくの間または範囲外になるまでそのビーコンを無視することができます。

次のような方法で、ビーコンを 60 秒間無視できます。

private String lastBeaconId = "";
private long lastTimeFound = 0;

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

    //mRegionBootstrap.disable();
    // TODO Auto-generated method stub




    if (beacons.size() > 0) {        
        for (Beacon beacon: beacons) {          
            String beconid=beacon.toString();               

            if (beconid.equals(lastBeaconId)){

                long diff = new Date().getTime();
                //ignore this beacon if it was found 60 seconds ago
                if(diff - lastTimeFound >= 60*1000){ 
                    continue;
                }

            }


            if(beacon.getDistance()<1 && beconid.equals("id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 20155 id3: 28577"))
            {
                //save this beacon
                lastTimeFound = new Date().getTime();
                lastBeaconId = beconid;

                Log.d(TAG, "within 1 minute call");
                Intent intent = new Intent(this, FirstActivity.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);                     
            } 

            if(beacon.getDistance()<1 && beconid.equals("id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 8128 id3: 59794"))
            {
                //save this beacon
                lastTimeFound = new Date().getTime();
                lastBeaconId = beconid;

                Log.d(TAG, "within 1 minute call");
                Intent intent = new Intent(this, SecondActivity.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);                     
            } 

        }

    }
} 
于 2015-04-09T10:18:41.180 に答える
0

これはそれほど複雑な問題ではないと思いますが、

2 つのステップで解決でき ます

  1. 一貫性のあるメモリに設定するにはSharedPreferences

アクティビティを始める前に1

SharedPreferences sp = context.getSharedPreferences("MyPrefs", MODE_PRIVATE);
if(sp.getBoolean("activity1",false)==false){
    sp.edit().putBoolean ("activity1", true).commit();
    startActivity(activity1);
}
  1. コンシステント メモリをリセットするには

onCreateこれをあなたのmainActivityに追加してください

@Override
protected void onCreate() {
    SharedPreferences sp = context.getSharedPreferences("MyPrefs", MODE_PRIVATE);
    sp.edit().putBoolean ("activity1", false).commit();
    super.onResume();
}

SharedPreferences sp = context.getSharedPreferences("MyPrefs", MODE_PRIVATE);必要に応じてグローバル変数として作成できます

于 2015-04-09T13:46:07.063 に答える
0

これを解決する簡単な方法は、次のようにブール値のフラグを保存することです。

private Boolean firstActivityLaunched = false;
private Boolean secondActivityLaunched = false;

次に、これらを使用するようにコードを変更します。

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {

    //mRegionBootstrap.disable();
    // TODO Auto-generated method stub

    if (beacons.size() > 0) {        
        for (Beacon beacon: beacons) {          
            String beconid=beacon.toString();               

            if(beacon.getDistance()<1 && beconid.equals("id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 20155 id3: 28577") && !firstActivityLaunched)
            {
                firstActivityLaunched = true;
                Log.d(TAG, "within 1 minute call");
                Intent intent = new Intent(this, FirstActivity.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);                     
            } 

            if(beacon.getDistance()<1 && beconid.equals("id1: b9407f30-f5f8-466e-aff9-25556b57fe6d id2: 8128 id3: 59794") && !secondActivityLaunched)
            {
                secondActivityLaunched = true;
                Log.d(TAG, "within 1 minute call");
                Intent intent = new Intent(this, SecondActivity.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);                     
            } 

        }

    }
}  
于 2015-04-09T13:23:39.017 に答える