Rotation One Demo : Intent « Core Class « Android






Rotation One Demo

    
/* Copyright (c) 2008-2009 -- CommonsWare, LLC

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
   
package com.commonsware.android.rotation.one;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.view.View;
import android.widget.Button;
import android.util.Log;

public class RotationOneDemo extends Activity {
  static final int PICK_REQUEST=1337;
  Button viewButton=null;
  Uri contact=null;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    Button btn=(Button)findViewById(R.id.pick);
    
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        Intent i=new Intent(Intent.ACTION_PICK,
                            Contacts.CONTENT_URI);

        startActivityForResult(i, PICK_REQUEST);
      }
    });
    
    viewButton=(Button)findViewById(R.id.view);
    
    viewButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        startActivity(new Intent(Intent.ACTION_VIEW, contact));
      }
    });
    
    restoreMe(savedInstanceState);
    
    viewButton.setEnabled(contact!=null);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
    if (requestCode==PICK_REQUEST) {
      if (resultCode==RESULT_OK) {
        contact=data.getData();
        viewButton.setEnabled(true);
      }
    }
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    
    if (contact!=null) {
      outState.putString("contact", contact.toString());
    }
  }
  
  private void restoreMe(Bundle state) {
    contact=null;
    
    if (state!=null) {
      String contactUri=state.getString("contact");
      
      if (contactUri!=null) {
        contact=Uri.parse(contactUri);
      }
    }
  }
}

   
    
    
    
  








Related examples in the same category

1.Using Intent to open other Activity
2.Create Intent to open a Uri
3.extends IntentService
4.Phone Intent
5.Map Intent
6.Market Intent
7.Load Activity with Intent
8.Using Intent to make another phone call
9.extends IntentService to create your own Intent
10.Adding data bundle to Intent
11.Using Intent to show other Activities
12.Start Intent with Utility class
13.Load library Activity with Intent
14.Capture Image with Intent
15.Intent.ACTION_MEDIA_MOUNTED
16.Using Intent to record audio
17.Video Player Intent
18.Video Capture Intent
19.Implementing an application service that will run in response to an alarm, allowing us to move long duration work out of an intent receiver.
20.Example of various Intent flags to modify the activity stack.
21.Sample code that invokes the speech recognition intent API.
22.extends IntentService to upload a file
23.factory class for generating various intents
24.Open Web Page Intent
25.Is Intent Available
26.start MMS Intent
27.Access the Internet
28.Pdf viewer
29.Media activity