Saturday, March 29, 2014

Calling Another Application Component in Your Application

Calling Another Application Component in Your Application - Hallo sahabat Google Android Developer Tutorial, Pada Artikel yang anda baca kali ini dengan judul Calling Another Application Component in Your Application, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel QRScanner Application, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Calling Another Application Component in Your Application
link : Calling Another Application Component in Your Application

Baca juga


Calling Another Application Component in Your Application


Calling Another Application Component in Your Application

               To allow other apps to start your activity, you need to add an<intent-filter> element in your manifest file for the corresponding <activity> element.
When your app is installed on a device, the system identifies your intent filters and adds the information to an internal catalog of intents supported by all installed apps. When an app calls startActivity() or startActivityForResult(), with an implicit intent, the system finds which activity (or activities) can respond to the intent.
Explicit and implicit intents
                        Intents are asynchronous messages which allow application components to request functionality from other Android components. Intents allow you to interact with components from the same applications as well as with components contributed by other applications.
                            Explicit intents explicitly define the component which should be called by the Android system, by using the  Java class as identifier.
      Implicit intents are used to call other Activity from another application.Implicit Intents do not specify the Java class which should be called. They specify the action which should be performed and optionally one can add data directly to the Bundle via the overloaded putExtra() methods of the Intent objects. Extras are key/value pairs. The key is always of type String. As value you can use the primitive data types (int, float, ...) plus objects of type String, Bundle, Parceable and Serializable.if a call to another activity is through implicit intent,it is sent to the Android system, it searches for all components which are registered for the specific action and the fitting data type.If only one component is found, Android starts this component directly. If several components are identified by the Android system, the user will get a selection dialog and can decide which component should be used for the intent.
The Demonstrative application uses ZXing QR/Barcode scanner application for scanning QR code.
1.Import Zxing application as Shown in the Figure 1 and install it in to emulator/device as shown in Figure 2.If it is emulator then it should be camera enabled.

 
2.Create  Android project with details as listed in the table below.
Property name
Property value
Project name
SRM_QRScanner
Package name
in.ac.srmuniv.qrscanner
Activity name
Scanner
Layout xml name
scanner
3..Copy the code  to the file scanner.xml in res/layout folder 
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="SRM QR_Scanner Application"
        android:textSize="25sp" />

    <ImageView
        android:id="@+id/scanner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="85dp"
        android:background="@drawable/qrscannericon"
        android:gravity="center"
        android:text="Click to Scan "
        android:textColor="#D70909" />


</RelativeLayout>
4.Copy the code in SRM_ScannerActivity.java. Activity.
packagein.ac.srmuniv.qrscanner;

importin.ac.srmuniv.qrscanner.R;

importandroid.app.Activity;
importandroid.content.ActivityNotFoundException;
importandroid.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
importandroid.view.Gravity;
import android.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.ImageView;
importandroid.widget.Toast;

public class SRM_ScannerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scanner);

        try {
            ImageView scanner = (ImageView)findViewById(R.id.scanner);
            scanner.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    startActivityForResult(intent, 0);
                }

            });


        } catch(ActivityNotFoundException anfe) {
            Log.e("onCreate", "Scanner Not Found", anfe);
        }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                // Handle successful scan
                Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format , Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400);
                toast.show();
                Intent i = newIntent(android.content.Intent.ACTION_VIEW,
                        Uri.parse(contents));
                startActivity(i);
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
                Toast toast = Toast.makeText(this, "Scan was Cancelled!", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 25, 400);
                toast.show();

            }
        }
    }

}
5.Modify the AndroidManifest.xml with necessary permission added.
           <?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.ac.srmuniv.qrscanner"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
   
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ScannerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
 </manifest>
6.Run the application in device


Figure 3 Shows SRM_QRScannerActivity of SRM_QRScanner application when launched.
Figure 4 Shows on Image Click Capture Activity of ZXing application is called



























Figure 5 Shows Capture Activity on returning result send the scanned data content extracted from QRCode
back to SRM_QRScanneractivity which inturn calls a browser through implicit intent
CODE EXPLANATION
        In this application QR Code scanner we have to  install zxing application which do both barcode and qrcode scanning.Download either source code or apk from the following link https://code.google.com/p/zxing/downloads/list.Figure 1 and 2 shows the downloaded ZXing.zip library is extracted and imported to our project and installed.The following code is an exert from AndroidManifest.xml file of ZXing project.The CaptureActivity's entry in manifest file has an intent filter with action name ""com.google.zxing.client.android.SCAN" which we have used as implicit intent call in our SRM_QRScanner application to call upon the Activity on a button click.The CaptureActivity activity from ZXing application (which is already installed) is called which returns the result of the scanned content emdeded in the QRCode.
  <activity android:name=".CaptureActivity" 
              android:screenOrientation="landscape"
              android:clearTaskOnLaunch="true"
              android:stateNotNeeded="true"
              android:configChanges="orientation|keyboardHidden"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
              android:windowSoftInputMode="stateAlwaysHidden">
      <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
      <intent-filter>
        <action android:name="com.google.zxing.client.android.SCAN"/>
        <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>
      <!-- Allow web apps to launch Barcode Scanner by linking to http://zxing.appspot.com/scan. -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="http"android:host="zxing.appspot.com" android:path="/scan"/>
      </intent-filter>
      <!-- We also support a Google Product Search URL. -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="http"android:host="www.google.com" android:path="/m/products/scan"/>
      </intent-filter>
      <!-- And the UK version. -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="http"android:host="www.google.co.uk" android:path="/m/products/scan"/>
      </intent-filter>
      <!-- Support zxing://scan/?... like iPhone app -->
      <intent-filter>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:scheme="zxing"android:host="scan" android:path="/"/>
      </intent-filter>
    </activity>


In our example SRM_QRScannerActivity following code snippet calls the CaptureActivity in ZXing application through implicit intent call through action message and passes extra data about type of scan.

                    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE""QR_CODE_MODE");
                    startActivityForResult(intent, 0);
The receiving component can access this information via the getAction() and getData() methods on the Intent object. This Intent object can be retrieved via the getIntent() method.The component which receives the intent can use the getIntent().getExtras() method call to get the extra data. 
The called Activity in ZXing application receives intent message.It may be registered to multiple action messages.Hence it gets the intent action message and extracts the data.
                                    Intent intent=getIntent();
                                    Intent intent = getIntent();
                                     .................
                                 if (intent != null) {
                                        String action = intent.getAction();
                                        String dataString = intent.getDataString();
If  activity is called with  the startActivityForResult() method call, you expect feedback from the called sub-activity. Once the sub-activity ends,the onActivityResult() method on the calling-activity is called and you can perform actions based on the result.
In the startActivityForResult() method call you can specify a result code to determine which activity you started. This result code is returned to you. The started activity can also set a result code which the caller can use to determine if the activity was canceled or not.
     When the CaptureActivity finishes, it sends resultcode and also returns data which would be the data extracted from  scanned image.It will be received  in the SRM_QRScannerActivity the following code portrays it. 
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
....
if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                String contents = intent.getStringExtra("SCAN_RESULT");
                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
                // Handle successful scan

1.Make ZXing project as Library project .


2Add it as Library in SRM_QRScanner application.



The Zxing Application can be made as Library  to our SRM_QRScanner application then you can import the classes of Zxing application in to our application.and call the CaptureActivity through explict intent call.You need not install Zxing application now separately since it is integrated as Library to the project
3.Modify the Androidmanifest.xml file to include CaptureActivity for explict intent call.
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.ac.srmuniv.qrscanner"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ScannerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.zxing.client.android.CaptureActivity"
            android:configChanges="orientation|keyboardHidden"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
            android:windowSoftInputMode="stateAlwaysHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
            <intent-filter>
                <action android:name="com.google.zxing.client.android.SCAN"/>

                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>
</manifest>
4.Modify the SRM_QRScannerActivity.java.Activity.Add the import and modify the onClick() method as shown below.
import com.google.zxing.client.android.CaptureActivity;
.......
public void onClick(View v) {
                    //Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    Intent intent = new Intent(ScannerActivity.this,com.google.zxing.client.android.CaptureActivity.class); 
.......           
Instead of implicit intent call we have made explicit intent call by directly calling the activity by class name since ZXing application is now part of our application as library.The capture activity is imported in to SRM_QRScannerActivity.
5.Run the Application you will have the similar working result.


Demikianlah Artikel Calling Another Application Component in Your Application

Sekianlah artikel Calling Another Application Component in Your Application kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Calling Another Application Component in Your Application dengan alamat link https://googleandroiddevelopertutorial.blogspot.com/2014/03/calling-another-application-component.html

Artikel Terkait

Calling Another Application Component in Your Application
4/ 5
Oleh

Berlangganan

Suka dengan artikel di atas? Silakan berlangganan gratis via email