来自 Google 的新 Geofencing API 在应用停止时不会触发任何事件。我尝试使用 PendingIntent.getBroadcast()
和 PendingIntent.getService()
但只能在打开应用程序时获取转换事件。
我关注了this来自代码实验室的教程,但修改了代码以使用新的 GeofencingClient
更新
这就是我创建待定 Intent 的方式:
private PendingIntent getGeofencePendingIntent() {
if (mGeofencePendingIntent != null) {
Log.d(TAG, "Pending intent is already there");
return mGeofencePendingIntent;
}
Log.d(TAG, "Creating a new pending intent");
// In case I'm using Service, the second parameter will be GeoIntentService.class
Intent mIntent = new Intent(mContext, GeoReceiver.class);
// In case I'm using Service, the method will be getService(...)
mGeofencePendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
return mGeofencePendingIntent;
}
我的 GeoReceiver 的 onReceive
方法中的代码。在 onHandleIntent
方法中使用 Services 时,代码在某种程度上是相似的
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive");
if (intent == null) {
Log.e(TAG, "Intent is null");
return;
}
final String action = intent.getAction();
if (ACTION_ADD_GEOFENCE.equals(action)) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errMsg = GeofenceExceptionMessages.getErrorString(context, geofencingEvent.getErrorCode());
Log.e(TAG, "onReceive Error: " + errMsg);
return;
}
int geofenceTransition = geofencingEvent.getGeofenceTransition();
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();
NotificationHelper helper = new NotificationHelper();
String geofenceTransitionDetails = helper.getGeofenceTransitionDetails(
context,
geofenceTransition,
triggeringGeofences
);
helper.sendNotificaiton(context, geofenceTransitionDetails);
Log.i(TAG, "onReceive: " + geofenceTransitionDetails);
}
} else {
Log.d(TAG, "Diffirent Action: " + action);
}
}
最佳答案
地理围栏不起作用,因为有些设备默认只允许某些白名单应用程序的后台服务。如果您的应用也必须像那样工作意味着您必须从设置中启用AutoStart,下面的代码将帮助您让用户为您的应用启用自动启动。如果AutoStart是启用后,您的服务将在后台正常运行。
为了更好的手机性能,一些公司会停止应用程序的所有后台服务(“一些白名单应用程序将只允许在后台进行服务,例如:WhatsApp,Google Apps和公认的应用程序。” )。所以如果我们的应用程序也必须像那样工作,我们必须启用自动启动服务。
自动启动:
当 Android 系统启动时,它会发出启动完成事件。 Android 应用程序可以监听和捕获此事件以采取特定操作,例如自动启动 Activity 或服务。
截至目前,无法确定是否启用了AutoStart。 因此,您可以将它们重定向到设置并告诉用户启用它。我正在为最常见的电话公司提供重定向代码,这将终止后台服务。(仅向用户显示一次,使用 SharedPreference 进行管理,如我之前所说无法确定它是启用还是禁用。)
为了实现这一目标,
在AndroidManifest.xml中声明权限。将 android.permission.RECEIVE_BOOT_COMPLETED 权限添加到应用程序声明节点之前的应用程序 list 文件中:
这是必须调用的函数代码。
private void enableAutoStart() {
if (Build.BRAND.equalsIgnoreCase("xiaomi")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow AppName to always run in the background,else our services can't be accessed.")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.BRAND.equalsIgnoreCase("Letv")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow AppName to always run in the background,else our services can't be accessed.")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.letv.android.letvsafe",
"com.letv.android.letvsafe.AutobootManageActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.BRAND.equalsIgnoreCase("Honor")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow AppName to always run in the background,else our services can't be accessed.")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.huawei.systemmanager",
"com.huawei.systemmanager.optimize.process.ProtectActivity"));
startActivity(intent);
}
})
.show();
} else if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow AppName to always run in the background,else our services can't be accessed.")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
try {
Intent intent = new Intent();
intent.setClassName("com.coloros.safecenter",
"com.coloros.safecenter.permission.startup.StartupAppListActivity");
startActivity(intent);
} catch (Exception e) {
try {
Intent intent = new Intent();
intent.setClassName("com.oppo.safe",
"com.oppo.safe.permission.startup.StartupAppListActivity");
startActivity(intent);
} catch (Exception ex) {
try {
Intent intent = new Intent();
intent.setClassName("com.coloros.safecenter",
"com.coloros.safecenter.startupapp.StartupAppListActivity");
startActivity(intent);
} catch (Exception exx) {
}
}
}
}
})
.show();
} else if (Build.MANUFACTURER.contains("vivo")) {
new MaterialDialog.Builder(MainActivity.this).title("Enable AutoStart")
.content(
"Please allow AppName to always run in the background.Our app runs in background else our services can't be accesed.")
.theme(Theme.LIGHT)
.positiveText("ALLOW")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.iqoo.secure",
"com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
startActivity(intent);
} catch (Exception e) {
try {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
startActivity(intent);
} catch (Exception ex) {
try {
Intent intent = new Intent();
intent.setClassName("com.iqoo.secure",
"com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
startActivity(intent);
} catch (Exception exx) {
ex.printStackTrace();
}
}
}
}
})
.show();
}
}
关于android - 应用程序死机时的地理围栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47633032/