首页 > 解决方案 > 从广播接收器更改 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 应该变为:“你在地理围栏中”

标签: androidclasstextviewbroadcastreceiverreceiver

解决方案


推荐阅读