简体   繁体   English

如何“更新”我的活动?

[英]How can I “update” my activity?

Hi guys I'm new on android and maybe this question is so stupid for you. 嗨,大家好,我是android的新手,也许这个问题对您来说太愚蠢了。 I'm developing my first android app and I'm doing a method that check the connection service status. 我正在开发我的第一个android应用程序,并且正在做一种检查连接服务状态的方法。 All is working, but what I wanna do is, when the connection status switch from "Not connected" to "Connected" I wanna reload or update or recall my main activity in order to update my data the app download from my server. 一切正常,但是我想做的是,当连接状态从“未连接”切换到“已连接”时,我想重新加载或更新或调用我的主要活动,以便更新从服务器下载的应用程序中的数据。 Here is my code: 这是我的代码:

Java class for the BroadcastReceiver only: 仅适用于BroadcastReceiver的Java类:

public class NetworkStateReceiver extends BroadcastReceiver {



    private static final String TAG = "NetworkStateReceiver";
    @Override

    public void onReceive(final Context context, final Intent intent) {

        Log.d(TAG, "Network connectivity change");

        if (intent.getExtras() != null) {
            final ConnectivityManager connectivityManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

            if (ni != null && ni.isConnectedOrConnecting()) {
                Log.i(TAG, "Network " + ni.getTypeName() + " connected");

            } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
                Log.d(TAG, "There's no network connectivity");
            }
        }
    }
}

Snippet in the "application" tag in manifest: 清单中“应用程序”标签中的代码段:

<receiver android:name="com.example.findmyclients.NetworkStateReceiver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
    </intent-filter>
</receiver>

With the debug I've already seen that all is working, the log is written by "onReceive" method in java class but if I try to recall my activity my app crash :( What can I do to solve ?? Thank you so much you all. 通过调试,我已经看到一切正常,日志是通过java类中的“ onReceive”方法编写的,但是如果我尝试回想我的活动,则我的应用崩溃了:(我该怎么办才能解决??非常感谢你们。

If I understand you correct, you need to look at Activity Lifecycle: AndroidDevDocs and call to that method where you update or reload your data. 如果我理解的正确,则需要查看“活动生命周期: AndroidDevDocs”并调用该方法,以在其中更新或重新加载数据。 For example you call for onStart() again and again for reload data in TextView or something like that. 例如,您一次又一次调用onStart()以在TextView或类似的东西中重新加载数据。 But this depends on your app: maybe you should create a function where you "update" all fields of your app. 但这取决于您的应用程序:也许您应该创建一个函数来“更新”应用程序的所有字段。 every time the status changes to "connected" 每次状态更改为“已连接”

If I were you I would do the following. 如果我是你,我将执行以下操作。
I would make this class(your network class) accessible via Singleton and I would create a pool of activities to be notified in case there is a connection. 我将使该类(您的网络类)可以通过Singleton访问,并且我将创建一个活动池,以便在有连接的情况下得到通知。 Every activity that wants to listen for this can register to this class of yours and when there is a network change you notify the activities(it can be 1 or 2...100). 每个想要听的活动都可以注册到您的此类中,并且当网络发生变化时,您会通知活动(可以是1或2 ... 100)。

All you have to do is to create an interface like that: 您要做的就是创建一个像这样的接口:

public class NetworkStateReceiver extends BroadcastReceiver {
private static NetworkStateReceiver instance = null;
private List<INetworkNotify> list = new ArrayList<INetworkNotify>();
protected NetworkStateReceiver(){}
public static NetworkStateReceiver getInstance()
{
  //standard code for initializing the instance;
}
public void registerListener(INetworkNotify listener)
{
   //add it to the list pool.
}

public void deregisterListener(INetworkNotify listener)
{
   //remove it from the pool.
} 

private static final String TAG = "NetworkStateReceiver";
@Override

public void onReceive(final Context context, final Intent intent) {

    Log.d(TAG, "Network connectivity change");

    if (intent.getExtras() != null) {
        final ConnectivityManager connectivityManager =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo ni = connectivityManager.getActiveNetworkInfo();

        if (ni != null && ni.isConnectedOrConnecting()) {
            Log.i(TAG, "Network " + ni.getTypeName() + " connected");

        } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
            Log.d(TAG, "There's no network connectivity");
        }
        notifyListeners("Something, anything.");
    }
}

private void notifyListeners(String msg)
{
   //e.g
   for(INetworkListener listener:list)
   {
       listener.onUpdate(msg);
   }
   //notify the listeners;
}    

Below you can see the INetworkNotify interface 在下面您可以看到INetworkNotify接口

Interface INetworkNotify
{
   void onUpdate(String msg);
}

In the activity all you have to do is to implement the listener. 在活动中,您要做的就是实现侦听器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据更新为活动? - How can I update my data into my activity? Android Studio Java:如果产品已经存在于我的购物车活动中,我该如何更新我的数量? - Android Studio Java: How can I update my quantity if the product already exist's in my Cart Activity? 如何在线程中更新和活动的引用? - How can i update the reference of and activity in a thread? Android - 当收到推送通知 FCM 时,如何更新我的活动中的数据或 UI? - Android - How can I update data or UI in my Activity when Push Notification FCM is received? 当我的方法不在活动中时,如何将其传递给活动? - How can I pass my method an activity when it is not in an activity? 当我在其他应用程序上时,如何在活动中更新我的位置 - How to update my location on activity when I am on other application 单击通知中的按钮时如何更新活动 UI? - How to update activity UI when i click a button in my notification? 如何从后台服务更新Android活动中的信息 - How can I update information in an Android Activity from a background Service 如何从Retrofit onResponse()更新活动/片段UI? - How can I update Activity/Fragment UI from Retrofit onResponse()? 如果TextView包含在活动中,如何更新TextView的文本? - How can I update the text of a TextView if the TextView is in an included activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM