You don't know some amazing Android Api

Keywords: Android Java Google Gradle

Links to the original text: http://www.jianshu.com/p/4d21341f94ee 

This will be a series of articles about Android Api. AntSoft's Android team has been following the technological frontier for more than eight years, training Android technology in Budapest University of Technology and Economics. There is a tradition in the company that technology is shared every week. Here are some interesting APIs on Android platforms.

At present, Android has a lot of available dependency libraries, but in fact, some of the APIs of Android platform are little known, but very useful methods and classes, it is very interesting to study these APIs.

We know that the Java SE API that Android API relies on is also very large. According to statistics, Java SE 8 has 217 packages and 4240 methods, while java SE 7 has 209 packages and 4024 methods.


source: Java 8 Pocket Guide book by Robert Liguori, Patricia Liguor

In this series of articles, we will show some little-known Android API s from different perspectives and use them to write demo, open source address: https://github.com/peekler/GDG

Each API given in demo App is used in a different Activity, and can be accessed from the App home page to a different API demoActivity.


Spelling check

Android has an API for spelling checking starting at level 14, which can be passed through TextServicesManager Use it. You can even check a complete sentence from Level 16.

The method of use is very simple, through the ___________ TextServicesManager Can create SpellCheckerSession:

  1. TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);  
  2. SpellCheckerSession spellCheckerSession = tsm.newSpellCheckerSession(null, null, this, true

Check results can be obtained by implementing SpellCheckerSessionListener interface:

  1. onGetSuggestions(SentenceSuggestionsInfo[] sentenceSuggestionsInfos)
  2. onGetSentenceSuggestions(SentenceSuggestionsInfo[] sentenceSuggestionsInfos));

SentenceSuggestionsInfo The correct text, offset and all relevant information are stored in the data.


demo address SpellCheckerActivity

Character recognition

This is a feature provided in the Google Play Services Vision API, which can be easily introduced into the project through gradle dependency. It is important to note that the entire Play Services should not be introduced, because Play Services are very large, and all we need is a small part of them. https://developers.google.com/android/guides/setup Relevant help can be found.

Vision API The services included are:

  • Face recognition

  • Barcode scanning

  • Character recognition

Use Text Recognizer API It's simple:


First, introduce dependencies in build.gradle:

  1. compile 'com.google.android.gms:play-services-vision:10.0.1'

Then create the TextRecognizer object:

  1. TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build();

Later realized Detector.Processor Interface interface monitoring results, the results are TextBlock Array.

  1. public class OcrDetectorProcessor implements Detector.Processor<TextBlock> {
  2.  
  3.     @Override
  4.     public void receiveDetections(Detector.Detections<TextBlock> detections) {
  5.         ...
  6.         SparseArray<TextBlock> items = detections.getDetectedItems();
  7.         ...
  8.     }
  9.  
  10.     @Override
  11.     public void release() {
  12.     }
  13. }

Rational use of ____________ TextRecognizer Generally, you need to customize the inclusion SurfaceView View is used to display results on the screen. demo address OCRActivity , ocr There are some help classes.

TimingLogger

TimingLogger The time difference between two log information can be easily calculated as follows:

D/TAG_MYJOB: MyJob: begin
D/TAG_MYJOB: MyJob:      2002 ms, Phase 1 ready
D/TAG_MYJOB: MyJob:      2002 ms, Phase 2 ready
D/TAG_MYJOB: MyJob:      2001 ms, Phase 3 ready
D/TAG_MYJOB: MyJob: end, 6005 ms

Use TimingLogger:

  1. TimingLogger timings = new TimingLogger("TAG_MYJOB", "MyJob");

Then create a log entry by addSplit(...):

  1. timings.addSplit("Phase 1 ready");

When dumpToLog() is used, the log information is printed out:

  1. timings.dumpToLog();

Note that to use Timing Logger, set the adb command to be Tag available:

  1. setprop log.tag.TAG_MYJOB VERBOSE

demo address: TimingLoggerActivity.

Screenshots

In some cases, screenshots are very useful. There are also some third-party libraries such as Falcon Implementing this function, starting with level 21 MediaProjection Screen content and system voice information flow can be obtained in real time.

qi, sometimes using the standard Android API, saves screen content as Bitmap very simply through getWindow():

  1. View viewRoot = getWindow().getDecorView().getRootView();  
  2. viewRoot.setDrawingCacheEnabled(true);  
  3. Bitmap screenShotAsBitmap = Bitmap.createBitmap(viewRoot.getDrawingCache());  
  4. viewRoot.setDrawingCacheEnabled(false);  
  5. // use screenShotAsBitmap as you need



demo address: ScreenCaptureActivity.

Create PDF

Android has supported local content generation of PDF files since level 19.

First create a PageInfonew PdfDocument.PageInfo.Builder(w,h,pageNum).create(); then create a PDF file using the startPage([pageInfo]) in PDFDocument.

The following code creates a demo.pdf file:

  1. public void createPdfFromCurrentScreen() {  
  2.     new Thread() {
  3.         public void run() {
  4.             // Get the directory for the app's private pictures directory.
  5.             final File file = new File(
  6.                     Environment.getExternalStorageDirectory(), "demo.pdf");
  7.  
  8.             if (file.exists ()) {
  9.                 file.delete ();
  10.             }
  11.  
  12.             FileOutputStream out = null;
  13.             try {
  14.                 out = new FileOutputStream(file);
  15.  
  16.                 PdfDocument document = new PdfDocument();
  17.                 Point windowSize = new Point();
  18.                 getWindowManager().getDefaultDisplay().getSize(windowSize);
  19.                 PdfDocument.PageInfo pageInfo =
  20.                         new PdfDocument.PageInfo.Builder(
  21.                         windowSize.x, windowSize.y, 1).create();
  22.                 PdfDocument.Page page = document.startPage(pageInfo);
  23.                 View content = getWindow().getDecorView();
  24.                 content.draw(page.getCanvas());
  25.                 document.finishPage(page);
  26.                 document.writeTo(out);
  27.                 document.close();
  28.                 out.flush();
  29.  
  30.                 runOnUiThread(new Runnable() {
  31.                     @Override
  32.                     public void run() {
  33.                         Toast.makeText(PDFCreateActivity.this, "File created: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
  34.                     }
  35.                 });
  36.             } catch (Exception e) {
  37.                 Log.d("TAG_PDF", "File was not created: "+e.getMessage());
  38.             } finally {
  39.                 try {
  40.                     out.close();
  41.                 } catch (IOException e) {
  42.                     e.printStackTrace();
  43.                 }
  44.             }
  45.         }
  46.     }.start();
  47. }

Thank you for reading.

This is a translation, the address of the original text. Discovering the Android API - Part 1

Posted by reidme on Fri, 29 Mar 2019 06:21:30 -0700