Pdf viewer : Intent « Core Class « Android






Pdf viewer

    

package app.test;
import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;

public class Test extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create a Uri for files on external storage
         File externalFile = new File(Environment.getExternalStorageDirectory(), "a.pdf");
         Uri external = Uri.fromFile(externalFile);
         viewPdf(external);

        shareContent("Check this out!");
    }

    private void shareContent(String update) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, update);
        startActivity(Intent.createChooser(intent, "Share..."));
    }

    private void viewPdf(Uri file) {
        Intent intent;
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(file, "application/pdf");
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            // No application to view, ask to download one
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("No Application Found");
            builder.setMessage("Download one from Android Market?");
            builder.setPositiveButton("Yes, Please",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                            marketIntent
                                    .setData(Uri
                                            .parse("market://details?id=com.adobe.reader"));
                            startActivity(marketIntent);
                        }
                    });
            builder.setNegativeButton("No, Thanks", null);
            builder.create().show();
        }
    }
}

   
    
    
    
  








Related examples in the same category

1.Using Intent to open other Activity
2.Create Intent to open a Uri
3.extends IntentService
4.Phone Intent
5.Map Intent
6.Market Intent
7.Load Activity with Intent
8.Using Intent to make another phone call
9.extends IntentService to create your own Intent
10.Adding data bundle to Intent
11.Using Intent to show other Activities
12.Start Intent with Utility class
13.Load library Activity with Intent
14.Capture Image with Intent
15.Intent.ACTION_MEDIA_MOUNTED
16.Using Intent to record audio
17.Video Player Intent
18.Video Capture Intent
19.Implementing an application service that will run in response to an alarm, allowing us to move long duration work out of an intent receiver.
20.Example of various Intent flags to modify the activity stack.
21.Sample code that invokes the speech recognition intent API.
22.extends IntentService to upload a file
23.factory class for generating various intents
24.Open Web Page Intent
25.Is Intent Available
26.start MMS Intent
27.Access the Internet
28.Rotation One Demo
29.Media activity