0

After I've implemented a solution for the Android app to post to web server and validate Google Order then issue a download link. Now I am trying to work on a code for the App to read the link from the thankyou.php page

<a href="http://domain.com/218348214.dat">Download File</a>

The file is a ".DAT" extension and coming from a certain link. The App should retrieve the link in a new page and let the customer download the file.

enter image description here

WiTon Nope
  • 166
  • 4
  • 20

1 Answers1

4

Inside you manifest file you need to add an intent filter:

    <activity
        android:name="Downloader">
        <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:host="domain.com"
                android:scheme="http" />
        </intent-filter>
    </activity>

Then inside your "Download" activity's onCreate:

public class Download extends Activity {

private String filename;

@Override
protected void onCreate (final Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView(<your layout file>);
    final Intent intent = getIntent ();
    final Uri data = intent.getData ();
    if (data != null) {
        final List<String> pathSegments = data.getPathSegments ();
        fileName = pathSegments.get (pathSegments.size () - 1);
    }
}

Then in the clickhandler for the download button you can use a view intent to act like a link in android.

button.setOnClickListener (new View.OnClickListener () {
        @Override public void onClick (final View v) {
            Uri intentUri = Uri.parse("http://domain.com/" + filename);

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(intentUri);
            startActivity(intent);
        }
    });
Cory Roy
  • 5,379
  • 2
  • 28
  • 47
  • thanks for the info but 1 thing still missing the file name is variable which is generated based on order info thus the file name isn't static using your code I still need to retrieve the filename and then ask the user to download. – WiTon Nope Feb 16 '16 at 18:32
  • Updated the answer with more parts now that I understand your problem more. – Cory Roy Feb 16 '16 at 19:18
  • thanks a lot for your sure, one thing is it possible to keep the user inside the app and initiate the download from there rather pulling him out to a web browser to complete his action? – WiTon Nope Feb 16 '16 at 19:20
  • Yes. You can do that. See: http://stackoverflow.com/questions/15758856/android-how-to-download-file-from-webserver – Cory Roy Feb 16 '16 at 19:44
  • thanks for the share. Your answer is helpful however the app is crashing and through me out. – WiTon Nope Feb 16 '16 at 19:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103641/discussion-between-cory-roy-and-witon-nope). – Cory Roy Feb 16 '16 at 20:07