CONNECTIVITY_CHANGE在Android N的目标中已弃用 [英] CONNECTIVITY_CHANGE deprecated in target of Android N

查看:430
本文介绍了CONNECTIVITY_CHANGE在Android N的目标中已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到广播接收器声明已过时的警告。

I am getting warning of deprecated declaration of Broadcast Receiver.

<!-- NETWORK RECEIVER... -->
<receiver android:name=".utils.NetworkUtils" >
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
    </intent-filter>
</receiver>

警告:


声明N.及更高版本的应用已弃用android.net.conn.CONNECTIVITY_CHANGE
的广播接收器。通常,应用程序
不应依赖此广播,而应使用JobScheduler或
GCMNetworkManager。

Declaring a broadcastreceiver for android.net.conn.CONNECTIVITY_CHANGE is deprecated for apps targeting N and higher. In general, apps should not rely on this broadcast and instead use JobScheduler or GCMNetworkManager.

还有其他不使用不推荐使用的方法的方法吗?

Is there any other way to use it without deprecated methods?

推荐答案

我遇到了同样的问题,我做了类似的事情。

I had the same problem, i did something like that. It worked for me, i hope it helps.

public class NewActivity extends AppCompatActivity {
    final static String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
    IntentFilter intentFilter;
    MyReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);

        intentFilter = new IntentFilter();
        intentFilter.addAction(CONNECTIVITY_ACTION);
        receiver = new MyReceiver();

        if(checkForInternet()){
            loadData();
        }else{
            updateUI();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    // Self explanatory method
    public boolean checkForInternet() {
        ConnectivityManager cm =
                (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        return activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();
    }

    void loadData(){
        // do sth
    }

    void updateUI(){
        // No internet connection, update the ui and warn the user
    }


    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            String actionOfIntent = intent.getAction();
            boolean isConnected = checkForInternet();
            if(actionOfIntent.equals(CONNECTIVITY_ACTION)){
                if(isConnected){
                    loadData();
                }else{
                    updateUI();
                }
            }
        }
    }
}

不要在清单中添加接收方,以使其仅存在于此活动中。

Don't add the receiver in the manifest so that it only lives in this activity.

这篇关于CONNECTIVITY_CHANGE在Android N的目标中已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆