Implementation of intent Filter in Android

Posted By : Ashish Tyagi | 27-Nov-2013

Intent Filter In Android

Android Intent Filters gives the ability to receive Intents from other Android applications. If you are an Android device user, you're probably familiar with the SEND Intent. we are going to create a very simple app that will make itself available to a very specific Intent via Android Intent Filters.

We will Perform Two Task here :

1. Open your email attachment via your android app.

2. Open Specific Url via your android app.

Step 1). Create a default mobile project in Titanium Studio.

Step 1). For invoking android intent filter ,We have need custom AndroidManifest.xml

Creation of Custom AndroidManifest.xml

Titanium Studio will generate a new one AndroidManifest.xml for you on each build.You have just copy androidManifest.xml file from build/android directory to your newly create platform/android directory.

Open Specific Url via your android app.

Add new intent filter in your AndroidManifest file as given bellow:


	
	
	
	
	</intent-filter>
       

In Above code:

the data subelement tells the intent-filter that we are handling for HTTP requests targeted at the host www.oodlestechnologies.com

host > is used to determine a url ,when this url is open in Browser then a pop is open with given a option(like open in browser or your app name)

By making above changes in your android manifest file ,your app is automatically invoke when you open url (which is mention in host).

 

Open your email attachment via your android app.

1. Again you have to add a intent filter in androidManifest file ,But in this intent filter ,we have to define path and MIME Type for opening document,like application/pdf mime type is used here for pdf.


	
	
	
	
	
	

        

2. Now, you open your email attachment in your android device, then automatically your app is invoked to open attach document. here we have mention mime type for pdf ,if your click on text file or other format file then your app is not open.

3. At this Stage,your app is unable to open attach document,here you have to get the file path of attach document and then check it can be open in android webview or not.

     var currentIntentData = [];
	var currentActivity = Ti.Android.currentActivity;
	currentActivity.addEventListener('newintent', function(e) {
		var newintent = e.intent;
		currentIntentData = newintent.getData();
		var tmpFile = Ti.Filesystem.getFile(String(currentIntentData));
		var fileData = Ti.Filesystem.getFile(Ti.Filesystem.getTempDirectory() + tmpFile.name);
		var filename = fileData.nativePath; // This is file path for your attach document.
});

try {
			Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
				action : Ti.Android.ACTION_VIEW,
				type : 'application/pdf',
				data : filename
			}));

		} catch (err) {
			alert(“Your app is unable to open document, you have to search the marketplace for a PDF viewer
 ”)			});
        

4. When you click on attach email then your app is open and give a alert message for installing a PDF viewer. when you install a PDF viewer in your device and again run your app then your attach document is open in PDF Viewer.You androidManifest.xml file should like that.

androidManifest.xml



	

	
	

        



		

	
	
	
	
	
	
	
	
	
		
		
		
		
		
		
		


		
		
		










You can also open html file in your app without installing any app(like PDF Viewer) by creating a webview in android.

Hope it is useful for you :)

Aashish Tyagi

[email protected]

About Author

Author Image
Ashish Tyagi

Ashish is a iPhone application developer with experience in Objective-C , Titanium and Phonegap frameworks. Ashish loves watching movies in his free time.

Request for Proposal

Name is required

Comment is required

Sending message..