应用程序运行良好,得到了我期望的灯塔,当应用程序在后台成功检测到信标时启动应用程序。
然而,我注意到有时会发动两次。 我在下面粘贴了AndroidManifest和Application代码。
<application
android:name=".KforceApplication"
android:allowBackup="true"
android:configChanges="keyboard|keyboardHidden|orientation"
android:icon="@drawable/k_512_v2"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".LogoActivity"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
</activity>
public class KforceApplication extends MultiDexApplication implements BootstrapNotifier {
private static Context context;
private static ApplifeCycleHandler applifeCycleHandler;
private final String TAG = "KforceApplication";
private static final long BACKGROUND_SCAN_PERIOD = 1100L;
private static final long BACKGROUND_BETWEEN_SCAN_PERIOD = 1100L;
private static final long FOREGROUND_BETWEEN_SCAN_PERIOD = 30000L;
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private BeaconManager beaconManager;
private Intent intent;
private MemberDao memberDao;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
KforceApplication.applifeCycleHandler = new ApplifeCycleHandler();
registerActivityLifecycleCallbacks(applifeCycleHandler);
initBeaconService();
}
private void initBeaconService() {
beaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
Log.d(TAG, ".......init beacon");
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"));
beaconManager.setForegroundBetweenScanPeriod(FOREGROUND_BETWEEN_SCAN_PERIOD);
beaconManager.setBackgroundBetweenScanPeriod(BACKGROUND_BETWEEN_SCAN_PERIOD);
beaconManager.setBackgroundScanPeriod(BACKGROUND_SCAN_PERIOD);
Region region = new Region("all-region-beacon", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
@Override
public void onTerminate() {
super.onTerminate();
}
public static Context getAppContext() {
return context;
}
public static ApplifeCycleHandler getApplifeCycleHandler() {
return applifeCycleHandler;
}
public static boolean isRunningOnBackground() {
return applifeCycleHandler.getStartedCount() - applifeCycleHandler.getStoppedCount() == 0;
}
@Override
public void didEnterRegion(Region region) {
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();
memberDao = new MemberDao(context);
Cursor cursor = memberDao.select();
if (cursor.getCount() < 1) {
Intent it = new Intent(context, SignInActivity.class);
startActivity(it);
} else {
intent = new Intent(context, LogoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
@Override
public void didExitRegion(Region region) {
Log.d(TAG, "......... did exit");
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.d(TAG, "......... didDetermineStateForRegion exit");
Log.d(TAG, "beacon determine state : " + state);
}
}
LogoActivity是第一个没有重要意义的活动,只是一个应用徽标。 虽然意图使用MainActivity,但是应用程序有时会自动重新启动并在LogoActivity停止,即使我尝试将其设置为singleInstance,它也从不打算使用MainActivity。
[错误的应用活动顺序]
LogoActivity→MainActivity→LogoActivity(再次启动)→......停止
请问我应该如何防止这种情况。
public class LogoActivity extends AppCompatActivity{
private final String TAG = "LogoActivity";
private BluetoothAdapter mBluetoothAdapter = null;
private int LOCATION_CODE = 5646;
private MemberInfoBean bean;
private SigninParam param;
private long member_id;
private SignApi api;
private TokenParam tokenParam;
private Cursor cursor;
private MemberDao memberDao;
private MemberInfoBean infoBean;
private String accessToken, refreshToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(biz.kingdo.k_force.R.layout.activity_logo);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
askPermission();
netWorkStatus();
}
private void netWorkStatus() {
ConnectivityManager cManager = (ConnectivityManager) getSystemService(LogoActivity.CONNECTIVITY_SERVICE);
NetworkInfo info = cManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
Log.d(TAG, "..... user network normal");
doRefreshToken();
} else {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
Intent it = new Intent(LogoActivity.this, SignInActivity.class);
startActivity(it);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
private void doRefreshToken() {
memberDao = new MemberDao(this);
tokenParam = new TokenParam();
cursor = memberDao.select();
while (cursor.moveToNext()) {
api = new SignApi(this);
tokenParam.access_token = cursor.getString(15);
tokenParam.refresh_token = cursor.getString(16);
Log.d(TAG,"tokenParam.access_token...." + tokenParam.access_token);
Log.d(TAG,"tokenParam.refresh_token...." + tokenParam.refresh_token);
api.refreshToken(tokenParam, new Response.Listener<SigninResult>() {
@Override
public void onResponse(SigninResult response) {
GlobalParameter.getInstance().signinResult = response;
accessToken = response.data.access_token;
refreshToken = response.data.refresh_token;
member_id = response.data.member_id;
boolean isNew =false;
infoBean = memberDao.findOne(member_id);
if(infoBean == null){
isNew = true;
infoBean = new MemberInfoBean();
infoBean.setMember_id(member_id);
}
infoBean.setAccessToken(accessToken);
infoBean.setRefreshToken(refreshToken);
infoBean.setMember_id(member_id);
if (isNew) {
memberDao.insertMemberInfo(infoBean);
} else {
memberDao.updateData(infoBean);
}
Intent it = new Intent(LogoActivity.this, MainActivity.class);
Log.d(TAG, ".... prepare to Main");
it.putExtra("member_id", member_id);
startActivity(it);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Intent it = new Intent(LogoActivity.this, SignInActivity.class);
startActivity(it);
}
});
}
if(cursor.getCount()<1){
askPermission();
Log.d(TAG,".... cursor.getcount..." + cursor.getCount());
Intent it = new Intent(LogoActivity.this, SignInActivity.class);
startActivity(it);
}
}
*/
private void askPermission() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_CODE);
Log.d(TAG, "not permitted , and request");
} else {
Log.d(TAG, "permitted , no request");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
// doRefreshToken();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.d(TAG,"... got request permission ....");
if (requestCode == LOCATION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, ".....permitted");
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
// doRefreshToken();
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent it = new Intent(LogoActivity.this, SignInActivity.class);
startActivity(it);
}
}).start();
}
}
}
}