How to create two entry points to application using deeplink? - android

I want to create an application which will have two entry points.
The first will be a regular entry when the user clicks the app icon.
The second one will be through a deeplink which will be sent through push notifications.
I used this tutorial to build the deeplink.
I tried to use the deeplink to open the second entry point using adb, however I keep getting
Activity not started, unable to resolve Intent.
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.edwardkeselman.siemens">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ReportActivity" />
<activity android:name=".DetailsActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "example://gizmos” -->
<data android:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
</application>
</manifest>
Here is my DetailsActivity:
public class DetailsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
}
}
This is the adb command that I use to try to open the DetailsActivity:
adb shell am start -W -a android.intent.action.BROWSE -d "example://gizmoz" com.example.myname
Help will be much appreciated.

manifest.xml
<activity android:name=".DeepLinking">
<intent-filter android:label="#string/filter_deeplinking">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
<data
android:host="domain.in"
android:scheme="http" />
<!-- note that the leading "/" is required for pathPrefix -->
<!-- Accepts URIs that begin with "example://gizmos” -->
<data android:scheme="example"
android:host="gizmos" />
</intent-filter>
</activity>
DeepLinking.java
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.net.URLDecoder;
public class DeepLinking extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deep_linking);
Intent intent = getIntent();
String action = intent.getAction();
Uri uri_data = intent.getData();
String data=uri_data.toString();
Log.d("DATA : ",""+uri_data.toString());
if(data.equals("domain.in") || data.equals("http://domain.in") )
{
Intent i = new Intent(getApplicationContext(), Splash.class);
startActivity(i);
finish();
}
else if (!data.equals(null))
{
Intent i = new Intent(getApplicationContext(), DetailActivity.class);
startActivity(i);
finish();
}
else
{
Intent i = new Intent(getApplicationContext(), Splash.class);
startActivity(i);
finish();
}
}
}

Related

How do I specify multiple activities in AndroidManifest one for splash screen, one for mainactivity - pass the original intent to the mainactivy

I am only new to android this is my first application.
I am making progress on an app that fires when the NFC reader detects a TAG that contains NDEF messages, specifically when a URL is detect matching my domain.
I would like to add a splashscreen that fires triggered by the Android INTENT of but then passes the original INTENT to the mainactivity for further processing, I have made a start but not sure how to marry up the manifest and code to do what I am after.
MANIFEST.XML
<activity
android:name=".SplashScreenActivity"
android:theme="#style/Theme.MyApp.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:theme="#style/Theme.MyApp.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="<my custom domain>"/>
</intent-filter>
</activity>
Any help on how I should structure my Manifest to deal with this scenario, the SplashScreenActivity I could use the intent filters here to ensure that it is triggered, but then I create a new intent which means losing the NDEF extras.
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(SplashScreenActivity.this, MainActivity.class));
finish();
}
}
I would like to be able to "just pass" the whole original INTENT to the mainactivity but I am unsure how to do this.
Here is the code that extracts the NDEF messages from the INTENT any ideas appreciated.
private void readFromIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(action) ||
NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
logViewModel.insert(new LogEntry("READFROMINTENT ACTUALLY FIRED", "Action " + intent.getExtras()));
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
getNdefRecords(msgs);
addLinkToDbFromRecord(records);
}
//buildTagViews(msgs);
}
}
So I managed to solve this by doing the following.
I moved my intent filters from MainActivity to the .splashscreen actvity changed the category of mainactivity to DEFAULT, and allowed splashscreen activity to remain the LAUNCHER
<activity
android:name=".MainActivity"
android:theme="#style/Theme..NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.myap.app.SplashScreenActivity"
android:theme="#style/Theme.myapp.SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"
android:host="my domain"/>
</intent-filter>
</activity>
In my splashscreen activity i used the following code to copy the extras from the original intent, set the action to the expected action the mainactivity was designed to handle and used this to start the main activity
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent startMainActivity = new Intent(SplashScreenActivity.this, MainActivity.class);
startMainActivity.putExtras(getIntent());
startMainActivity.setAction(NfcAdapter.ACTION_NDEF_DISCOVERED);
startActivity(startMainActivity);
finish();
}
}

Where to call the Android Intent handler code in Gluon

Trying to handle the intents that are specified in AndroidManifest.xml. Not sure where to call the Service method that is created. Unlike in native android code where the intents are handled in the activity.
Referring to http://gluonhq.com/handling-android-native-activities-like-a-pro/
tried calling the service in the init method of the main class of the application (But seems like the intent handler methods are not called)
For the Gluon app to receive data from other apps in android
AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.nativeeg" android:versionCode="1" android:versionName="1.0">
<supports-screens android:xlargeScreens="true"/>
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="21"/>
<application android:label="NativeEg" android:name="android.support.multidex.MultiDexApplication" android:icon="#mipmap/ic_launcher">
<activity android:name="javafxports.android.FXActivity" android:label="NativeEg" android:configChanges="orientation|screenSize">
<meta-data android:name="main.class" android:value="com.nativeeg.NativeEg"/>
<meta-data android:name="debug.port" android:value="0"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
<activity android:name="com.gluonhq.impl.charm.down.plugins.android.PermissionRequestActivity" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
The Native Service to handle the intent
import nativeutility.ReceiveFromApps;
import javafxports.android.FXActivity;
import android.content.Intent;
/**
*
* #author Vaishnavi
*/
public class AndroidReceiveFromApps implements ReceiveFromApps{
#Override
public void receiveData() {
Intent intent = FXActivity.getInstance().getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
System.out.println("in handle intent!");
if ("text/plain".equals(type)) {
// Handle text being sent
System.out.println("text data");
} else if (type.startsWith("image/")) {
// Handle single image being sent
System.out.println("image data");
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
System.out.println("in handle intent!");
if (type.startsWith("image/")) {
// Handle multiple images being sent
System.out.println("multiple image data");
}
} else {
// Handle other intents, such as being started from the home screen
}
}
}
Gluon Main class:
public class NativeEg extends MobileApplication {
#Override
public void init() {
AppViewManager.registerViewsAndDrawer(this);
System.out.println("in main");
Services.get(ReceiveFromApps.class).ifPresent(service -> service.receiveData());
}
...
package nativeutility;
import com.gluonhq.charm.down.DefaultServiceFactory;
public class ReceiveFromAppsFactory extends DefaultServiceFactory<ReceiveFromApps> {
public ReceiveFromAppsFactory() {
super(ReceiveFromApps.class);
}}
Interface
package nativeutility;
public interface ReceiveFromApps {
public void receiveData();
}

how to share links with my app?

how do i open links in my webview shared by other apps?
I have tried this but it doesn't list my app anywhere-
TextView uri = (TextView) findViewById(R.id.urlField);
//if (Intent.ACTION_MAIN.equals(getIntent().getAction())) {
String intentUri = (new Intent("com.example.browsableintent.MY_ACTION"))
.toUri(Intent.URI_INTENT_SCHEME).toString();
uri.setText(intentUri);
Log.w("URLHandler", intentUri);
//} else {
Uri data = getIntent().getData();
if (data == null) {
uri.setText(" ");
} else {
uri.setText(getIntent().getData().toString());
}
//}
// Load URL from Browsable intent filter if there is a valid URL pasted
if (uri.length() > 0)
webView.loadUrl(url);
else
// dont load
manifest
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
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.
<intent-filter > ... </intent-filter>
intent filter defines the Intents that your activity "listens for" in order to launch.
<action android:name="ndroid.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
Action and category are both fields that get set on an Intent before it is "fired off" into the system. The system will then look for any activities that match both the action and category and if it finds one then it will launch that activity, or if it finds multiple it will show the user all of them and let them pick.
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.
Inside your Activity to the following
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the intent that started this activity
Intent intent = getIntent();
Uri data = intent.getData();
// Figure out what to do based on the intent type
if (intent.getType().indexOf("image/") != -1) {
// Handle intents with image data ...
} else if (intent.getType().equals("text/plain")) {
// Handle intents with text ...
}
}
If you want to return a result to the activity that invoked yours, simply call setResult() to specify the result code and result Intent. When your operation is done and the user should return to the original activity, call finish() to close (and destroy) your activity. For example:
// Create intent to deliver some kind of result data
Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri"));
setResult(Activity.RESULT_OK, result);
finish();
To read more on how to implements click here
how do i open links in my webview shared by other apps? I want my app to be here in this list
I have tried this but it doesn't list my app anywhere-
TextView uri = (TextView) findViewById(R.id.urlField);
// Get the intent that started this activity
Intent intent = getIntent();
String link = intent.getDataString();
uri.seText(link);
Taken from Chromium AndroidManifest.xml : https://cs.chromium.org/chromium/src/chrome/android/java/AndroidManifest.xml?q=AndroidManife&sq=package:chromium&l=1
<!-- Matches the common case of intents with no MIME type.-->
<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" />
<data android:scheme="https" />
</intent-filter>
<!-- Same filter as above but with MIME types.-->
<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" />
<data android:scheme="https" />
<data android:mimeType="text/html"/>
</intent-filter>
Then from other apps it's enought to use:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
and Android will list you in Select App dialog.
Complete example for client app:
In AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.j2ko.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="https"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
</application>
</manifest>
In activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.j2ko.myapplication.MainActivity">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</RelativeLayout>
In Main Activity:
package com.j2ko.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
final Intent intent = getIntent();
if (intent != null) {
handleIntent(intent);
}
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(final Intent intent) {
if (intent.getAction() == Intent.ACTION_VIEW) {
final String url = intent.getDataString();
mWebView.loadUrl(url);
}
}
}

Receiving data from other application comes null in android

I want to make my application like Instagram i can share pictures
directly from my application , like if i select any image from
gallery and when click on share my application shows on the list of chooser
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<activity
android:name=".ui.activities.SplashActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="sensorPortrait"
android:windowSoftInputMode="adjustResize" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Code for receiving data from intent is
private void initActivityState() {
if (getIntent() != null){
runShareActivityIntent();
}
}
public void runShareActivityIntent(){
Intent passIntent=getIntent();
String action = passIntent.getAction();
String type = passIntent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
Uri imageUri = (Uri) passIntent.getParcelableExtra(Intent.EXTRA_STREAM);
Intent intentcall = new Intent(this, MainActivity.class);
intentcall.putExtra(KEY_TYPE,type);
intentcall.putExtra(KEY_VALUE,imageUri.toString());
startActivity(intentcall);
finish();
}
}
Now in my case when my app is in background i am able to get
String type = passIntent.getType();
and i am able to get image uri but when my app is not running( kill app ) and i
select my application from chooser i get type as null
This worked for me:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
try adding permissions to your manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
You have to change in Manifeast.xml
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" />
</intent-filter>
In your Main activity you have to receive the intent on oncreate() method.
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
else if (type.startsWith("audio/")) {
handleSendAudio(intent); // Handle single audio being sent
}
}
Now whenever you call the application using send. You can see the icon of your app. When you get the intent you can change according to your need.
I hope this may help you.

Why isn't my app on the list of apps to open txt file?

I have an text reader app that is designed to receive intent from Android system when I click on a text file to open it. But my app isn't on the list popped up by the system. Below are my codes:
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastreceivertest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".BroadcastReceiverTest1Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_VIEW" />
<action android:name="android.intent.action.ACTION_EDIT" />
<action android:name="android.intent.action.ACTION_PICK" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.txt" />
<data android:host="*" />
</intent-filter>
</receiver>
</application>
</manifest>
My extended BroadcastReceiver
public final class MyBroadcastReceiver extends BroadcastReceiver {
private String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent i = new Intent(context, BroadcastReceiverTest1Activity.class);
i.putExtra("URI", intent.getData());
context.startActivity(i);
Log.d(TAG, "Leaving onReceived...");
}
}
My activity to be opened by the broadcast receiver
public class BroadcastReceiverTest1Activity extends Activity {
private String uri ="";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Intent intent = getIntent();
final String action = intent.getAction();
if(Intent.ACTION_VIEW.equals(action)){
uri = intent.getStringExtra("URI");
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(uri);
}
}
}
Thanks!
You need to associate your app with file extension. To do so, add these two line within intent filter and u'r good to go
<data android:scheme="file" />
<data android:mimeType="*/*"/>
<data android:pathPattern=".*\\.pdf" />
And your manifest would be look like this
<activity name="com.your.activity">
<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="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.txt" />
</intent-filter>
</activity>
<data android:scheme="file" /> => this define that the file must be local, not from http or else
<data android:mimeType="*/*" /> => match any mime type
<data android:pathPattern=".*\\.txt" /> => this is where you specify what extension you want to match
Hope this help
elaborating on HERO's pseudo code, this effectively works:
change the <manifest> like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastreceivertest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="9" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".BroadcastReceiverTest1Activity" >
<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="file" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.txt" />
</intent-filter>
</activity>
</application>
</manifest>
drop your broadcast receiver, because it is unnecessary.
change your BroadcastReceiverTest1Activity class (it does NOT need to be your MAIN activity, see BONUS below):
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class BroadcastReceiverTest1Activity extends Activity {
private String TAG = "TagOpenTxt";
private String uri ="";
private Uri uri2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent = getIntent();
final String action = intent.getAction();
if(Intent.ACTION_VIEW.equals(action)){
//uri = intent.getStringExtra("URI");
uri2 = intent.getData();
uri = uri2.getEncodedPath() + " complete: " + uri2.toString();
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(uri);
// now you call whatever function your app uses
// to consume the txt file whose location you now know
} else {
Log.d(TAG, "intent was something else: "+action);
}
}
}
you have effectively created an intent listener for TXT files, which will call your app IF the user decides to use it (unless the user has previously associated file type TXT to another app...)
your app does NOT need to be active to catch intents. once installed, the system recognizes it as one of the "goto apps" for the particular mime types and/or extensions (less easy than associating by mime type) you chose.
BONUS: you can have a separate MAIN activity and when your BroadcastReceiver is called, it will execute within the same sandbox as your application, without impacting it (you will have to implement that in your MAIN activity's onResume method).
you can read the text data into static variable(s) [sloppy] OR you can place it in a SQLite db, which is permanent AND safe, regardless of app and/or phone shutting down, for example.
you could have the activity selfterminating and never even firing up a layout/window - which is sort of weird in case your user wants some kind of confirmation that the txt file was correctly and completely consumed by app.
You have to register an ACTIVITY not a broadcast receiver with the filter attributes.

Categories

Resources