Advertisement
ridjis

ActivityLoaderActivity

Dec 23rd, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. package course.labs.intentslab;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.net.Uri;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12.  
  13. public class ActivityLoaderActivity extends Activity {
  14.    
  15.     static private final int REQUEST_CODE = 1;
  16.     static private final String URL = "http://www.google.com";
  17.     static private final String TAG = "Lab-Intents";
  18.    
  19.     // For use with app chooser
  20.     static private final String CHOOSER_TEXT = "Load " + URL + " with:";
  21.    
  22.     // TextView that displays user-entered text from ExplicitlyLoadedActivity runs
  23.     private TextView mUserTextView;
  24.  
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_loader_activity);
  29.        
  30.         // Get reference to the textView
  31.         mUserTextView = (TextView) findViewById(R.id.textView1);
  32.  
  33.         // Declare and setup Explicit Activation button
  34.         Button explicitActivationButton = (Button) findViewById(R.id.explicit_activation_button);
  35.         explicitActivationButton.setOnClickListener(new OnClickListener() {
  36.            
  37.             // Call startExplicitActivation() when pressed
  38.             @Override
  39.             public void onClick(View v) {
  40.                 startExplicitActivation();
  41.             }
  42.         });
  43.        
  44.         // Declare and setup Implicit Activation button
  45.         Button implicitActivationButton = (Button) findViewById(R.id.implicit_activation_button);
  46.         implicitActivationButton.setOnClickListener(new OnClickListener() {
  47.            
  48.             // Call startImplicitActivation() when pressed
  49.             @Override
  50.             public void onClick(View v) {
  51.                 startImplicitActivation();
  52.             }
  53.         });
  54.     }
  55.    
  56.    
  57.     // Start the ExplicitlyLoadedActivity
  58.    
  59.     private void startExplicitActivation() {
  60.         Log.i(TAG,"Entered startExplicitActivation()");
  61.        
  62.         Intent explicitIntent = new Intent(getApplicationContext(), ExplicitlyLoadedActivity.class);
  63.         startActivityForResult(explicitIntent, REQUEST_CODE);
  64.     }
  65.  
  66.     @Override
  67.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  68.         Log.i(TAG, "Entered onActivityResult()");
  69.  
  70.         if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
  71.             mUserTextView.setText(data.getStringExtra("string"));
  72.     }
  73.  
  74.     // Start a Browser Activity to view a web page or its URL
  75.  
  76.     private void startImplicitActivation() {
  77.         Log.i(TAG, "Entered startImplicitActivation()");
  78.  
  79.         Intent baseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
  80.  
  81.         Intent chooserIntent = Intent.createChooser(baseIntent, CHOOSER_TEXT);
  82.  
  83.         Log.i(TAG,"Chooser Intent Action:" + chooserIntent.getAction());
  84.  
  85.         startActivity(chooserIntent);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement