Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Working with Intent

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 372
    Comment on it

    An Android Intent is an object that carries message from one component to another component with-in the application or outside the application.

    There are three fundamental cases where one can use the Intents:

    case 1:- To start an activity

    The Intent describes the activity to start and carries any necessary data.

      Intent i = new Intent(this, TargetActivity.class);
       startActivity(i);
    

    Data through intent can carried from one component to another in the following ways:

    (1)- Sending primitive data through intent

     Intent i = new Intent(this, TargetActivity.class);
       i.putExtra("KeyValue", "ABC");
       startActivity(i);
    Extracting primitive data from intent
       Intent intent = getIntent();     
        String myString = intent.getStringExtra("KeyValue");
    

    (2)-Sending Bundle through intent

    Intent mIntent = new Intent(this, Example.class);
    Bundle mBundle = new Bundle();
    mBundle.putString("Key1", value);
    mIntent.putExtras(mBundle);
    startActivity(mIntent);
    Extracting bundle from intent
    Bundle extras = getIntent().getExtras();
        String value1 = extras.getString("Key1");

    case 2:- To start a service

    A Service is a component that performs the operations in the background without a user interface.

    For this, we need to create the service by extending Service class as:-

    package com.blogdemo;
    
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    
    public class DemoService extends Service {
    
      @Override
      public int onStart(Intent intent, int startId) {
      System.outprintln("This is my service")
        super.onStart(intent, startId);Y;
      }
    
      @Override
      public IBinder onBind(Intent intent) {
      //TODO for communication return IBinder implementation
        return null;
      }
      @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            Log.d(TAG, "FirstService destroyed");
        }
      } 
    

    Now call this service from your main activity:-

    package com.blogdemo;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    
    public class AndroidServiceCallingActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            startService(new Intent(this, DemoService.class));
        }
    }
    

    It is also needed to declare your service in the manifest as:-

    <service
      android:name="DemoService"
      android:icon="@drawable/ic_launcher"
      android:label="@string/service_name"
      >
    </service> 
    

    case 3:- To deliver a broadcast:

    package com.blogdemo;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class SendBraodcast extends BroadcastReceiver{
    
        public static final String CUSTOM_INTENT = "TEST";
    
                @Override
                public void onReceive(Context context, Intent intent) {
                    System.out.println("I am sending an intent");
                    Intent i = new Intent();
                    i.setAction(CUSTOM_INTENT);
                    context.sendBroadcast(i);
                }
            }
    

    Now create receiver class to receive the coming broadcasting intent

    package com.blogdemo;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class ReceiveBroadcast extends BroadcastReceiver{
    
        @Override
        public void onReceive(Context context, Intent intent) {
    
            if (intent.getAction().equals(SendBraodcast.CUSTOM_INTENT)) {
                    System.out.println("Here is my broadcasting Intent");
    
            }
        }
    }
    

    Types of Intents:-

    Explicit Intents

    These intents designate the target component by its name and they are typically used for application-internal messages - such as an activity starting an other activity. For example:

    // Explicit Intent by specifying its class name
    Intent i = new Intent(this, TargetActivity.class);
    startActivity(i);
    

    Implicit Intents

    These intents do not name a target and the field for the component name is left blank. Implicit intents are often used to activate components in other applications. For example:

    // Implicit Intent by specifying a URI
    Intent i = new Intent(Intent.ACTION_VIEW, 
    Uri.parse("http://www.finderd.com"));
    startActivity(i); 
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: