summaryrefslogtreecommitdiffstats
path: root/Android/02-Intents/IntentsLab/src/course/labs/intentslab/ActivityLoaderActivity.java
blob: abd912a2fae7ca4a285c493cb283d2115e9d5835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package course.labs.intentslab;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ActivityLoaderActivity extends Activity {

    static private final int GET_TEXT_REQUEST_CODE = 1;
    static private final String URL = "http://www.google.com";
    static private final String TAG = "Lab-Intents";

    // For use with app chooser
    static private final String CHOOSER_TEXT = "Load " + URL + " with:";

    // TextView that displays user-entered text from ExplicitlyLoadedActivity runs
    private TextView mUserTextView;

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

        // Get reference to the textView
        mUserTextView = (TextView) findViewById(R.id.textView1);

        // Declare and setup Explicit Activation button
        Button explicitActivationButton = (Button) findViewById(R.id.explicit_activation_button);
        explicitActivationButton.setOnClickListener(new OnClickListener() {

            // Call startExplicitActivation() when pressed
            @Override
            public void onClick(View v) {
                startExplicitActivation();
            }
        });

        // Declare and setup Implicit Activation button
        Button implicitActivationButton = (Button) findViewById(R.id.implicit_activation_button);
        implicitActivationButton.setOnClickListener(new OnClickListener() {

            // Call startImplicitActivation() when pressed
            @Override
            public void onClick(View v) {
                startImplicitActivation();
            }
        });
    }

    // Start the ExplicitlyLoadedActivity
    private void startExplicitActivation() {
        Log.i(TAG,"Entered startExplicitActivation()");
        Intent intent = new Intent(ActivityLoaderActivity.this, ExplicitlyLoadedActivity.class);
        startActivityForResult(intent, GET_TEXT_REQUEST_CODE);
    }

    // Start a Browser Activity to view a web page or its URL
    private void startImplicitActivation() {
        Log.i(TAG, "Entered startImplicitActivation()");
        Uri webpage = Uri.parse("http://asynk.ch");
        Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
        Intent chooserIntent = Intent.createChooser(webIntent, "Choose damn it!");
        Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
        if (chooserIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(chooserIntent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i(TAG, "Entered onActivityResult()");
        if (requestCode == GET_TEXT_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                mUserTextView.setText(data.getStringExtra("data"));
            }
        }
    }
}