android - 从广播接收器更改 Activity 中的 TextView
问题描述
我有一个活动“HauptActivity”,其中有一个名为“status”的 textView。然后我有一个 GeofenceBroadcastReceiver。每次用户走进或走出地理围栏时,我都想更改 HauptActivity 中的 textView。我怎么意识到这一点。我无法像接收者是普通班级那样处理这个问题。必须有另一种方式。你能以某种方式帮助我吗?
这是我的地理围栏广播接收器:
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "GeofenceBroadcastReceiv";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// Toast.makeText(context, "Geofence triggered...", Toast.LENGTH_SHORT).show();
NotificationHelper notificationHelper = new NotificationHelper(context);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()){
Log.d(TAG, "onReceive: Error receiving geofence event...");
return;
}
List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
for (Geofence geofence: geofenceList) {
Log.d(TAG, "onReceive: " + geofence.getRequestId());
}
// Location location = geofencingEvent.getTriggeringLocation();
int transitionType = geofencingEvent.getGeofenceTransition();
// Here are the cases when I want to change the TextView in HauptActivity
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER: // change textView here
notificationHelper.sendHighPriorityNotification("Vorsicht: Kamera", "Du näherst dich einer Überwachungskamera", HauptActivity.class);
break;
/*case Geofence.GEOFENCE_TRANSITION_DWELL:
notificationHelper.sendHighPriorityNotification("Vorsicht: Kamera", "Du bist in der Nähe einer Überwachungskamera", HauptActivity.class);
break;*/
case Geofence.GEOFENCE_TRANSITION_EXIT: // change textView here
notificationHelper.sendHighPriorityNotification("Gefahr vorüber", "In der Nähe sind keine Kameras", HauptActivity.class);
break;
}
}
}
现在我正在智能手机上发送推送通知。但同时 HauptActivity 中的 textView 应该变为:“你在地理围栏中”
解决方案
推荐阅读
- scala - 获取模块符号,假设我有模块类,scala 宏
- php - MySQL 行限制和 AUTO_INCREMENT
- bitbucket - 如何使用 bitbucket 静态站点处理“不安全评估”
- javascript - R Shiny Datatable 中的嵌入式输入 - javascript 问题
- python - TypeError:“PosixPath”类型的参数不是 iterabl
- slice - 切片索引的惰性如何影响数组/列表的切片?[乐]
- c++ - C++:只允许数字作为输入
- angular - 部署后未显示数据表 [Netlify]
- date - 使用 SQLLoader 将数据字段的 $ char 替换为零
- search - 如何检查邮件中的哪个附件与搜索匹配