From 016138a7605b5e14aabe8ea0f1baba2a21c0848e Mon Sep 17 00:00:00 2001 From: yangzhiying Date: Sat, 26 Dec 2015 10:15:54 +0800 Subject: [PATCH] IntentTest --- app/.gitignore | 1 + app/build.gradle | 27 +++ app/proguard-rules.pro | 17 ++ .../glimmer/intenttest/ApplicationTest.java | 13 ++ app/src/main/AndroidManifest.xml | 38 ++++ .../com/glimmer/intenttest/MainActivity.java | 182 ++++++++++++++++++ .../glimmer/intenttest/SecondActivity.java | 15 ++ app/src/main/res/layout/activity_main.xml | 61 ++++++ app/src/main/res/layout/activity_second.xml | 8 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3418 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2206 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4842 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7718 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10486 bytes app/src/main/res/values-w820dp/dimens.xml | 6 + app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/dimens.xml | 5 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 ++ .../glimmer/intenttest/ExampleUnitTest.java | 15 ++ build.gradle | 23 +++ gradle.properties | 18 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 53637 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 160 +++++++++++++++ gradlew.bat | 90 +++++++++ settings.gradle | 1 + 27 files changed, 706 insertions(+) create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/com/glimmer/intenttest/ApplicationTest.java create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/glimmer/intenttest/MainActivity.java create mode 100644 app/src/main/java/com/glimmer/intenttest/SecondActivity.java create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/layout/activity_second.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/values-w820dp/dimens.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/com/glimmer/intenttest/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..d610a8e --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,27 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.2" + + defaultConfig { + applicationId "com.glimmer.intenttest" + minSdkVersion 15 + targetSdkVersion 23 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + useLibrary 'org.apache.http.legacy' +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + testCompile 'junit:junit:4.12' + compile 'com.android.support:appcompat-v7:23.1.1' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..3f3f667 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,17 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in /Users/glimmer/devTools/android-sdk-macosx/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} diff --git a/app/src/androidTest/java/com/glimmer/intenttest/ApplicationTest.java b/app/src/androidTest/java/com/glimmer/intenttest/ApplicationTest.java new file mode 100644 index 0000000..5905606 --- /dev/null +++ b/app/src/androidTest/java/com/glimmer/intenttest/ApplicationTest.java @@ -0,0 +1,13 @@ +package com.glimmer.intenttest; + +import android.app.Application; +import android.test.ApplicationTestCase; + +/** + * Testing Fundamentals + */ +public class ApplicationTest extends ApplicationTestCase { + public ApplicationTest() { + super(Application.class); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..6d607fb --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/glimmer/intenttest/MainActivity.java b/app/src/main/java/com/glimmer/intenttest/MainActivity.java new file mode 100644 index 0000000..268dc0e --- /dev/null +++ b/app/src/main/java/com/glimmer/intenttest/MainActivity.java @@ -0,0 +1,182 @@ +package com.glimmer.intenttest; + +import android.app.Activity; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.provider.CalendarContract; +import android.provider.ContactsContract; +import android.view.View; +import android.widget.Button; +import android.widget.ImageButton; +import android.widget.Toast; +import java.util.Calendar; +import java.util.List; +import org.apache.http.protocol.HTTP; + +public class MainActivity extends Activity implements View.OnClickListener { + private static final int PICK_CONTACT_REQUEST = 1; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + Button btn_call = (Button) findViewById(R.id.btn_call); + Button btn_map = (Button) findViewById(R.id.btn_map); + Button btn_web = (Button) findViewById(R.id.btn_web); + Button btn_send_email = (Button) findViewById(R.id.btn_send_email); + Button btn_create_calendar = (Button) findViewById(R.id.btn_create_calendar); + Button btn_pick_contact = (Button) findViewById(R.id.btn_pick_contact); + ImageButton ibtn_picture = (ImageButton) findViewById(R.id.ibtn_picture); + + btn_call.setOnClickListener(this); + btn_map.setOnClickListener(this); + btn_web.setOnClickListener(this); + btn_send_email.setOnClickListener(this); + /** + * 添加日期备注 + */ + btn_create_calendar.setOnClickListener(this); + btn_pick_contact.setOnClickListener(this); + + Intent intent = getIntent(); + String action = intent.getAction(); + String type = intent.getType(); + if (action.equals(Intent.ACTION_SEND)) { + if (type.startsWith("image/")) { + //handle sent text + Uri data = intent.getParcelableExtra(Intent.EXTRA_STREAM); + if (data != null) { + if (intent.getType().contains("image/")) { + Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show(); + ibtn_picture.setImageURI(data); + } + } + } else if (type.startsWith("text/")) { + //handle sent image + String text = intent.getParcelableExtra(Intent.EXTRA_TEXT); + if (text != null) { + Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); + + } + } + } else if (action.equals(Intent.ACTION_MAIN)) { + Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show(); + } + + } + + + @Override + public void onClick(View v) { + switch (v.getId()) { + case R.id.btn_call: + call(); + break; + case R.id.btn_map: + map("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California"); + break; + case R.id.btn_web: + web("http://www.android.com"); + break; + case R.id.btn_create_calendar: + calendar(); + break; + case R.id.btn_send_email: + sendEmail(); + break; + case R.id.btn_pick_contact: + pickContact(); + break; + default: + break; + + } + } + + /** + * 从content provider里获取数据 如何从被选的联系人中读出电话号码 + */ + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + + if (requestCode == PICK_CONTACT_REQUEST) { + if (resultCode == RESULT_OK) { + + Uri contactUri = data.getData(); + String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER}; + Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null); + if (cursor != null) { + cursor.moveToFirst(); + int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); + String number = cursor.getString(column); + Toast.makeText(MainActivity.this, number, Toast.LENGTH_SHORT).show(); + cursor.close(); + } + } + } + } + + private void calendar() { + Intent calendarIntent = new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI); + Calendar beginTime = Calendar.getInstance(); + beginTime.set(2012, 0, 19, 7, 30); //2012.1.19 7:30-10:30 + Calendar endTime = Calendar.getInstance(); + endTime.set(2012, 0, 19, 10, 30); + calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis()); + calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis()); + calendarIntent.putExtra(CalendarContract.Events.TITLE, "Glimmer class"); + calendarIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Secret dojo"); + startActivity(calendarIntent); + } + + private void sendEmail() { + Intent emailIntent = new Intent(Intent.ACTION_SEND); + emailIntent.setType(HTTP.PLAIN_TEXT_TYPE); + emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"jon@example.com"}); + emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject"); + //发送的消息 + emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message text"); + emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment")); + String title = (String) getResources().getText(R.string.app_name); + //指定显示的标题 + Intent chooser = Intent.createChooser(emailIntent, title); + startActivity(chooser); + } + + private void web(String uriString) { + Uri webpage = Uri.parse(uriString); + Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage); + startActivity(webIntent); + } + + private void map(String uriString) { + Uri location = Uri.parse(uriString); + Intent locationIntent = new Intent(Intent.ACTION_VIEW, location); + startActivity(locationIntent); + } + + private void call() { + Uri number = Uri.parse("tel:15019494700"); + Intent intent = new Intent(Intent.ACTION_DIAL, number); + //判断是否有app去接受这intent防止app crash + PackageManager packageManager = getPackageManager(); + List activities = packageManager.queryIntentActivities(intent, 0); + boolean isIntentSafe = activities.size() > 0; + if (isIntentSafe) { + startActivity(intent); + } + } + + private void pickContact() { + Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); + pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); + startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST); + } + +} diff --git a/app/src/main/java/com/glimmer/intenttest/SecondActivity.java b/app/src/main/java/com/glimmer/intenttest/SecondActivity.java new file mode 100644 index 0000000..914fb32 --- /dev/null +++ b/app/src/main/java/com/glimmer/intenttest/SecondActivity.java @@ -0,0 +1,15 @@ +package com.glimmer.intenttest; + +import android.app.Activity; +import android.os.Bundle; + +/** + * Created by glimmer on 15/12/22. + */ +public class SecondActivity extends Activity{ + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_second); + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..cba710b --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,61 @@ + + +