Hello,
I am working on an application which gets relaunched when a certain beacons comes in range. The detection and launching bit is working perfectly. However, the app gets relaunched even if the app is already launched. I read about SingleInstance and I have implemented it on my launcher activity, but the app still get relaunched.
Following is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nimbledevices.guideviewer">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/pai"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".GuideViewer"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".Login_Activity" />
<activity android:name=".HomeScreen" />
<activity android:name=".Request_Activity" />
<activity
android:name=".Splash"
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=".Register" />
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".Arabic_Login" />
<activity android:name=".Register_Arabic" />
<activity android:name=".HomeScreen_Arab" />
<activity android:name=".Request_Activity_arab"></activity>
</application>
</manifest>
and MyApplication.java : responsible for launching an app when Beacon is detected:
package com.nimbledevices.guideviewer;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import com.estimote.sdk.Beacon;
import com.estimote.sdk.BeaconManager;
import com.estimote.sdk.Region;
import java.util.List;
import java.util.UUID;
/**
* Created by DELL on 28-Jun-16.
*/
public class MyApplication extends Application {
private BeaconManager beaconManager;
@Override
public void onCreate() {
super.onCreate();
beaconManager = new BeaconManager(getApplicationContext());
beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
@Override
public void onEnteredRegion(Region region, List<Beacon> list) {
/* showNotification(
"Upper Notify",
"Lower Notify"); */
Intent intent = new Intent(MyApplication.this, Splash.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
@Override
public void onExitedRegion(Region region) {
// could add an "exit" notification too if you want (-:
}
});
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
@Override
public void onServiceReady() {
beaconManager.startMonitoring(new Region(
"monitored region",
UUID.fromString("f7826da6-4fa2-4e98-8024-bc5b71e0893e"),
8581, 5239));
}
});
}
public void showNotification(String title, String message) {
Intent notifyIntent = new Intent(this, Login_Activity.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);
}
}