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:
- TextServicesManager tsm = (TextServicesManager) getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE);
- SpellCheckerSession spellCheckerSession = tsm.newSpellCheckerSession(null, null, this, true
Check results can be obtained by implementing SpellCheckerSessionListener interface:
- onGetSuggestions(SentenceSuggestionsInfo[] sentenceSuggestionsInfos)
- 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:
- compile 'com.google.android.gms:play-services-vision:10.0.1'
Then create the TextRecognizer object:
- TextRecognizer textRecognizer = new TextRecognizer.Builder(context).build();
Later realized Detector.Processor Interface interface monitoring results, the results are TextBlock Array.
- public class OcrDetectorProcessor implements Detector.Processor<TextBlock> {
- @Override
- public void receiveDetections(Detector.Detections<TextBlock> detections) {
- ...
- SparseArray<TextBlock> items = detections.getDetectedItems();
- ...
- }
- @Override
- public void release() {
- }
- }
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:
- TimingLogger timings = new TimingLogger("TAG_MYJOB", "MyJob");
Then create a log entry by addSplit(...):
- timings.addSplit("Phase 1 ready");
When dumpToLog() is used, the log information is printed out:
- timings.dumpToLog();
Note that to use Timing Logger, set the adb command to be Tag available:
- 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():
- View viewRoot = getWindow().getDecorView().getRootView();
- viewRoot.setDrawingCacheEnabled(true);
- Bitmap screenShotAsBitmap = Bitmap.createBitmap(viewRoot.getDrawingCache());
- viewRoot.setDrawingCacheEnabled(false);
- // 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:
- public void createPdfFromCurrentScreen() {
- new Thread() {
- public void run() {
- // Get the directory for the app's private pictures directory.
- final File file = new File(
- Environment.getExternalStorageDirectory(), "demo.pdf");
- if (file.exists ()) {
- file.delete ();
- }
- FileOutputStream out = null;
- try {
- out = new FileOutputStream(file);
- PdfDocument document = new PdfDocument();
- Point windowSize = new Point();
- getWindowManager().getDefaultDisplay().getSize(windowSize);
- PdfDocument.PageInfo pageInfo =
- new PdfDocument.PageInfo.Builder(
- windowSize.x, windowSize.y, 1).create();
- PdfDocument.Page page = document.startPage(pageInfo);
- View content = getWindow().getDecorView();
- content.draw(page.getCanvas());
- document.finishPage(page);
- document.writeTo(out);
- document.close();
- out.flush();
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Toast.makeText(PDFCreateActivity.this, "File created: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
- }
- });
- } catch (Exception e) {
- Log.d("TAG_PDF", "File was not created: "+e.getMessage());
- } finally {
- try {
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }.start();
- }
Thank you for reading.
This is a translation, the address of the original text. Discovering the Android API - Part 1