Bir*_*rel 5 android android-notifications intentservice android-alertdialog android-geofence
我按照这个开发人员教程,按照预期在我的应用程序中运行Geofencing.
发生地理围栏转换时会发出通知,来自IntentService:
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
...
sendNotification(geofenceTransitionDetails);
}
private void sendNotification(String notificationDetails) {
// Create an explicit content Intent that starts the main Activity.
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// Construct a task stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Add the main Activity to the task stack as the parent.
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack.
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack.
PendingIntent notificationPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Define the notification settings.
builder.setSmallIcon(R.mipmap.ic_launcher)
// In a real app, you may want to use a library like Volley
// to decode the Bitmap.
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher))
.setColor(Color.RED)
.setContentTitle(notificationDetails)
.setContentText("Return to app")
.setContentIntent(notificationPendingIntent);
// Dismiss notification once the user touches it.
builder.setAutoCancel(true);
// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}
Run Code Online (Sandbox Code Playgroud)
这是教程中的千篇一律.意图在主要活动中设置:
private PendingIntent getGeofencePendingIntent() {
// Reuse the PendingIntent if we already have it.
if (mGeofencePendingIntent != null) {
return mGeofencePendingIntent;
}
Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
// addGeofences() and removeGeofences().
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Run Code Online (Sandbox Code Playgroud)
如果应用程序处于打开状态,如何添加禁止通知的功能,而是向用户显示AlertDialog?理想情况下,我希望能够执行不同的任务,具体取决于发生地理围栏转换时用户当前所处的视图.我可以监控/拦截每个视图中的转换,还是以某种方式全局监控?
提前致谢.
一些答案是不完整的,所以这里是我正在寻找的完整解决方案.
首先,设置MyApplication类,实现ActivityLifecycleCallbacks:
public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {
private static boolean isActive;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
public static boolean isActivityVisible(){
return isActive;
}
@Override
public void onActivityResumed(Activity activity) {
isActive = true;
}
@Override
public void onActivityPaused(Activity activity) {
isActive = false;
}
... no other methods need to be used, but there are more that
... must be included for the ActivityLifecycleCallbacks
}
Run Code Online (Sandbox Code Playgroud)
请务必在清单中为此命名(仅添加名称行,默认为rest):
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hardwareAccelerated="true">
Run Code Online (Sandbox Code Playgroud)
上面所做的工作用于跟踪应用的生命周期.您可以使用它来检查您的应用当前是否在前台.
接下来是设置a BroadcastReceiver,无论您希望代码运行在哪里(如果应用程序在触发器发生时打开).在这种情况下,它在我的MainActivity:
protected BroadcastReceiver mNotificationReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
... Do whatever you want here
Toast.makeText(...).show();
}
};
Run Code Online (Sandbox Code Playgroud)
在您onCreate的同一活动中注册接收者:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
LocalBroadcastManager.getInstance(this).registerReceiver(mNotificationReceiver, new IntentFilter("some_custom_id"));
}
Run Code Online (Sandbox Code Playgroud)
别忘了取消注册:
@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mNotificationReceiver);
super.onDestroy();
}
Run Code Online (Sandbox Code Playgroud)
当接收到广播时,执行接收器内的代码.
现在,检查应用程序是否在前台,如果是,则发送广播.里面的IntentService:
@Override
protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = getErrorString(this,
geofencingEvent.getErrorCode());
return;
}
int geofenceTransition = geofencingEvent.getGeofenceTransition();
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
...
if(MyApplication.isActivityVisible()){
Intent intnt = new Intent("some_custom_id");
intnt.putExtra("message", geofenceTransitionDetails);
LocalBroadcastManager.getInstance(this).sendBroadcast(intnt);
}else{
sendNotification(geofenceTransitionDetails);
}
} else {
// Log the error.
}
}
Run Code Online (Sandbox Code Playgroud)
重要的是最后一个嵌套的if语句:
if(MyApplication.isActivityVisible()){
Intent intnt = new Intent("some_custom_id");
intnt.putExtra("message", geofenceTransitionDetails);
LocalBroadcastManager.getInstance(this).sendBroadcast(intnt);
}else{
sendNotification(geofenceTransitionDetails);
}
Run Code Online (Sandbox Code Playgroud)
使用MyApplication.isActivityVisible()上面定义的应用程序检查应用程序是否位于前台,然后发送通知或发送广播.只需确保您的意图代码(即"some_custom_id")与您的发件人和收件人匹配.
这就是它.如果应用程序位于前台(特别是MainActivity),我会执行一些代码.如果应用程序不在前台,我会发送通知.