Mobile Software Development for Android - I397

Slides:



Advertisements
Similar presentations
Android Application Development A Tutorial Driven Course.
Advertisements

Google Android Introduction to Mobile Computing. Android is part of the build a better phone process Open Handset Alliance produces Android Comprises.
Android 18: Content Providers Kirk Scott
Manifest File, Intents, and Multiple Activities. Manifest File.
1 Working with the Android Services Nilanjan Banerjee Mobile Systems Programming University of Arkansas Fayetteville, AR
Cosc 5/4730 Android SMS. A note first Depending on the API level, an import changes because of a deprecated API 3 uses – import android.telephony.gsm.SmsManager;
System broadcasts and services. System broadcast events. EventDescription Intent.ACTION_BOOT_COMPLETEDBoot completed. Requires the android.permission.RECE.
SMS. Short Message Service – Primarily text messages between mobile phones – First one sent December 3, 1982 “Merry Christmas” – In 2008 Approximately.
CONTENT PROVIDER. Content Provider  A content provider makes a specific set of the application's data available to other applications => Share data to.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Mobile Application Development with ANDROID Tejas Lagvankar UMBC 29 April 2009.
About me Yichuan Wang Android Basics Credit goes to Google and UMBC.
© Keren Kalif Intro to Android Development Written by Keren Kalif, Edited by Liron Blecher Contains slides from Google I/O presentation.
Mobile Computing Lecture#08 IntentFilters & BroadcastReceivers.
Google Cloud Messaging for Android (GCM) is a free service that helps developers send data from servers to their Android.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Data Storage: Part 4 (Content Providers). Content Providers Content providers allow the sharing of data between applications. Inter-process communication.
COMP 365 Android Development.  Manages access from a central database  Allows multiple applications to access the same data.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
Integrating with Android Services. Introduction  Android has numerous built-in functionality that can be called from within your applications  SMS/MMS.
Cosc 5/4730 Broadcast Receiver. Broadcast receiver A broadcast receiver (short receiver) – is an Android component which allows you to register for system.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
CS378 - Mobile Computing Intents.
16 Services and Broadcast Receivers CSNB544 Mobile Application Development Thanks to Utexas Austin.
Cosc 5/4730 Android App Widgets. App Widgets App Widgets are miniature application views that can be embedded in other applications (such as the Home.
Android Content Providers In Android security model, one application cannot directly access (read/write) other application's data. Every application has.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Android - Broadcast Receivers
Mobile Programming Midterm Review
CSE 486/586, Spring 2014 CSE 486/586 Distributed Systems Android Programming Steve Ko Computer Sciences and Engineering University at Buffalo.
Lecture 2: Android Concepts
Intents and Broadcast Receivers Dr. David Janzen Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution.
Mobile Software Development for Android - I397 IT COLLEGE, ANDRES KÄVER, WEB:
By: Eliav Menachi.  On Android, all application data (including files) are private to that application  Android provides a standard way for an application.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
CS371m - Mobile Computing Intents 1. Allow us to use applications and components that are already part of Android System – start activities – start services.
Cosc 4735 Nougat API 24+ additions.
Android Application -Architecture.
Content provider.
Intents and Broadcast Receivers
Android Content Providers & SQLite
Lecture 2: Android Concepts
Content Providers And Content Resolvers
Android Application Development 1 6 May 2018
Understanding Android Security
CS499 – Mobile Application Development
Broadcast receivers.
Messaging Unit-4.
CS499 – Mobile Application Development
Content Providers.
Broadcast Receivers A Android Component where you can register for system or application events, receive and react to broadcast intent. Each broadcast.
Mobile Software Development for Android - I397
Mobile Device Development
Android Programming Lecture 9
CIS 470 Mobile App Development
Android Notifications (Part 2) Plus Alarms and BroadcastReceivers
Application Fundamentals
CIS 470 Mobile App Development
Application Development A Tutorial Driven Course
Android Topics Android Activity Lifecycle and Experiment Toast
Activities and Intents
CIS 470 Mobile App Development
Understanding Android Security
Android Developer Fundamentals V2 Lesson 5
CIS 470 Mobile App Development
Application Fundamentals
Lecture 2: Android Concepts
Mobile Programming Broadcast Receivers.
Presentation transcript:

Mobile Software Development for Android - I397 IT College, Andres Käver, 2015-2016 Email: akaver@itcollege.ee Web: http://enos.itcollege.ee/~akaver/2015-2016/Distance/Android Skype: akaver

Android app components Logging Activity Intent Fragment Service ContentProvider BroadcastReceiver

Android - ContentProvider Access to a central repository of data Often provides its own UI for working with the data Primarily intended to be used by other applications Inter-process communication and secure data access

Android - ContentProvider Provider presents data to external applications as one or more tables Row represents an instance of some type of data the provider collects, and each column in the row represents an individual piece of data collected 

Android – ContentProvider - Accessing ContentResolver – CRUD NB! Permissions in manifest! getContentResolver().query(…) Similar to sql select statement // Queries the user dictionary and returns results mCursor = getContentResolver().query(     UserDictionary.Words.CONTENT_URI,   // The content URI of the words table     mProjection,                        // The columns to return for each row     mSelectionClause                    // Selection criteria     mSelectionArgs,                     // Selection criteria     mSortOrder);                        // The sort order for the returned rows <uses-permission android:name="android.permission.READ_USER_DICTIONARY">

Android – ContentProvider - Accessing String[] projection = { UserDictionary.Words.WORD }; Cursor cursor = getContentResolver().query( UserDictionary.Words.CONTENT_URI, projection, null, null, null); while (cursor.moveToNext()) { int index = cursor.getColumnIndex(UserDictionary.Words.WORD) String word = cursor.getString(index); }

Android – ContentProvider - Inserting // Defines a new Uri object that receives the result of the insertion Uri mNewUri; ... // Defines an object to contain the new values to insert ContentValues mNewValues = new ContentValues(); /*  * Sets the values of each column and inserts the word. The arguments to the "put"  * method are "column name" and "value"  */ mNewValues.put(UserDictionary.Words.APP_ID, "example.user"); mNewValues.put(UserDictionary.Words.LOCALE, "en_US"); mNewValues.put(UserDictionary.Words.WORD, "insert"); mNewValues.put(UserDictionary.Words.FREQUENCY, "100"); mNewUri = getContentResolver().insert(     UserDictionary.Word.CONTENT_URI,   // the user dictionary content URI     mNewValues                          // the values to insert );

Android – ContentProvider - Updating // Defines an object to contain the updated values ContentValues mUpdateValues = new ContentValues(); // Defines selection criteria for the rows you want to update String mSelectionClause = UserDictionary.Words.LOCALE +  "LIKE ?"; String[] mSelectionArgs = {"en_%"}; // Defines a variable to contain the number of updated rows int mRowsUpdated = 0; ... /*  * Sets the updated value and updates the selected words.  */ mUpdateValues.putNull(UserDictionary.Words.LOCALE); mRowsUpdated = getContentResolver().update(     UserDictionary.Words.CONTENT_URI,   // the user dictionary content URI     mUpdateValues                       // the columns to update     mSelectionClause                    // the column to select on     mSelectionArgs                      // the value to compare to );

Android – ContentProvider - Deleting // Defines selection criteria for the rows you want to delete String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?"; String[] mSelectionArgs = {"user"}; // Defines a variable to contain the number of rows deleted int mRowsDeleted = 0; ... // Deletes the words that match the selection criteria mRowsDeleted = getContentResolver().delete(     UserDictionary.Words.CONTENT_URI,   // the user dictionary content URI     mSelectionClause                    // the column to select on     mSelectionArgs                      // the value to compare to );

Android – ContentProvider – Creating your own Do you really need it? You want to offer complex data or files to other applications. You want to allow users to copy complex data from your app into other apps. You want to provide custom search suggestions using the search framework.

Android – ContentProvider – Creating your own File data or structured data Define a concrete implementation of the ContentProvider class and its required methods. Define the provider's authority string, its content URIs, and column names. If you want the provider's application to handle intents, also define intent actions, extras data, and flags. Also define the permissions that you will require for applications that want to access your data.

Android – ContentProvider – Calendar Calendar Provider API allows you to perform query, insert, update, and delete operations on calendars, events, attendees, reminders, etc. A user can have multiple calendars, and different calendars can be associated with different types of accounts (Google Calendar, Exchange)

Android – ContentProvider – Calendar <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"...>     <uses-sdk android:minSdkVersion="14" />     <uses-permission android:name="android.permission.READ_CALENDAR" />     <uses-permission android:name="android.permission.WRITE_CALENDAR" />     ... </manifest>

Android – ContentProvider – Contacts Accommodates a wide range of data sources and tries to manage as much data as possible for each person, with the result that its organization is complex. ContactsContract.Contacts table Rows representing different people, based on aggregations of raw contact rows. ContactsContract.RawContacts table Rows containing a summary of a person's data, specific to a user account and type. ContactsContract.Data table Rows containing the details for raw contact, such as email addresses or phone numbers.

Android – Broadcast receiver Component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens. Example: applications can register for the ACTION_BOOT_COMPLETED system event which is fired once the Android system has completed the boot process.

Android – Broadcast receiver Can be registered via the AndroidManifest.xml Or dynamically via the Context.registerReceiver() Implementing class for a receiver extends the BroadcastReceiver class If the event for which the broadcast receiver has registered happens, the onReceive() method of the receiver is called by the Android system After the onReceive() of the receiver class has finished, the Android system is allowed to recycle the receiver Android system excludes all receivers from receiving intents by default if the corresponding application has never been started by the user or if the user explicitly stopped the application via the Android menu (in Manage → Application).

Android – Broadcast receiver import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.TelephonyManager; import android.util.Log; public class MyPhoneReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle extras = intent.getExtras(); if (extras != null) { String state = extras.getString(TelephonyManager.EXTRA_STATE); Log.w("MY_DEBUG_TAG", state); if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { String phoneNumber = extras .getString(TelephonyManager.EXTRA_INCOMING_NUMBER); Log.w("MY_DEBUG_TAG", phoneNumber); } } } }

Android – Custom broadcast receiver @Override public void onReceive(Context context, Intent intent) { if (isOrderedBroadcast()) { Bundle extras = intent.getExtras(); if (extras != null) { value1 = extras.getDouble(“something"); } setResultCode(Activity.RESULT_OK); setResultData(value1 + “something");

Android – Broadcast receiver <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"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="MyPhoneReceiver" > <intent-filter> <action android:name="android.intent.action.PHONE_STATE" > </action> </intent-filter> </receiver> </application>

Android – Custom broadcast receiver <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.akaver.custombroadcast"> <application android:allowBackup="true" android:label="@string/app_name" android:icon="@mipmap/ic_launcher" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".CustomReceiver" > <intent-filter> <action android:name="com.akaver.sendCustomRequest" > </action> </intent-filter> </receiver> </application> </manifest>

Android – Broadcast receiver Sending broadcast Sending to custom receiver, waiting for answer Intent intent = new Intent(); intent.setAction("com.example.ACTION_SOMETHING_HAPPENED"); sendBroadcast(intent); Intent intent = new Intent(); intent.setAction("com.akaver.sendCustomRequest"); intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); intent.putExtra(“something", value); sendOrderedBroadcast(intent, null, new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String result = getResultData(); screen.setText(result); } }, null, Activity.RESULT_OK, null, null);

Android – Broadcast receiver - Pending Intent Token that you give to another application (e.g., notification manager, alarm manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code To perform a broadcast via a pending intent, get a PendingIntent via the getBroadcast() method of the PendingIntent class. To perform an activity via a pending intent, you receive the activity via PendingIntent.getActivity()

Android – Broadcast receiver - Pending Intent public void startAlert(View view) { EditText text = (EditText) findViewById(R.id.time); int i = Integer.parseInt(text.getText().toString()); Intent intent = new Intent(this, MyBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent); Toast.makeText(this, "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show(); }

Android – Broadcast receiver - Pending Intent So what's a pending intent? It's a token that your app process will give to the location process, and the location process will use it to wake up your app when an event of interest happens. So this basically means that your app in the background doesn't have to be always running. When something of interest happens, we will wake you up. This saves a lot of battery. PendingIntent mIntent = PendingIntent.getService(...); mLocationClient.requestLocationUpdates(locationRequest, mIntent); public void onHandleIntent(Intent intent) { String action = intent.getAction(); if (ACTION_LOCATION.equals(action)) { Location location = intent.getParcelableExtra(...) } }