CIS 470 Mobile App Development

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

MESSAGING. Overview  SMS sends short text messages between mobile phones.  Supports sending both text messages and data messages  MMS (multimedia messaging.
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;
Android - Broadcast Receivers
Android and s Ken Nguyen Clayton state University 2012.
Google map v2.
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Mobile Software Development for Android - I397
Lab7 – Appendix.
Lab7 – Advanced.
Lecture 3 Zablon Ochomo Android Layouts Lecture 3 Zablon Ochomo
Android Introduction Hello World
Android Intent Filters
CS240: Advanced Programming Concepts
Mobile Computing With Android ACST 4550 Intro to Android, Part 1
Android Introduction Hello World.
Android dan database 2 M. Taufiq, M. Kom.
Android Dr. Vaishali D. Khairnar IT Department
Android Widgets 1 7 August 2018
Android – Read/Write to External Storage
Mobile Software Development for Android - I397
Mobile Device Development
Picasso Revisted.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Sensor Programming
CIS 470 Mobile App Development
CMPE419 Mobile Application Development
CMPE419 Mobile Application Development
Chapter 1: Basics of Android, First App: HelloAndroid
Activities and Intents
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 493/EEC 492 Android Sensor Programming
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Notifications
CIS 470 Mobile App Development
CIS 493/EEC 492 Android Sensor Programming
CIS 470 Mobile App Development
Adding Components to Activity
BLP 4216 MOBİL UYGULAMA GELİŞTİRME-2
CMPE419 Mobile Application Development
CMPE419 Mobile Application Development
CIS 470 Mobile App Development
Mobile Programming Broadcast Receivers.
Android Sensor Programming
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Android Sensor Programming
CIS 470 Mobile App Development
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

CIS 470 Mobile App Development Lecture 12 Wenbing Zhao Department of Electrical Engineering and Computer Science Cleveland State University wenbing@ieee.org 11/13/2018 CIS 470: Mobile App Development

Messaging with SMS and Email Emails 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development SMS Make sure you figure at least one email account on your phone/emulator Create an app called MySMS. Modify activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.wenbing.myemails.MainActivity"> <Button android:text="Send Email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnSendEmail" app:layout_constraintLeft_toLeftOf="@+id/activity_main" app:layout_constraintTop_toTopOf="@+id/activity_main" app:layout_constraintRight_toRightOf="@+id/activity_main" app:layout_constraintBottom_toBottomOf="@+id/activity_main” android:onClick="onClick" /> </android.support.constraint.ConstraintLayout> 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development SMS Add permission to Manifest (at the right place!): <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wenbing.mysms"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" 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> </application> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.RECEIVE_SMS"/> </manifest> 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development SMS import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.util.Log; import android.widget.Toast; import android.provider.Telephony; public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = "SMS from "; if (bundle != null) { //---retrieve the SMS message received--- msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent); for (int i = 0; i < msgs.length; i++) { str += msgs[i].getMessageBody().toString(); //---get the message body--- str += msgs[i].getMessageBody().toString(); } //---display the new SMS message--- Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); Log.d("SMSReceiver", str); } } } Create a new java class named SMSReceiver 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development SMS import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.Manifest; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.telephony.SmsManager; import android.view.View; import android.widget.Toast; import android.content.Intent; import android.util.Log; public class MainActivity extends AppCompatActivity { final private int REQUEST_SEND_SMS = 123; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_DENIED) { Log.d("permission", "permission denied to SEND_SMS - requesting it"); String[] permissions = {Manifest.permission.SEND_SMS}; requestPermissions(permissions, REQUEST_SEND_SMS); } } } Add code to MainActivity.java 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development SMS @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case REQUEST_SEND_SMS: if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show(); System.out.println("permission granted"); } else { Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show(); } break; default: super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } public void onClick(View v) { //---the "phone number" of your emulator should be 5554--- sendSMS("5554", "Hello my friends!"); } //---sends an SMS message--- private void sendSMS(String phoneNumber, String message) { SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null, message, null, null); } } Add code to MainActivity.java 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Emails Make sure you figure at least one email account on your phone/emulator Create an app called MyEmails. Modify activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.wenbing.myemails.MainActivity"> <Button android:text="Send Email" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnSendEmail" app:layout_constraintLeft_toLeftOf="@+id/activity_main" app:layout_constraintTop_toTopOf="@+id/activity_main" app:layout_constraintRight_toRightOf="@+id/activity_main" app:layout_constraintBottom_toBottomOf="@+id/activity_main” android:onClick="onClick" /> </android.support.constraint.ConstraintLayout> 11/13/2018 CIS 470: Mobile App Development

CIS 470: Mobile App Development Emails public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v) { //---replace the following email addresses with real ones--- String[] to = {"someguy@example.com", "anotherguy@example.com"}; String[] cc = {"busybody@example.com"}; sendEmail(to, cc, "Hello", "Hello my friends!"); } private void sendEmail(String[] emailAddresses, String[] carbonCopies, String subject, String message) { Intent emailIntent = new Intent(Intent.ACTION_SEND); emailIntent.setData(Uri.parse("mailto:")); String[] to = emailAddresses; String[] cc = carbonCopies; emailIntent.putExtra(Intent.EXTRA_EMAIL, to); emailIntent.putExtra(Intent.EXTRA_CC, cc); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(Intent.EXTRA_TEXT, message); emailIntent.setType("message/rfc822"); startActivity(Intent.createChooser(emailIntent, "Email")); } } Make sure you figure at least one email account on your phone/emulator Create an app called MyEmails. Modify activity_main.xml: import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.net.Uri; import android.view.View; If you configured the Email service on your emulator, you should see the Email application launched in your emulator/device 11/13/2018 CIS 470: Mobile App Development

Sending Emails Programmatically Exercise (required): learn how to send emails directly from your app without opening the default email app based on the following discussion: https://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a/2033124#2033124 Build an app to demonstrate the functionality 11/13/2018 CIS 470: Mobile App Development