Skip to content

Most visited

Recently visited

navigation
NetworkConnect / src / com.example.android.networkconnect /

MainActivity.java

1
/*
2
 * Copyright (C) 2016 The Android Open Source Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
package com.example.android.networkconnect;
18
 
19
import android.content.Context;
20
import android.net.ConnectivityManager;
21
import android.net.NetworkInfo;
22
import android.os.Bundle;
23
import android.support.v4.app.FragmentActivity;
24
import android.util.Log;
25
import android.view.Menu;
26
import android.view.MenuItem;
27
import android.widget.TextView;
28
 
29
/**
30
 * Sample Activity demonstrating how to connect to the network and fetch raw
31
 * HTML. It uses a Fragment that encapsulates the network operations on an AsyncTask.
32
 *
33
 * This sample uses a TextView to display output.
34
 */
35
public class MainActivity extends FragmentActivity implements DownloadCallback {
36
 
37
    // Reference to the TextView showing fetched data, so we can clear it with a button
38
    // as necessary.
39
    private TextView mDataText;
40
 
41
    // Keep a reference to the NetworkFragment which owns the AsyncTask object
42
    // that is used to execute network ops.
43
    private NetworkFragment mNetworkFragment;
44
 
45
    // Boolean telling us whether a download is in progress, so we don't trigger overlapping
46
    // downloads with consecutive button clicks.
47
    private boolean mDownloading = false;
48
 
49
    @Override
50
    protected void onCreate(Bundle savedInstanceState) {
51
        super.onCreate(savedInstanceState);
52
        setContentView(R.layout.sample_main);
53
        mDataText = (TextView) findViewById(R.id.data_text);
54
        mNetworkFragment = NetworkFragment.getInstance(getSupportFragmentManager(), "https://www.google.com");
55
    }
56
 
57
    @Override
58
    public boolean onCreateOptionsMenu(Menu menu) {
59
        getMenuInflater().inflate(R.menu.main, menu);
60
        return true;
61
    }
62
 
63
    @Override
64
    public boolean onOptionsItemSelected(MenuItem item) {
65
        switch (item.getItemId()) {
66
            // When the user clicks FETCH, fetch the first 500 characters of
67
            // raw HTML from www.google.com.
68
            case R.id.fetch_action:
69
                startDownload();
70
                return true;
71
            // Clear the text and cancel download.
72
            case R.id.clear_action:
73
                finishDownloading();
74
                mDataText.setText("");
75
                return true;
76
        }
77
        return false;
78
    }
79
 
80
    private void startDownload() {
81
        if (!mDownloading && mNetworkFragment != null) {
82
            // Execute the async download.
83
            mNetworkFragment.startDownload();
84
            mDownloading = true;
85
        }
86
    }
87
 
88
    @Override
89
    public void updateFromDownload(String result) {
90
        if (result != null) {
91
            mDataText.setText(result);
92
        } else {
93
            mDataText.setText(getString(R.string.connection_error));
94
        }
95
    }
96
 
97
    @Override
98
    public NetworkInfo getActiveNetworkInfo() {
99
        ConnectivityManager connectivityManager =
100
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
101
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
102
        return networkInfo;
103
    }
104
 
105
    @Override
106
    public void finishDownloading() {
107
        mDownloading = false;
108
        if (mNetworkFragment != null) {
109
            mNetworkFragment.cancelDownload();
110
        }
111
    }
112
 
113
    @Override
114
    public void onProgressUpdate(int progressCode, int percentComplete) {
115
        switch(progressCode) {
116
            // You can add UI behavior for progress updates here.
117
            case Progress.ERROR:
118
                break;
119
            case Progress.CONNECT_SUCCESS:
120
                break;
121
            case Progress.GET_INPUT_STREAM_SUCCESS:
122
                break;
123
            case Progress.PROCESS_INPUT_STREAM_IN_PROGRESS:
124
                mDataText.setText("" + percentComplete + "%");
125
                break;
126
            case Progress.PROCESS_INPUT_STREAM_SUCCESS:
127
                break;
128
        }
129
    }
130
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.