2017-02-16 34 views
1

我正在为Android设置地理栅栏应用程序。在谷歌的example的帮助下,并从IntentServiceBroadcastReceiver重构here我现在有一个应用程序,如果应用程序在后台,按预期工作。我没有管理的是我如何更新我的应用程序,例如一个模型,如果应用程序被杀害?我的GeofenceReceiver在应用程序被杀时触发/调用,但我的BroadcastReceiver消息未在我的MainActivity中收到。如何从IntentReceiver更新关闭/杀死的应用程序?

在我GeofenceReceiver我做这样的事,如果我得到一个地理围栏的过渡:正如我已经说过广播接收

Intent broadcastIntent = new Intent(ACTION); 
     broadcastIntent.putExtra("resultCode", Activity.RESULT_OK); 
     broadcastIntent.putExtra(Constants.BUNDLE_KEY, bundle); 
     LocalBroadcastManager.getInstance(context).sendBroadcast(broadcastIntent); 

1:

Intent notificationIntent = new Intent(context, MainActivity.class); 
    notificationIntent.putStringArrayListExtra(Constants.TRIGGERING_GEOFENCE_IDS_KEY, triggeringIDs); 

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(MainActivity.class); 
    stackBuilder.addNextIntent(notificationIntent); 

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
    builder.setSmallIcon(R.mipmap.ic_launcher) 
      .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) 
      .setColor(Color.RED) 
      .setContentTitle(notificationDetails) 
      .setContentText(context.getString(R.string.geofence_transition_notification_text)) 
      .setPriority(NotificationCompat.PRIORITY_HIGH) 
      .setContentIntent(resultPendingIntent); 
    builder.setAutoCancel(true); 

    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(0, builder.build()); 

2在我的MainActivity中,如果应用程序在后台。如果我杀了App,我的MainActivity没有收到广播。我检查了与终端和logcat。

任何想法?

+0

那么你的意思是你在你的BroadcastReceiver中收到了一个广播,但是你没有在你的主要活动中得到它? – cafebabe1991

+0

@ cafebabe1991基本上是的。即使应用程序被杀死,我的GeofenceReceiver仍然会被要求进行地理栅栏转换,但之后我无法向我的MainActivity广播某些内容。 – wman

回答

0

由于应用程序已被杀死,因此必须重新启动。应用程序可以使用在您的BroadcastReceiver类中传递的上下文来启动。查看下面的代码片段,将您的活动发送给geoFence数据。

@Override 
public void onReceive(Context context, Intent intent) { 
    Intent i = new Intent(); 
    i.setClassName("com.test", "com.test.MainActivity"); 
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    i.putExtra("data", geoData); 
    context.startActivity(i) 
} 
+0

我试过用Intent broadcastIntent = new Intent(context,MainActivity.class); broadcastIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); broadcastIntent.putExtra(“resultCode”,Activity.RESULT_OK); broadcastIntent.putExtra(Constants.BUNDLE_KEY,bundle); context.startActivity(broadcastIntent);但这意味着我的应用程序将前景。我不想那样。我只想更新模型以刷新lastPosition Geofence。 – wman

+0

然后您必须开始您的服务,您的服务将反过来更新数据。 – cafebabe1991